keikokuc 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in keikokuc.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Harold Giménez
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
+ # Keikokuc
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'keikokuc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install keikokuc
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 'Add 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,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/keikokuc.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/keikokuc/version', __FILE__)
3
+ require 'factory_girl'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Harold Giménez"]
7
+ gem.email = ["harold.gimenez@gmail.com"]
8
+ gem.description = %q{Keikoku API client}
9
+ gem.summary = %q{Keikoku API client}
10
+ gem.homepage = ""
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "keikokuc"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Keikokuc::VERSION
18
+
19
+ gem.add_dependency 'rest-client'
20
+ gem.add_dependency 'yajl-ruby'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'factory_girl'
23
+ gem.add_development_dependency 'sham_rack'
24
+ end
@@ -0,0 +1,20 @@
1
+ module HandlesTimeout
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ RequestTimeout = Class.new
7
+
8
+ module ClassMethods
9
+ def handle_timeout(method_name)
10
+ alias_method "#{method_name}_without_timeout", method_name
11
+ define_method method_name do |args|
12
+ begin
13
+ Timeout::timeout(5) { send("#{method_name}_without_timeout", args) }
14
+ rescue Timeout::Error
15
+ [nil, RequestTimeout]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,76 @@
1
+ require 'rest-client'
2
+ require 'yajl'
3
+ require 'timeout'
4
+
5
+ # Internal: Handles HTTP requests/responses to the keikoku API
6
+ #
7
+ # This class is meant to be used internally by Keikokuc
8
+ class Keikokuc::Client
9
+ include HandlesTimeout
10
+
11
+ InvalidNotification = Class.new
12
+ Unauthorized = Class.new
13
+
14
+ attr_accessor :producer_api_key
15
+
16
+ def initialize(opts = {})
17
+ @producer_api_key = opts[:producer_api_key]
18
+ end
19
+
20
+ # Internal: posts a new notification to keikoku
21
+ #
22
+ # attributes - a hash containing notification attributes
23
+ #
24
+ # Examples
25
+ #
26
+ # client = Keikokuc::Client.new(producer_api_key: 'abcd')
27
+ # response, error = client.post_notification(message: 'hello')
28
+ #
29
+ # Returns
30
+ #
31
+ # two objects:
32
+ # The response as a hash
33
+ # The error if any (nil if no error)
34
+ #
35
+ # Possible errors include:
36
+ #
37
+ # * `Client::Timeout` if the request takes longer than 5 seconds
38
+ # * `Client::InvalidNotification` if the response indicates
39
+ # invalid notification attributes
40
+ # * `Client::Unauthorized` if API key auth fails
41
+ def post_notification(attributes)
42
+ begin
43
+ response = notifications_api.post(encode_json(attributes), {'X-KEIKOKU-AUTH' => producer_api_key})
44
+ [parse_json(response), nil]
45
+ rescue RestClient::UnprocessableEntity => e
46
+ [parse_json(e.response), InvalidNotification]
47
+ rescue RestClient::Unauthorized => e
48
+ [{}, Unauthorized]
49
+ end
50
+ end
51
+ handle_timeout :post_notification
52
+
53
+ private
54
+ def notifications_api # :nodoc:
55
+ @notifications_api ||= RestClient::Resource.new(api_url)
56
+ end
57
+
58
+ def api_url # :nodoc:
59
+ "https://keikoku.herokuapp.com/api/v1/notifications"
60
+ end
61
+
62
+ def encode_json(hash) # :nodoc:
63
+ Yajl::Encoder.encode(hash)
64
+ end
65
+
66
+ def parse_json(data) # :nodoc:
67
+ symbolize_keys(Yajl::Parser.parse(data)) if data
68
+ end
69
+
70
+ def symbolize_keys(hash) # :nodoc:
71
+ hash.inject({}) do |result, (k, v)|
72
+ result[k.to_sym] = v
73
+ result
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,71 @@
1
+ # Public: Encapsulates a keikoku notification
2
+ #
3
+ # This is the entry point for dealing with notifications
4
+ #
5
+ # Examples
6
+ #
7
+ # notification = Keikokuc::Notification.new(message: 'hello',
8
+ # severity: 'info',
9
+ # target: 'sunny-skies-42'
10
+ # producer_api_key: 'abcd')
11
+ # if notificaiton.publish
12
+ # # persist notification
13
+ # else
14
+ # # handle error
15
+ # end
16
+ #
17
+ class Keikokuc::Notification
18
+ ATTRS = %w[message target severity url
19
+ producer_api_key remote_id errors].freeze
20
+
21
+ attr_accessor *ATTRS
22
+
23
+ # Public: class constructor
24
+ #
25
+ # opts - a hash of attributes to be set on constructed object
26
+ #
27
+ # Examples
28
+ #
29
+ # notification = Keikokuc::Notification.new(message: 'hello')
30
+ #
31
+ # All keys on matching ATTRS will be set
32
+ def initialize(opts = {})
33
+ ATTRS.each do |attribute|
34
+ if opts.has_key?(attribute.to_sym)
35
+ send("#{attribute}=", opts[attribute])
36
+ end
37
+ end
38
+ end
39
+
40
+ # Public: publishes this notification to keikoku
41
+ #
42
+ # This method sets the `remote_id` attribute if it succeeds.
43
+ # If it fails, the `errors` hash will be populated.
44
+ #
45
+ # Returns a boolean set to true if publishing succeeded
46
+ def publish
47
+ response, error = client.post_notification(to_hash)
48
+ if error.nil?
49
+ self.remote_id = response[:id]
50
+ self.errors = nil
51
+ elsif error == Keikokuc::Client::InvalidNotification
52
+ self.errors = response[:errors]
53
+ end
54
+ error.nil?
55
+ end
56
+
57
+ # Internal: coerces this notification to a hash
58
+ #
59
+ # Returns this notification's attributes as a hash
60
+ def to_hash
61
+ ATTRS.inject({}) do |h, attribute|
62
+ h[attribute.to_sym] = send(attribute)
63
+ h
64
+ end
65
+ end
66
+
67
+ private
68
+ def client # :nodoc:
69
+ @client ||= Keikokuc::Client.new(producer_api_key: producer_api_key)
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Keikokuc
2
+ VERSION = "0.0.1"
3
+ end
data/lib/keikokuc.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'handles_timeout'
2
+ require 'keikokuc/version'
3
+ require 'keikokuc/client'
4
+ require 'keikokuc/notification'
5
+
6
+ module Keikokuc
7
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :notification, class: Keikokuc::Notification do
3
+ skip_create
4
+
5
+ message 'Your database is over limits'
6
+ target 'cloudy-skies-243'
7
+ severity 'info'
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'sham_rack'
3
+ module Keikokuc
4
+ describe Client, '#post_notification' do
5
+ let(:fake_keikoku) { FakeKeikoku.new }
6
+
7
+ after { ShamRack.unmount_all }
8
+
9
+ it 'publishes a new notification' do
10
+ ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
11
+ fake_keikoku.register_publisher({api_key: 'abc'})
12
+ client = Client.new(producer_api_key: 'abc')
13
+ result, error = client.post_notification(message: 'hello',
14
+ severity: 'info')
15
+ result[:id].should_not be_nil
16
+ error.should be_nil
17
+ end
18
+
19
+ it 'handles invalid notifications' do
20
+ ShamRack.at('keikoku.herokuapp.com', 443) do |env|
21
+ [422, {}, StringIO.new(Yajl::Encoder.encode({ errors: :srorre }))]
22
+ end
23
+
24
+ response, error = Client.new.post_notification({})
25
+ error.should == Client::InvalidNotification
26
+ response[:errors].should == 'srorre'
27
+ end
28
+
29
+ it 'handles authentication failures' do
30
+ ShamRack.mount(fake_keikoku, "keikoku.herokuapp.com", 443)
31
+ fake_keikoku.register_publisher({api_key: 'abc'})
32
+ client = Client.new(producer_api_key: 'bad one')
33
+ result, error = client.post_notification(message: 'hello',
34
+ severity: 'info')
35
+ result[:id].should be_nil
36
+ error.should == Client::Unauthorized
37
+ end
38
+
39
+ it 'handles timeouts' do
40
+ RestClient::Resource.any_instance.stub(:post).and_raise Timeout::Error
41
+ response, error = Client.new.post_notification({})
42
+ response.should be_nil
43
+ error.should == Client::RequestTimeout
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ module Keikokuc
4
+ describe Notification, '#publish' do
5
+ it 'publishes to keikoku and stores an id' do
6
+ fake_client = double
7
+ Client.stub(new: fake_client)
8
+ fake_client.
9
+ should_receive(:post_notification).with do |args|
10
+ args[:message].should == 'hello'
11
+ end.
12
+ and_return([{ id: 1 }, nil])
13
+
14
+ notification = build(:notification, message: 'hello')
15
+
16
+ result = notification.publish
17
+ result.should be_true
18
+
19
+ notification.remote_id.should == 1
20
+ end
21
+
22
+ it 'returns false when publishing fails and stores errors' do
23
+ fake_client = double
24
+ Client.stub(new: fake_client)
25
+ fake_client.
26
+ should_receive(:post_notification).with do |args|
27
+ args[:message].should be_nil
28
+ end.
29
+ and_return([{ errors: { attributes: { message: ['is not present'] }}}, Keikokuc::Client::InvalidNotification])
30
+
31
+ notification = build(:notification, message: nil)
32
+
33
+ result = notification.publish
34
+ result.should be_false
35
+
36
+ notification.remote_id.should be_nil
37
+ notification.errors[:attributes][:message].should == ['is not present']
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'keikokuc'
8
+ require './spec/factories'
9
+ require './spec/support/fake_keikoku'
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+
21
+ config.include FactoryGirl::Syntax::Methods
22
+ end
@@ -0,0 +1,75 @@
1
+ class FakeKeikoku
2
+ def initialize
3
+ @publishers = []
4
+ @notifications = []
5
+ end
6
+
7
+ def register_publisher(opts)
8
+ @publishers << opts
9
+ end
10
+
11
+ def publisher_by_api_key(api_key)
12
+ @publishers.detect { |p| p[:api_key] == api_key }
13
+ end
14
+
15
+ def call(env)
16
+ with_rack_env(env) do
17
+ if request_path == '/api/v1/notifications' && request_verb == 'POST'
18
+ if publisher_by_api_key(request_api_key)
19
+ notification = Notification.new({id: next_id}.merge(request_body))
20
+ @notifications << notification
21
+ [200, { }, [Yajl::Encoder.encode({id: notification.id})]]
22
+ else
23
+ [401, { }, ["Not authorized"]]
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+ def rack_env
31
+ @rack_env
32
+ end
33
+
34
+ def with_rack_env(rack_env)
35
+ @rack_env = rack_env
36
+ response = yield
37
+ ensure
38
+ @rack_env = nil
39
+ response
40
+ end
41
+
42
+ def request_path
43
+ rack_env['PATH_INFO']
44
+ end
45
+
46
+ def request_verb
47
+ rack_env['REQUEST_METHOD']
48
+ end
49
+
50
+ def request_body
51
+ raw_body = rack_env["rack.input"].read
52
+ rack_env["rack.input"].rewind
53
+ Yajl::Parser.parse(raw_body)
54
+ end
55
+
56
+ def request_api_key
57
+ rack_env["HTTP_X_KEIKOKU_AUTH"]
58
+ end
59
+
60
+ def next_id
61
+ @@sequence ||= 0
62
+ @@sequence += 1
63
+ end
64
+
65
+ class Notification
66
+ def initialize(opts)
67
+ @opts = opts
68
+ opts.each do |key, value|
69
+ self.class.send :define_method, key do
70
+ value
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keikokuc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Harold Giménez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &70304962398080 !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: *70304962398080
25
+ - !ruby/object:Gem::Dependency
26
+ name: yajl-ruby
27
+ requirement: &70304962397660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70304962397660
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70304962397240 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70304962397240
47
+ - !ruby/object:Gem::Dependency
48
+ name: factory_girl
49
+ requirement: &70304962396820 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70304962396820
58
+ - !ruby/object:Gem::Dependency
59
+ name: sham_rack
60
+ requirement: &70304962396400 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70304962396400
69
+ description: Keikoku API client
70
+ email:
71
+ - harold.gimenez@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - keikokuc.gemspec
83
+ - lib/handles_timeout.rb
84
+ - lib/keikokuc.rb
85
+ - lib/keikokuc/client.rb
86
+ - lib/keikokuc/notification.rb
87
+ - lib/keikokuc/version.rb
88
+ - spec/factories.rb
89
+ - spec/keikoku/client_spec.rb
90
+ - spec/keikoku/notification_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/support/fake_keikoku.rb
93
+ homepage: ''
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.10
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Keikoku API client
117
+ test_files:
118
+ - spec/factories.rb
119
+ - spec/keikoku/client_spec.rb
120
+ - spec/keikoku/notification_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/fake_keikoku.rb