simple_sdk_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ = Simple SDK Builder
2
+
3
+ == Version 1.0.0
4
+ * Initial release of gem
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 RevPAR Collective, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,72 @@
1
+ = Simple SDK Builder
2
+
3
+ A set of libraries that make it easy to build an SDK gem on top of an HTTP web service.
4
+
5
+ Currently, only RESTful JSON services are supported.
6
+
7
+ == Synopsis:
8
+
9
+ class MyNotFoundError < StandardError
10
+ def initialize(response)
11
+ super "object not found: #{response.body}"
12
+ end
13
+ end
14
+
15
+ class MyUnknownError < StandardError; end
16
+
17
+ class MyBaseClass
18
+ include SimpleSDKBuilder::Base
19
+
20
+ config :service_url => 'https://api.davidmdawson.com'
21
+ config :timeout => 15000
22
+ config :error_handlers => {
23
+ '404' => MyNotFoundError,
24
+ '*' => MyUnknownError
25
+ }
26
+
27
+ end
28
+
29
+ class MyResource < MyBaseClass
30
+ include SimpleSDKBuilder::Resource
31
+
32
+ simple_sdk_attribute :id, :name, :value
33
+
34
+ class << self
35
+
36
+ def all
37
+ response = json_request( :path => "/my_resources" )
38
+ response.build(MyResource)
39
+ end
40
+
41
+ def find(id)
42
+ response = json_request( :path => "/my_resources/#{id}" )
43
+ response.build(MyResource)
44
+ end
45
+
46
+ end
47
+
48
+ def create
49
+ response = json_request( :path => "/my_resources", :method => :post, :body => self )
50
+ self.attributes = response.parsed_body
51
+ self
52
+ end
53
+
54
+ def update(attrs)
55
+ response = json_request( :path => "/my_resources/#{id}", :method => :put, :body => attrs )
56
+ self.attributes = response.parsed_body
57
+ self
58
+ end
59
+
60
+ def destroy
61
+ json_request( :path => "/my_resources/#{id}", :method => :delete )
62
+ # leaves current object alone
63
+ end
64
+
65
+ end
66
+
67
+ all_resources = MyResource.all
68
+ some_resource = MyResource.find(1)
69
+ new_resource = MyResource.new( :name => 'test', :value => 'this is a test' ).create
70
+ new_resource.update( :value => 'this test is almost done...' )
71
+ new_resource.destroy
72
+
@@ -0,0 +1,140 @@
1
+ require 'active_model'
2
+ require 'json'
3
+ require 'simply_configurable'
4
+ require 'typhoeus'
5
+
6
+ module SimpleSDKBuilder
7
+ module Base
8
+
9
+ DEFAULT_TIMEOUT_MILLISECONDS = 15000
10
+
11
+ def self.included(klass)
12
+ klass.class_eval do
13
+ include SimplyConfigurable
14
+ end
15
+
16
+ klass.extend ClassMethods
17
+
18
+ klass.config :service_url => "http://localhost:3000/v1"
19
+ klass.config :timeout => DEFAULT_TIMEOUT_MILLISECONDS
20
+ klass.config :error_handlers => {
21
+ nil => ConnectionError,
22
+ '404' => NotFoundError,
23
+ '422' => RequestError,
24
+ '*' => UnknownError
25
+ }
26
+ klass.config :logger => default_logger
27
+ end
28
+
29
+ def ==(other)
30
+ self.equal?(other) || (self.id && self.id == other.id && self.class == other.class)
31
+ end
32
+
33
+ def eql?(other)
34
+ self == other
35
+ end
36
+
37
+ def json_request(options = {})
38
+ self.class.json_request(options)
39
+ end
40
+
41
+ def logger
42
+ self.class.logger
43
+ end
44
+
45
+ private
46
+
47
+ def self.default_logger
48
+ if defined?(::Rails)
49
+ ::Rails.logger
50
+ else
51
+ logger = ::Logger.new(STDERR)
52
+ logger.level = ::Logger::INFO
53
+ logger
54
+ end
55
+ end
56
+
57
+ module ClassMethods
58
+
59
+ def json_request(options = {})
60
+ options = config.merge({
61
+ :path => '/',
62
+ :method => :get,
63
+ :body => nil,
64
+ :params => nil,
65
+ :build => false
66
+ }).merge(options)
67
+
68
+ options[:headers] = {
69
+ 'Content-Type' => 'application/json'
70
+ }.merge(options[:headers] || {})
71
+
72
+ hydra = config[:hydra] || Typhoeus::Hydra.new
73
+
74
+ url = "#{options[:service_url]}#{options[:path]}"
75
+
76
+ request_body = options[:body]
77
+ if request_body && !request_body.is_a?(String)
78
+ request_body = request_body.to_json
79
+ end
80
+
81
+ request = Typhoeus::Request.new url,
82
+ :method => options[:method],
83
+ :timeout => options[:timeout],
84
+ :headers => options[:headers],
85
+ :params => options[:params],
86
+ :body => request_body
87
+
88
+ logger.debug "running HTTP #{options[:method]}: #{url}; PARAMS: #{options[:params]}; BODY: #{request_body};"
89
+
90
+ hydra.queue(request)
91
+ hydra.run
92
+
93
+ response = request.response
94
+
95
+ logger.debug "received response code #{response.code}; BODY: #{response.body};"
96
+
97
+ check_response(response)
98
+ Response.new(response)
99
+ end
100
+
101
+ def check_response(response)
102
+ return if response.code.to_s =~ /^2/
103
+
104
+ error_handlers = config[:error_handlers] || {}
105
+ error_handlers[nil] ||= StandardError
106
+ error_handlers['*'] ||= StandardError
107
+
108
+ if response.timed_out?
109
+ raise error_handlers[nil], "timed out before receiving response"
110
+ end
111
+
112
+ # search for exact match
113
+ error_handlers.each do |key, error|
114
+ if key.is_a?(String) || key.is_a?(Symbol)
115
+ if response.code.to_s == key.to_s
116
+ raise error, response
117
+ end
118
+ end
119
+ end
120
+
121
+ # search for regex match
122
+ error_handlers.each do |key, error|
123
+ if key.is_a?(Regexp)
124
+ if !!(key =~ response.code.to_s)
125
+ raise error, response
126
+ end
127
+ end
128
+ end
129
+
130
+ raise error_handlers['*'], "an error occurred with the response; code: #{response.code}; body: #{response.body};"
131
+ end
132
+
133
+ def logger
134
+ config[:logger]
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+ end
@@ -0,0 +1,34 @@
1
+ require 'typhoeus'
2
+
3
+ module SimpleSDKBuilder
4
+
5
+ # An abstract super-class of all other SimpleSDKBuilder Error classes
6
+ class Error < StandardError
7
+ attr_reader :code
8
+
9
+ def initialize(response)
10
+ if response.is_a?(Typhoeus::Response)
11
+ @code = response.code
12
+ super(response.body)
13
+ else
14
+ super(response.to_s)
15
+ end
16
+ end
17
+ end
18
+
19
+ # Indicates an error establishing a connection to the API, or a timeout that occurs while
20
+ # making an API call. Is relatively common and transient- worth retrying in important cases.
21
+ class ConnectionError < Error; end
22
+
23
+ # Indicates that the requested object was not found.
24
+ class NotFoundError < Error; end
25
+
26
+ # Indicates that something was wrong with the request. See the message for details.
27
+ class RequestError < Error; end
28
+
29
+ # Indicates any other API error (generally should be non-transient and not worth retrying). The
30
+ # error code and string will be present in this error's message if one was provided in the
31
+ # response.
32
+ class UnknownError < Error; end
33
+
34
+ end
@@ -0,0 +1,122 @@
1
+ require 'active_model'
2
+ require 'active_support/core_ext/hash'
3
+ require 'active_support/inflector'
4
+
5
+ module SimpleSDKBuilder
6
+ module Resource
7
+
8
+ def self.included(klass)
9
+ klass.class_eval do
10
+ extend ActiveModel::Naming
11
+ include ActiveModel::Conversion
12
+ include ActiveModel::Serializers::JSON
13
+ self.include_root_in_json = false
14
+ end
15
+
16
+ klass.extend ClassMethods
17
+ end
18
+
19
+ def initialize(attrs = {})
20
+ @attributes = {}.with_indifferent_access
21
+ #self.class.simple_sdk_attributes.each do |attr|
22
+ # @attributes[attr] = nil
23
+ #end
24
+ self.attributes = attrs
25
+ end
26
+
27
+ def attributes
28
+ @attributes
29
+ end
30
+
31
+ def attributes=(attributes)
32
+ attributes.each do |key, value|
33
+ if self.class.simple_sdk_attributes.include?(key.to_s)
34
+ self.send("#{key}=", value)
35
+ end
36
+ end
37
+ end
38
+
39
+ def persisted?
40
+ !!id
41
+ end
42
+
43
+ private
44
+
45
+ def write_attribute(attr, value, options = {})
46
+ @attributes[attr] = build_attribute(attr, value, options)
47
+ end
48
+
49
+ def build_attribute(attr, value, options)
50
+ options = {
51
+ :class_name => nil,
52
+ :nested => false
53
+ }.merge(options)
54
+
55
+ if options[:nested] && value.is_a?(Array)
56
+ value.collect { |v| build_attribute(attr, v, options) }
57
+ elsif options[:nested] && value.is_a?(Hash) && value.include?("0")
58
+ arr = []
59
+ (0...(value.size)).each do |i|
60
+ arr.push value[i.to_s]
61
+ end
62
+ build_attribute(attr, arr, options)
63
+ elsif options[:nested] && value.is_a?(Hash)
64
+ class_name = options[:class_name] || guess_class_name(attr)
65
+ nested_class = eval(class_name)
66
+ nested_class.new(value)
67
+ else
68
+ value
69
+ end
70
+ end
71
+
72
+ def guess_class_name(attr)
73
+ namespace = ''
74
+ class_name = self.class.name
75
+ if class_name.rindex(':')
76
+ namespace = class_name[0..class_name.rindex(':')]
77
+ end
78
+ attr.chomp!("_attributes")
79
+ result = "#{namespace}#{attr.camelize.singularize}"
80
+ result
81
+ end
82
+
83
+ module ClassMethods
84
+
85
+ def simple_sdk_attribute(*attrs)
86
+ attrs.each do |attr|
87
+ attr = attr.to_s
88
+ simple_sdk_attributes.push(attr)
89
+ define_method attr do
90
+ @attributes[attr]
91
+ end
92
+ define_method "#{attr}=" do |value|
93
+ write_attribute(attr, value)
94
+ end
95
+ end
96
+ end
97
+
98
+ def simple_sdk_nested_attribute(attr, options = {})
99
+ options = {
100
+ :nested => true
101
+ }.merge(options)
102
+ attr = attr.to_s
103
+ simple_sdk_attributes.push(attr)
104
+ simple_sdk_attributes.push("#{attr}_attributes") # for ActiveRecord nested attributes
105
+ define_method attr do
106
+ @attributes[attr] ||= []
107
+ end
108
+ alias_method :"#{attr}_attributes", :"#{attr}" # for ActiveRecord nested attributes
109
+ define_method "#{attr}=" do |value|
110
+ write_attribute(attr, value, options)
111
+ end
112
+ alias_method :"#{attr}_attributes=", :"#{attr}=" # for ActiveRecord nested attributes
113
+ end
114
+
115
+ def simple_sdk_attributes
116
+ @simple_sdk_attributes ||= []
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+ end
@@ -0,0 +1,44 @@
1
+ require 'json'
2
+
3
+ module SimpleSDKBuilder
4
+ class Response
5
+
6
+ def initialize(typhoeus_response)
7
+ @typhoeus_response = typhoeus_response
8
+ end
9
+
10
+ def code
11
+ @typhoeus_response.code
12
+ end
13
+
14
+ def time
15
+ @typhoeus_response.time
16
+ end
17
+
18
+ def headers
19
+ @typhoeus_response.headers_hash
20
+ end
21
+
22
+ def body
23
+ @typhoeus_response.body
24
+ end
25
+
26
+ def parsed_body
27
+ @parsed_body ||= JSON.parse(body)
28
+ end
29
+
30
+ def build(type)
31
+ if parsed_body.is_a?(Array)
32
+ parsed_body.collect { |value| type.new.from_json(value.to_json) }
33
+ else
34
+ type.new.from_json(body)
35
+ end
36
+ end
37
+
38
+ def build_search_results(type)
39
+ SearchResults.new(self, type)
40
+ end
41
+
42
+ end
43
+ end
44
+
@@ -0,0 +1,20 @@
1
+ module SimpleSDKBuilder
2
+ class SearchResults
3
+
4
+ attr_reader :result_count, :links, :results
5
+
6
+ def initialize(response, type)
7
+ @result_count = response.headers['X-Count'].to_s.to_i
8
+
9
+ @links = {}
10
+ response.headers['Link'].to_s.split(',').each do |link|
11
+ if link =~ /<(.*)>; rel="(.*)"/
12
+ @links[$2] = $1
13
+ end
14
+ end
15
+
16
+ @results = response.build(type)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleSDKBuilder
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,20 @@
1
+ # This file should require or autoload the implementation of this gem
2
+ #
3
+ # Example:
4
+ #
5
+ # require 'my_gem/my_extension'
6
+ #
7
+ # module MyGem
8
+ # autoload :MyClass, 'my_gem/my_class'
9
+ # end
10
+
11
+ require 'simple_sdk_builder/errors'
12
+
13
+ module SimpleSDKBuilder
14
+
15
+ autoload :Base, 'simple_sdk_builder/base'
16
+ autoload :Resource, 'simple_sdk_builder/resource'
17
+ autoload :Response, 'simple_sdk_builder/response'
18
+ autoload :SearchResults, 'simple_sdk_builder/search_results'
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_sdk_builder
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - David Dawson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ hash: 7
27
+ segments:
28
+ - 3
29
+ - 0
30
+ version: "3.0"
31
+ version_requirements: *id001
32
+ name: activemodel
33
+ prerelease: false
34
+ type: :runtime
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 7
42
+ segments:
43
+ - 3
44
+ - 0
45
+ version: "3.0"
46
+ version_requirements: *id002
47
+ name: activesupport
48
+ prerelease: false
49
+ type: :runtime
50
+ - !ruby/object:Gem::Dependency
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 1
59
+ - 6
60
+ version: "1.6"
61
+ version_requirements: *id003
62
+ name: json
63
+ prerelease: false
64
+ type: :runtime
65
+ - !ruby/object:Gem::Dependency
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ hash: 9
72
+ segments:
73
+ - 0
74
+ - 1
75
+ version: "0.1"
76
+ version_requirements: *id004
77
+ name: simply_configurable
78
+ prerelease: false
79
+ type: :runtime
80
+ - !ruby/object:Gem::Dependency
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ hash: 13
87
+ segments:
88
+ - 0
89
+ - 3
90
+ version: "0.3"
91
+ version_requirements: *id005
92
+ name: typhoeus
93
+ prerelease: false
94
+ type: :runtime
95
+ description: A set of libraries that supports building an object-oriented ruby SDK on top of a RESTful JSON web service.
96
+ email: daws23@gmail.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - README.rdoc
103
+ - CHANGELOG.rdoc
104
+ - LICENSE.txt
105
+ files:
106
+ - lib/simple_sdk_builder/base.rb
107
+ - lib/simple_sdk_builder/errors.rb
108
+ - lib/simple_sdk_builder/resource.rb
109
+ - lib/simple_sdk_builder/response.rb
110
+ - lib/simple_sdk_builder/search_results.rb
111
+ - lib/simple_sdk_builder/version.rb
112
+ - lib/simple_sdk_builder.rb
113
+ - README.rdoc
114
+ - CHANGELOG.rdoc
115
+ - LICENSE.txt
116
+ homepage: https://github.com/daws/simple_sdk_builder
117
+ licenses: []
118
+
119
+ post_install_message:
120
+ rdoc_options:
121
+ - --main
122
+ - README.rdoc
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ requirements: []
144
+
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.10
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: Makes building SDKs for RESTful JSON services easy.
150
+ test_files: []
151
+