apidiesel 0.9 → 0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe3d16b8a91f4bd35deafce8149466d3815f4990
4
- data.tar.gz: 20f22493471da55fb45c76931ec9a2feb24a8c46
3
+ metadata.gz: abd698dc7975148cd9d24f1a365abb1e21dcf873
4
+ data.tar.gz: 28f247075558965d6c68c59a3fa0afe9fc899ea4
5
5
  SHA512:
6
- metadata.gz: 9bc4ad1cacf4576f0eaa195c7bf52553bb80cb8d5b897a2dc6c93c027dac01295f7665da3e15e2706902256708aff69935335f7f7eca76b50a218e2bbb5744ea
7
- data.tar.gz: 86d14c91e1e8ff1b8db828ce61cbc7c38aa60d02672eb6e7035fc4d372b66789c5b859f278d22514e12d6f78e6e54fc3d1d7aeb05ac16866bbe73967e9ac6f7d
6
+ metadata.gz: 9dd5aac9aa74b83ef5a14bf1537ae97893db4f768b099464fefe0af1094da9246691e7c695543b3dfd60453f53f0e0902a23d997c9df16ffb92a2d640ca70200
7
+ data.tar.gz: 6f761f94ba751451f2f2fa42243bd0a54325e97f7bff55b362545f7f3f3830953df807321a0cd99dafee75b1f673c0659ea83e6d83d0b694a23f04e4275f8714
@@ -7,6 +7,8 @@ module Apidiesel
7
7
  # accessors for class instance variables
8
8
  # (class-level variables, not shared with subclasses)
9
9
  class << self
10
+ include Handlers
11
+
10
12
  attr_reader :url_args
11
13
 
12
14
  # Hash for storing validation closures. These closures are called with the request
@@ -53,7 +55,9 @@ module Apidiesel
53
55
  def url(value = nil, **args)
54
56
  return @url unless value || args.any?
55
57
 
56
- if value
58
+ if value && value.is_a?(Proc)
59
+ @url = value
60
+ elsif value
57
61
  @url = URI.parse(value)
58
62
  else
59
63
  @url_args = args
@@ -124,10 +128,21 @@ module Apidiesel
124
128
  self.class.endpoint
125
129
  end
126
130
 
131
+ def base_url
132
+ if self.class.url.nil? || self.class.url.is_a?(Proc)
133
+ @api.class.url.dup
134
+ else
135
+ self.class.url.dup
136
+ end
137
+ end
138
+
127
139
  def url
128
- url = self.class.url || @api.class.url
140
+ if self.class.url.is_a?(Proc)
141
+ url = self.class.url
142
+
143
+ elsif self.class.url_args
144
+ url = base_url
129
145
 
130
- if self.class.url_args
131
146
  self.class.url_args.each do |key, value|
132
147
  url.send("#{key}=", value)
133
148
  end
data/lib/apidiesel/api.rb CHANGED
@@ -26,17 +26,7 @@ module Apidiesel
26
26
  #
27
27
  class Api
28
28
  class << self
29
- def request_handlers
30
- @request_handlers ||= []
31
- end
32
-
33
- def response_handlers
34
- @response_handlers ||= []
35
- end
36
-
37
- def exception_handlers
38
- @exception_handlers ||= []
39
- end
29
+ include Handlers
40
30
 
41
31
  def config(key = nil, value = nil)
42
32
  @config ||= {}
@@ -74,18 +64,18 @@ module Apidiesel
74
64
  end
75
65
  end
76
66
 
77
- # Registers a handler for requests and/or responses
67
+ # Combined getter/setter for the HTTP Basic Auth
78
68
  #
79
- # @param [Class] klass
80
-
81
- def use(klass, *args, &block)
82
- request_handler = "#{klass.name}::RequestHandler".safe_constantize
83
- response_handler = "#{klass.name}::ResponseHandler".safe_constantize
84
- exception_handler = "#{klass.name}::ExceptionHandler".safe_constantize
85
-
86
- request_handlers << request_handler.new(*args, &block) if request_handler
87
- response_handlers << response_handler.new(*args, &block) if response_handler
88
- exception_handlers << exception_handler.new(*args, &block) if exception_handler
69
+ # Falls back to the Api setting if blank.
70
+ #
71
+ # @param [String] value
72
+ def http_basic_auth(username = nil, password = nil)
73
+ if username && password
74
+ config[:http_basic_username] = username
75
+ config[:http_basic_password] = password
76
+ else
77
+ return config[:http_basic_username], config[:http_basic_password]
78
+ end
89
79
  end
