-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POST Request Notifier #406
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# encoding: utf-8 | ||
require 'net/https' | ||
|
||
module Backup | ||
module Notifier | ||
class PostRequest < Base | ||
|
||
## | ||
# The host to send the request to | ||
attr_accessor :host | ||
|
||
## | ||
# The Application Token | ||
# Defined by the user to possibly separate backup logs | ||
attr_accessor :token | ||
|
||
|
||
def initialize(model, &block) | ||
super(model) | ||
|
||
instance_eval(&block) if block_given? | ||
end | ||
|
||
private | ||
|
||
## | ||
# Notify the user of the backup operation results. | ||
# `status` indicates one of the following: | ||
# | ||
# `:success` | ||
# : The backup completed successfully. | ||
# : Notification will be sent if `on_success` was set to `true` | ||
# | ||
# `:warning` | ||
# : The backup completed successfully, but warnings were logged | ||
# : Notification will be sent, including a copy of the current | ||
# : backup log, if `on_warning` was set to `true` | ||
# | ||
# `:failure` | ||
# : The backup operation failed. | ||
# : Notification will be sent, including the Exception which caused | ||
# : the failure, the Exception's backtrace, a copy of the current | ||
# : backup log and other information if `on_failure` was set to `true` | ||
# | ||
def notify!(status) | ||
name = case status | ||
when :success then 'Success' | ||
when :failure then 'Failure' | ||
when :warning then 'Warning' | ||
end | ||
message = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name | ||
|
||
post(message, status) | ||
end | ||
|
||
# Send a POST request to the host with a message & status | ||
def post(message, status) | ||
|
||
# Add the token to the URL if present | ||
if token | ||
uri = URI.parse([host, token].join('/')) | ||
else | ||
uri = URI.parse(host) | ||
end | ||
|
||
http_request = Net::HTTP::Post.new(uri.request_uri) | ||
http_request.initialize_http_header({"User-Agent" => "BackupClient/#{Backup::Version.current}"}) | ||
|
||
http_request.set_form_data({:message => message, :status => status, :backup_version => Backup::Version.current}) | ||
|
||
http = Net::HTTP.new(uri.host, uri.port) | ||
|
||
if uri.scheme == 'https' | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
end | ||
|
||
http_result = http.request(http_request) | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# encoding: utf-8 | ||
|
||
require File.expand_path('../../spec_helper.rb', __FILE__) | ||
|
||
describe Backup::Notifier::PostRequest do | ||
let(:model) { Backup::Model.new(:test_trigger, 'test label') } | ||
let(:notifier) do | ||
Backup::Notifier::PostRequest.new(model) do |notifier| | ||
notifier.host = 'http://host.com' | ||
notifier.token = 'token' | ||
end | ||
end | ||
|
||
it 'should be a subclass of Notifier::Base' do | ||
Backup::Notifier::PostRequest. | ||
superclass.should == Backup::Notifier::Base | ||
end | ||
|
||
describe '#initialize' do | ||
after { Backup::Notifier::PostRequest.clear_defaults! } | ||
|
||
it 'should load pre-configured defaults through Base' do | ||
Backup::Notifier::PostRequest.any_instance.expects(:load_defaults!) | ||
notifier | ||
end | ||
|
||
it 'should pass the model reference to Base' do | ||
notifier.instance_variable_get(:@model).should == model | ||
end | ||
|
||
context 'when no pre-configured defaults have been set' do | ||
it 'should use the values given' do | ||
notifier.host.should == 'http://host.com' | ||
notifier.token.should == 'token' | ||
|
||
notifier.on_success.should == true | ||
notifier.on_warning.should == true | ||
notifier.on_failure.should == true | ||
end | ||
|
||
it 'should use default values if none are given' do | ||
notifier = Backup::Notifier::PostRequest.new(model) | ||
notifier.token.should be_nil | ||
notifier.host.should be_nil | ||
|
||
notifier.on_success.should == true | ||
notifier.on_warning.should == true | ||
notifier.on_failure.should == true | ||
end | ||
end # context 'when no pre-configured defaults have been set' | ||
|
||
context 'when pre-configured defaults have been set' do | ||
before do | ||
Backup::Notifier::PostRequest.defaults do |n| | ||
n.token = 'the_token' | ||
n.host = 'http://anotherhost.com' | ||
n.on_failure = false | ||
end | ||
end | ||
|
||
it 'should use pre-configured defaults' do | ||
notifier = Backup::Notifier::PostRequest.new(model) | ||
|
||
notifier.token.should == 'the_token' | ||
notifier.host.should == 'http://anotherhost.com' | ||
|
||
notifier.on_success.should == true | ||
notifier.on_warning.should == true | ||
notifier.on_failure.should == false | ||
end | ||
|
||
it 'should override pre-configured defaults' do | ||
notifier = Backup::Notifier::PostRequest.new(model) do |n| | ||
n.token = 'new_token' | ||
n.host = 'http://testhost.com' | ||
n.on_success = false | ||
n.on_failure = true | ||
end | ||
|
||
notifier.token.should == 'new_token' | ||
notifier.host.should == 'http://testhost.com' | ||
|
||
notifier.on_success.should == false | ||
notifier.on_warning.should == true | ||
notifier.on_failure.should == true | ||
end | ||
end # context 'when pre-configured defaults have been set' | ||
end # describe '#initialize' | ||
|
||
# Can't really test this area without a working host | ||
# | ||
# describe '#notify!' do | ||
# context 'when status is :success' do | ||
# it 'should send Success message' do | ||
# notifier.expects(:send_message).with( | ||
# '[Backup::Success] test label (test_trigger)' | ||
# ) | ||
# notifier.send(:notify!, :success) | ||
# end | ||
# end | ||
|
||
# context 'when status is :warning' do | ||
# it 'should send Warning message' do | ||
# notifier.expects(:send_message).with( | ||
# '[Backup::Warning] test label (test_trigger)' | ||
# ) | ||
# notifier.send(:notify!, :warning) | ||
# end | ||
# end | ||
|
||
# context 'when status is :failure' do | ||
# it 'should send Failure message' do | ||
# notifier.expects(:send_message).with( | ||
# '[Backup::Failure] test label (test_trigger)' | ||
# ) | ||
# notifier.send(:notify!, :failure) | ||
# end | ||
# end | ||
# end # describe '#notify!' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## | ||
# Post Request [Notifier] | ||
# | ||
notify_by PostRequest do |post| | ||
post.on_success = true | ||
post.on_warning = true | ||
post.on_failure = true | ||
|
||
post.host = "http://host.com" | ||
post.token = "TOKEN" # This is optional | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the reason behind using OpenSSL::SSL::VERIFY_NONE ?
I think it should not disable certificate verification by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That code was copied from another gem so I just left it as it is
On 24 May 2013, at 19:26, Michael Bumann notifications@github.com
wrote: