dialers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ module Dialers
2
+ class ShortCircuitsCollection
3
+ ResponseLambda = -> (_response) { nil }
4
+
5
+ def initialize
6
+ self.collection = []
7
+ end
8
+
9
+ def add(options)
10
+ collection << Dialers::ShortCircuit.new(options.fetch(:if), options.fetch(:do))
11
+ end
12
+
13
+ def search_for_stops(response)
14
+ short_circuit = collection.find do |item|
15
+ item.can_stop?(response)
16
+ end
17
+
18
+ if !short_circuit.nil?
19
+ short_circuit.stop(response)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_accessor :collection
26
+ end
27
+ end
@@ -0,0 +1,82 @@
1
+ module Dialers
2
+ # A wrapper over an HTTP status to answer some questions related to what a state means.
3
+ class Status
4
+ def initialize(status)
5
+ self.status = status
6
+ end
7
+
8
+ # @param code [Fixnum] The code to compare
9
+ # @return [Boolean] wether the status is the argument
10
+ def is?(code)
11
+ status.to_i == code.to_i
12
+ end
13
+
14
+ # @return [Boolean] wether the status is a 2xx one.
15
+ def success?
16
+ initial_letter == "2"
17
+ end
18
+
19
+ # @return [Boolean] wether the status is a 3xx one.
20
+ def redirect?
21
+ initial_letter == "3"
22
+ end
23
+
24
+ # @return [Boolean] wether the status is a 4xx one.
25
+ def client_error?
26
+ initial_letter == "4"
27
+ end
28
+
29
+ # @return [Boolean] wether the status is a 5xx one.
30
+ def server_error?
31
+ initial_letter == "5"
32
+ end
33
+
34
+ # @return [Boolean] wether the status is 200.
35
+ def ok?
36
+ is?(200)
37
+ end
38
+
39
+ # @return [Boolean] wether the status is 201.
40
+ def created?
41
+ is?(201)
42
+ end
43
+
44
+ # @return [Boolean] wether the status is 202.
45
+ def accepted?
46
+ is?(202)
47
+ end
48
+
49
+ # @return [Boolean] wether the status is 204.
50
+ def no_content?
51
+ is?(204)
52
+ end
53
+
54
+ # @return [Boolean] wether the status is 400.
55
+ def bad_request?
56
+ is?(400)
57
+ end
58
+
59
+ # @return [Boolean] wether the status is 401.
60
+ def unauthorized?
61
+ is?(401)
62
+ end
63
+
64
+ # @return [Boolean] wether the status is 404.
65
+ def not_found?
66
+ is?(404)
67
+ end
68
+
69
+ # @return [Boolean] wether the status is 405.
70
+ def method_not_allowed?
71
+ is?(405)
72
+ end
73
+
74
+ private
75
+
76
+ def initial_letter
77
+ @initial_letter ||= status.to_s[0]
78
+ end
79
+
80
+ attr_accessor :status
81
+ end
82
+ end
@@ -0,0 +1,84 @@
1
+ module Dialers
2
+ # This class provide some ways to transform a reponse into an object or a set of objects
3
+ # based on what objects the user of this class wants to provide.
4
+ class Transformable
5
+ def initialize(response)
6
+ self.response = response
7
+ self.raw_data = response.body
8
+ end
9
+
10
+ # Transforms a response into one object based on the response's body.
11
+ #
12
+ # If you pass a hash like this:
13
+ #
14
+ # { 200 => ProductCreated, 202 => ProductAccepted }
15
+ #
16
+ # the transformation will decide which object to create based on the status.
17
+ #
18
+ # It's important to note that the class must provide attribute writers for this method to be
19
+ # able to fill an object with the responses's body data.
20
+ #
21
+ # @param entity_class_or_decider [Hash, Class] A class or a hash of Fixnum => Class
22
+ # to decide with the status.
23
+ # @param root [String, nil] The root to use to get the object from the body.
24
+ #
25
+ # @return [Object] The result of the transformation.
26
+ def transform_to_one(entity_class_or_decider, root: nil)
27
+ object = root ? raw_data[root] : raw_data
28
+ transform_attributes_to_object(entity_class_or_decider, object)
29
+ end
30
+
31
+ # Transforms a response into many objects based on the response's body.
32
+ #
33
+ # @param entity_class_or_decider [Hash, Class] A class or a hash of Fixnum => Class
34
+ # to decide with the status.
35
+ # @param root [String, nil] The root to use to get an array from the body.
36
+ #
37
+ # @return [Array<Object>] The result of the transformation.
38
+ def transform_to_many(entity_class_or_decider, root: nil)
39
+ items = get_rooted_items(root)
40
+ unless items.is_a?(Array)
41
+ fail Dialers::ImpossibleTranformationError.new(response)
42
+ end
43
+ items.map { |item| transform_attributes_to_object(entity_class_or_decider, item) }
44
+ end
45
+
46
+ # Returns the raw body of the request.
47
+ # @return [Hash, Array] the raw body of the request.
48
+ def as_received
49
+ raw_data
50
+ end
51
+
52
+ private
53
+
54
+ attr_accessor :raw_data, :response
55
+
56
+ def transform_attributes_to_object(entity_class_or_decider, attributes)
57
+ entity_class = decide_entity_class(entity_class_or_decider)
58
+ if entity_class.nil?
59
+ fail Dialers::ResponseError.new(response)
60
+ else
61
+ Dialers::AssignAttributes.call(entity_class.new, attributes)
62
+ end
63
+ end
64
+
65
+ def decide_entity_class(entity_class_or_decider)
66
+ case entity_class_or_decider
67
+ when Class
68
+ entity_class_or_decider
69
+ when Hash
70
+ entity_class_or_decider[response.status]
71
+ end
72
+ end
73
+
74
+ def get_rooted_items(root)
75
+ if root.nil?
76
+ raw_data
77
+ elsif raw_data.is_a?(Array)
78
+ raw_data
79
+ else
80
+ raw_data[root]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module Dialers
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ module Dialers
2
+ # This class is just a convenience to this:
3
+ #
4
+ # class Anything
5
+ # def api_caller
6
+ # @api_caller ||= ApiCaller.new
7
+ # end
8
+ # end
9
+ #
10
+ # Instead, you can wrap it like this:
11
+ #
12
+ # class Anything < Dialers::Wrapper
13
+ # api_caller { ApiCaller.new }
14
+ # end
15
+ #
16
+ # The major reason of the existence of this class is to provide a place to add future
17
+ # improvements like automatic injection of callers within Rails based on conventions and
18
+ # methods and patterns that may arise in the future.
19
+ #
20
+ class Wrapper
21
+ # Defines the api caller instance to use on all wrappers.
22
+ #
23
+ # api_caller { ApiCaller.new }
24
+ #
25
+ def self.api_caller(&block)
26
+ define_method(:api_caller) do
27
+ @api_caller ||= block.call
28
+ end
29
+ end
30
+
31
+ # Returns the value of the `api_caller` as defined by the {api_caller} class method
32
+ attr_reader :api_caller
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,260 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dialers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - juliogarciag
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-conductivity
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '3.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '2.13'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '2.13'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: '4.6'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: '4.6'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-nc
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ~>
144
+ - !ruby/object:Gem::Version
145
+ version: '0.2'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ~>
151
+ - !ruby/object:Gem::Version
152
+ version: '0.2'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec-legacy_formatters
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ~>
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simple_oauth
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ~>
172
+ - !ruby/object:Gem::Version
173
+ version: '0.3'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ~>
179
+ - !ruby/object:Gem::Version
180
+ version: '0.3'
181
+ - !ruby/object:Gem::Dependency
182
+ name: patron
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ~>
186
+ - !ruby/object:Gem::Version
187
+ version: '0.4'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ~>
193
+ - !ruby/object:Gem::Version
194
+ version: '0.4'
195
+ description: Api Wrappers for Ruby
196
+ email:
197
+ - julioggonz@gmail.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - .gitignore
203
+ - .rbenv-vars-example
204
+ - .rspec
205
+ - .rubocop.yml
206
+ - .travis.yml
207
+ - Gemfile
208
+ - Guardfile
209
+ - LICENSE.txt
210
+ - NOTES.md
211
+ - README.md
212
+ - Rakefile
213
+ - bin/_guard-core
214
+ - bin/console
215
+ - bin/guard
216
+ - bin/rspec
217
+ - bin/setup
218
+ - dialers.gemspec
219
+ - examples/github/api.rb
220
+ - examples/github/api_caller.rb
221
+ - examples/github/usage.rb
222
+ - examples/twitter/api.rb
223
+ - examples/twitter/api_caller.rb
224
+ - examples/twitter/usage.rb
225
+ - lib/dialers.rb
226
+ - lib/dialers/assign_attributes.rb
227
+ - lib/dialers/caller.rb
228
+ - lib/dialers/errors.rb
229
+ - lib/dialers/request_options.rb
230
+ - lib/dialers/short_circuit.rb
231
+ - lib/dialers/short_circuits_collection.rb
232
+ - lib/dialers/status.rb
233
+ - lib/dialers/transformable.rb
234
+ - lib/dialers/version.rb
235
+ - lib/dialers/wrapper.rb
236
+ homepage: https://github.com/platanus/dialers
237
+ licenses:
238
+ - MIT
239
+ metadata: {}
240
+ post_install_message:
241
+ rdoc_options: []
242
+ require_paths:
243
+ - lib
244
+ required_ruby_version: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - '>='
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
249
+ required_rubygems_version: !ruby/object:Gem::Requirement
250
+ requirements:
251
+ - - '>='
252
+ - !ruby/object:Gem::Version
253
+ version: '0'
254
+ requirements: []
255
+ rubyforge_project:
256
+ rubygems_version: 2.4.7
257
+ signing_key:
258
+ specification_version: 4
259
+ summary: Api Wrappers for Ruby
260
+ test_files: []