90
80
 
91
81
  # Registers the individual API endpoint definitions
@@ -120,7 +110,13 @@ module Apidiesel
120
110
  def execute_request(action_klass, *args)
121
111
  request = action_klass.new(self).build_request(*args)
122
112
 
123
- self.class.request_handlers.each do |handler|
113
+ request_handlers =
114
+ action_klass.request_handlers.any? ? action_klass.request_handlers : self.class.request_handlers
115
+
116
+ response_handlers =
117
+ action_klass.response_handlers.any? ? action_klass.response_handlers : self.class.response_handlers
118
+
119
+ request_handlers.each do |handler|
124
120
  request = handler.run(request, @config)
125
121
  break if request.response_body.present?
126
122
  end
@@ -129,12 +125,12 @@ module Apidiesel
129
125
  raise "All request handlers failed to deliver a response"
130
126
  end
131
127
 
132
- self.class.response_handlers.each do |handler|
128
+ response_handlers.each do |handler|
133
129
  request = handler.run(request, @config)
134
130
  end
135
131
 
136
132
  response_handler_klasses =
137
- self.class.response_handlers.collect { |handler| handler.class.name.to_s.demodulize }
133
+ response_handlers.collect { |handler| handler.class.name.split('::')[-2] }
138
134
 
139
135
  # Execute the actions' `responds_with` block automatically, unless
140
136
  # the handler has been included manually in order to control the
@@ -145,7 +141,10 @@ module Apidiesel
145
141
 
146
142
  request
147
143
  rescue => e
148
- self.class.exception_handlers.each do |handler|
144
+ exception_handlers =
145
+ action_klass.exception_handlers.any? ? action_klass.exception_handlers : self.class.exception_handlers
146
+
147
+ exception_handlers.each do |handler|
149
148
  request = handler.run(e, request, @config)
150
149
  end
151
150
 
data/lib/apidiesel/dsl.rb CHANGED
@@ -369,7 +369,7 @@ module Apidiesel
369
369
  # integer :order_id
370
370
  # datetime :ordered_at
371
371
  #
372
- # an_array_of :products do
372
+ # array :products do
373
373
  # string :name
374
374
  # integer :product_id
375
375
  # end
@@ -390,7 +390,7 @@ module Apidiesel
390
390
  #
391
391
  # @example
392
392
  # expects do
393
- # an_array_of do
393
+ # array do
394
394
  # string :name
395
395
  # integer :order_id
396
396
  # end
@@ -410,7 +410,7 @@ module Apidiesel
410
410
  args = normalize_arguments(args, kargs)
411
411
 
412
412
  response_formatters << lambda do |data, processed_data|
413
- data = get_value(data, args[:at])
413
+ data = get_value(data, args[:at]) if args[:at]
414
414
 
415
415
  return processed_data unless data.present?
416
416
 
@@ -438,9 +438,12 @@ module Apidiesel
438
438
  result
439
439
  end
440
440
 
441
- processed_data[ args[:as] ] = array_of_hashes.compact
442
-
443
- processed_data
441
+ if args[:as]
442
+ processed_data[ args[:as] ] = array_of_hashes.compact
443
+ processed_data
444
+ else
445
+ array_of_hashes.compact
446
+ end
444
447
  end
445
448
  end
446
449
 
@@ -11,9 +11,13 @@ module Apidiesel
11
11
  # instance, as given to the handlers #run method
12
12
  #
13
13
  def execute_request(request:, payload:, api_config:)
14
- http_request = HTTPI::Request.new(request.action.url.try(:to_s))
14
+ http_request = HTTPI::Request.new(request.url.try(:to_s))
15
15
  http_request.body = payload
16
16
 
17
+ if api_config[:http_basic_username] && api_config[:http_basic_password]
18
+ http_request.auth.basic(api_config[:http_basic_username], api_config[:http_basic_password])
19
+ end
20
+
17
21
  http_request.auth.ssl.verify_mode = api_config[:ssl_verify_mode] || :peer
18
22
  http_request.open_timeout = api_config[:timeout] || 30
19
23
  http_request.read_timeout = api_config[:timeout] || 30
@@ -28,7 +32,7 @@ module Apidiesel
28
32
  end
