egg-notifier 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p194@egg-notifier"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.14.12 ()" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ # if [[ -s Gemfile ]] && {
42
+ # ! builtin command -v bundle >/dev/null ||
43
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ # }
45
+ # then
46
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ # gem install bundler
48
+ # fi
49
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ # then
51
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+ gemspec
3
+ gem 'rake'
4
+ gem 'rspec'
5
+ gem 'fakeweb'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 mikegee
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Egg::Notifier
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby-egg-notifier', :require => 'egg-notifier'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby-egg-notifier
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new('spec')
8
+
9
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/egg-notifier/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "egg-notifier"
6
+ gem.version = Egg::Notifier::VERSION
7
+
8
+ gem.authors = ["mikegee"]
9
+ gem.email = ["gee.24@osu.edu"]
10
+ gem.homepage = "https://github.com/ASCTech/ruby-egg-notifier"
11
+ gem.summary = %q{A Ruby library to send events to Egg}
12
+ gem.description = %q{This simple Ruby library notifies Egg of events via an HTTP POST.}
13
+
14
+ gem.files = `git ls-files`.split($\)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_runtime_dependency 'json'
20
+ end
@@ -0,0 +1,12 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "time"
4
+
5
+ # The gemspec uses the full path of the version file, and 1.8 requires
6
+ # it twice if the path is different. So we expand_path here, too.
7
+ require File.expand_path('../egg-notifier/version', __FILE__)
8
+
9
+ require "egg-notifier/configuration"
10
+ require "egg-notifier/egg"
11
+ require "egg-notifier/egg/notifier"
12
+ require "egg-notifier/egg/exceptions"
@@ -0,0 +1,10 @@
1
+ class Egg
2
+ class << self
3
+ attr_accessor :api_key, :raise_on_failure
4
+ attr_reader :url
5
+
6
+ def url=(url)
7
+ @url = URI(url)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class Egg
2
+ def self.notify(*args)
3
+ Notifier.new(*args).execute!
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class Egg
2
+ class APIKeyNotDefined < RuntimeError; end
3
+ class UrlNotDefined < RuntimeError; end
4
+
5
+ class Notifier
6
+ class HTTPFailure < RuntimeError; end
7
+ end
8
+ end
@@ -0,0 +1,60 @@
1
+ class Egg
2
+ class Notifier
3
+
4
+ require 'net/https'
5
+
6
+ attr_accessor :event_name, :timestamp
7
+
8
+ def initialize(event_name, timestamp=Time.now)
9
+ @event_name = event_name
10
+
11
+ timestamp = Time.parse(timestamp.to_s) unless timestamp.respond_to?(:utc)
12
+ @timestamp = timestamp.utc.iso8601
13
+ end
14
+
15
+ def execute!
16
+ raise APIKeyNotDefined unless Egg.api_key
17
+ raise UrlNotDefined unless Egg.url
18
+
19
+ response = self.class.http.post(url.path, params.to_json, headers)
20
+
21
+ if Egg.raise_on_failure and !response.kind_of?(Net::HTTPSuccess)
22
+ if response
23
+ raise HTTPFailure.new("#{response.code}: #{response.body}")
24
+ else
25
+ raise HTTPFailure.new("nil HTTP response, that's odd.")
26
+ end
27
+ end
28
+
29
+ response
30
+ end
31
+
32
+
33
+ private
34
+
35
+ def self.http
36
+ return @http if @http
37
+
38
+ @http = Net::HTTP.new(url.host, url.port)
39
+ @http.use_ssl = true if url.scheme == 'https'
40
+ @http
41
+ end
42
+
43
+ def self.url
44
+ Egg.url
45
+ end
46
+
47
+ def url
48
+ Egg.url
49
+ end
50
+
51
+ def params
52
+ {:name => @event_name, :timestamp => @timestamp}
53
+ end
54
+
55
+ def headers
56
+ {'Content-type' => 'application/json', 'X-API-Key' => Egg.api_key}
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ class Egg
2
+ class Notifier
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Egg, 'configuration' do
4
+ subject { Egg } # not Egg.new
5
+
6
+ describe 'setting the URL' do
7
+ let(:url) { 'http://egg.example.com/events' }
8
+ before { subject.url = url }
9
+
10
+ its(:url) { should eq(URI(url)) }
11
+ its('url.scheme') { should eq('http') }
12
+ its('url.host') { should eq('egg.example.com') }
13
+ its('url.port') { should eq(80) }
14
+ its('url.path') { should eq('/events') }
15
+ end
16
+
17
+ describe 'setting the API key' do
18
+ let(:api_key) { '1234567890abcdef' }
19
+ before { subject.api_key = api_key }
20
+ its(:api_key) { should eq(api_key) }
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Egg, '#notify' do
4
+ subject { Egg } # not Egg.new
5
+
6
+ let(:event_name) { 'EventName' }
7
+ let(:notifier) { stub(Egg::Notifier) }
8
+
9
+ before do
10
+ Egg::Notifier.should_receive(:new).with(*notification_args).and_return(notifier)
11
+ notifier.should_receive(:execute!).and_return(:response)
12
+ end
13
+
14
+ context 'not specifying a timestamp' do
15
+ let(:notification_args) { [event_name] }
16
+
17
+ it 'should return the response from Egg::Notifier#execute!' do
18
+ subject.notify(*notification_args).should eq(:response)
19
+ end
20
+ end
21
+
22
+ context 'specifying the timestamp' do
23
+ let(:timestamp) { Time.now - 3600 }
24
+ let(:notification_args) { [event_name, timestamp] }
25
+
26
+ it 'should return the same response' do
27
+ subject.notify(*notification_args).should eq(:response)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Egg::Notifier do
4
+
5
+ describe '#new' do
6
+ describe 'the default timestamp is now' do
7
+ let(:timestamp) { Time.at(100000000) }
8
+ subject { Egg::Notifier.new('the-event-name') }
9
+ before { Time.should_receive(:now).and_return(timestamp) }
10
+ its(:timestamp) { should eq(timestamp.utc.iso8601) }
11
+ end
12
+ end
13
+
14
+ describe '#execute!' do
15
+ let(:api_key) { '1234567890abcdef' }
16
+ subject { Egg::Notifier.new('the-event-name', '2012-08-23T14:25:00Z') }
17
+
18
+ it 'should raise an exception when the API key is not defined' do
19
+ Egg.url = 'http://egg.example.com/events'
20
+ lambda { subject.execute! }.should raise_error(Egg::APIKeyNotDefined)
21
+ end
22
+
23
+ it 'should raise an exception when the url is not defined' do
24
+ Egg.api_key = api_key
25
+ lambda { subject.execute! }.should raise_error(Egg::UrlNotDefined)
26
+ end
27
+
28
+ context 'with api_key and url defined' do
29
+ let(:http) { Egg::Notifier.send(:http) }
30
+ let(:json) { '{"name":"the-event-name","timestamp":"2012-08-23T14:25:00Z"}' }
31
+ let(:headers) { {'Content-type' => 'application/json', 'X-API-Key' => api_key} }
32
+
33
+ before do
34
+ Egg.api_key = api_key
35
+ Egg.url = 'http://egg.example.com/events'
36
+ http.should_receive(:post).with('/events', json, headers)
37
+ end
38
+
39
+ it 'should work right' do
40
+ subject.execute!
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '.http' do
46
+ let(:url) { URI('http://egg.example.com/events') }
47
+ before do
48
+ Egg::Notifier.instance_variable_set('@http', nil)
49
+ Egg.url = url
50
+ http = Net::HTTP.new(url.host, url.port)
51
+ Net::HTTP.should_receive(:new).once.and_return(http)
52
+ end
53
+ it 'should only instantiate the HTTP object once' do
54
+ 3.times { Egg::Notifier.send(:http) }
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Egg::Notifier, 'raise an exception on HTTP failure' do
4
+ subject { Egg::Notifier.new('some-event') }
5
+
6
+ let(:url) { 'http://egg.example.com/events' }
7
+
8
+ before do
9
+ Egg.url = url
10
+ Egg.api_key = '1234567890abcdef'
11
+ FakeWeb.register_uri(:post, url, :status => 406, :body => 'Rejected')
12
+ end
13
+
14
+ context 'raise_on_failure set to true' do
15
+ before { Egg.raise_on_failure = true }
16
+ specify do
17
+ lambda { subject.execute! }.should raise_error(Egg::Notifier::HTTPFailure)
18
+ end
19
+
20
+ it 'should include the code and body in the Error message' do
21
+ begin
22
+ subject.execute!
23
+ rescue Egg::Notifier::HTTPFailure => e
24
+ e.message.should eq("406: Rejected")
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'raise_on_failure set to false' do
30
+ before { Egg.raise_on_failure = false }
31
+ specify do
32
+ lambda { subject.execute! }.should_not raise_error(Egg::Notifier::HTTPFailure)
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'fake_web'
4
+
5
+ require 'egg-notifier'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.order = 'random'
11
+
12
+ FakeWeb.allow_net_connect = false
13
+
14
+ # FakeWeb.register_uri(:post, 'http://egg.example.com/events', :status => 201)
15
+
16
+ config.before(:each) { Egg.instance_variable_set('@url', nil); Egg.api_key = nil }
17
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: egg-notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mikegee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70167371929660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70167371929660
25
+ description: This simple Ruby library notifies Egg of events via an HTTP POST.
26
+ email:
27
+ - gee.24@osu.edu
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - .rvmrc
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - egg-notifier.gemspec
40
+ - lib/egg-notifier.rb
41
+ - lib/egg-notifier/configuration.rb
42
+ - lib/egg-notifier/egg.rb
43
+ - lib/egg-notifier/egg/exceptions.rb
44
+ - lib/egg-notifier/egg/notifier.rb
45
+ - lib/egg-notifier/version.rb
46
+ - spec/configuration_spec.rb
47
+ - spec/egg_notify_helper_method_spec.rb
48
+ - spec/notifier_spec.rb
49
+ - spec/raise_on_failure_spec.rb
50
+ - spec/spec_helper.rb
51
+ homepage: https://github.com/ASCTech/ruby-egg-notifier
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.17
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A Ruby library to send events to Egg
75
+ test_files:
76
+ - spec/configuration_spec.rb
77
+ - spec/egg_notify_helper_method_spec.rb
78
+ - spec/notifier_spec.rb
79
+ - spec/raise_on_failure_spec.rb
80
+ - spec/spec_helper.rb