29
33
 
30
34
  if response.error?
31
- raise RequestError.new("#{request.action.http_method} #{request.action.url} returned #{response.code}", request)
35
+ raise RequestError.new("#{request.action.http_method} #{request.url} returned #{response.code}", request)
32
36
  end
33
37
 
34
38
  request
@@ -0,0 +1,27 @@
1
+ module Apidiesel
2
+ module Handlers
3
+ module JSON
4
+ class RequestHandler
5
+ include HttpRequestHelper
6
+
7
+ def run(request, api_config)
8
+ action = request.action
9
+
10
+ payload = ::JSON.dump(request.parameters)
11
+
12
+ request.metadata[:started_at] = DateTime.now
13
+
14
+ execute_request(request: request,
15
+ payload: payload,
16
+ api_config: api_config)
17
+
18
+ request.metadata[:finished_at] = DateTime.now
19
+
20
+ request.response_body = ::JSON.parse(request.http_response.body)
21
+
22
+ request
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module Apidiesel
2
+ module Handlers
3
+ def request_handlers
4
+ @request_handlers ||= []
5
+ end
6
+
7
+ def response_handlers
8
+ @response_handlers ||= []
9
+ end
10
+
11
+ def exception_handlers
12
+ @exception_handlers ||= []
13
+ end
14
+
15
+ # Registers a handler for requests, responses and/or exceptions
16
+ #
17
+ # @param [Class] klass
18
+ def use(klass, *args, &block)
19
+ request_handler = "#{klass.name}::RequestHandler".safe_constantize
20
+ response_handler = "#{klass.name}::ResponseHandler".safe_constantize
21
+ exception_handler = "#{klass.name}::ExceptionHandler".safe_constantize
22
+
23
+ request_handlers << request_handler.new(*args, &block) if request_handler
24
+ response_handlers << response_handler.new(*args, &block) if response_handler
25
+ exception_handlers << exception_handler.new(*args, &block) if exception_handler
26
+ end
27
+ end
28
+ end
@@ -13,6 +13,16 @@ module Apidiesel
13
13
  @metadata = metadata
14
14
  end
15
15
 
16
+ def url
17
+ @url ||=
18
+ case action.url
19
+ when Proc
20
+ action.url.call(action.base_url, self)
21
+ when URI
22
+ action.url
23
+ end
24
+ end
25
+
16
26
  def response_body
17
27
  @response_body || http_response.try(:body)
18
28
  end
@@ -31,7 +41,7 @@ module Apidiesel
31
41
  [
32
42
  "Apidiesel::Request",
33
43
  action.http_method.to_s.upcase,
34
- action.url.try(:to_s),
44
+ url.try(:to_s),
35
45
  action.endpoint,
36
46
  parameters.collect { |key, value| "#{key}: #{value}"}.join(',')
37
47
  ].join(' ')
@@ -1,3 +1,3 @@
1
1
  module Apidiesel
2
- VERSION = "0.9"
2
+ VERSION = "0.10"
3
3
  end
data/lib/apidiesel.rb CHANGED
@@ -6,10 +6,12 @@ HTTPI.log = false
6
6
  require 'apidiesel/version'
7
7
 
8
8
  require 'apidiesel/errors'
9
+ require 'apidiesel/handlers'
9
10
  require 'apidiesel/api'
10
11
  require 'apidiesel/dsl'
11
12
  require 'apidiesel/request'
12
13
  require 'apidiesel/action'
13
14
  require 'apidiesel/handlers/action_response_processor'
14
15
  require 'apidiesel/handlers/http_request_helper'
16
+ require 'apidiesel/handlers/json'
15
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apidiesel
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.9'
4
+ version: '0.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan-Christian Foeh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-28 00:00:00.000000000 Z
11
+ date: 2016-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -88,8 +88,10 @@ files:
88
88
  - lib/apidiesel/api.rb
89
89
  - lib/apidiesel/dsl.rb
90
90
  - lib/apidiesel/errors.rb
91
+ - lib/apidiesel/handlers.rb
91
92
  - lib/apidiesel/handlers/action_response_processor.rb
92
93
  - lib/apidiesel/handlers/http_request_helper.rb
94
+ - lib/apidiesel/handlers/json.rb
93
95
  - lib/apidiesel/handlers/mock_response.rb
94
96
  - lib/apidiesel/request.rb
95
97
  - lib/apidiesel/version.rb