linodeapi 2.0.0 → 2.0.1

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: 386eb467c95101a915f20e0859bf356bb8c765ed
4
- data.tar.gz: 45437aba6937cb0e0a03de655948479d5938241d
3
+ metadata.gz: 774a92f57a045e5df6250bd6654fc773b7e7c33f
4
+ data.tar.gz: efb03272ff013266e9a43ed270eeafa03fb5b2a2
5
5
  SHA512:
6
- metadata.gz: 18391de6e8204d94eef64f38b28a6f5fba0e0c3f37bb948029237cdeebfbbddcd00df70e107c88450c14e6700f2c1cb9b1c257d968cd5d6b87586dd27cfff1ed
7
- data.tar.gz: a01e3df48784a83b12a8cabf96e0608505c825d56a7fd4a81a7ebd330fd41d18a26ec1d7134df3b69a23cc0191c0d37e89f0e5b294c93b3f34e35df9618569f5
6
+ metadata.gz: cced09324ac708e26276287c6a3e93fd4afd95d95dc289e33ba0e0b26941544bca779fe5118f403e5a9d6226dab9c108cdafa59a3cc2df92deb6167b089ad6a0
7
+ data.tar.gz: 81546c62f9d6e28601dd3e6526affffc7bbe276f2cbab82ed6a96c5d1bd53a4187b3529fea67690c45797af68552e14a3209c5300c704b91e0c34169a0d4371f
@@ -1,3 +1,14 @@
1
+ # 2.0.1 / 2017-10-03
2
+
3
+ * [ENHANCEMENT] Broke out some of the parsing code into linodeapi/helpers to make it easier to read
4
+ * [FEATURE] Support using multiple LinodeAPI::Raw instances with different endpoint URIs at the same time
5
+
6
+ # 2.0.0 / 2017-10-03
7
+
8
+ * [FEATURE] LinodeAPI::Retryable class that automatically retries supported errors
9
+ * [ENHANCEMENT] Cleaned up LinodeAPI::Raw's parameter handling
10
+ * [FEATURE] Added unique error codes to lib/linodeapi/errors.rb for API/HTTP errors
11
+
1
12
  # 1.0.1 / 2016-07-01
2
13
 
3
14
  * [FEATURE] Expose LinodeAPI.spec_version
@@ -76,5 +76,6 @@ module LinodeAPI
76
76
  end
77
77
 
78
78
  require 'linodeapi/errors'
79
+ require 'linodeapi/helpers'
79
80
  require 'linodeapi/raw'
80
81
  require 'linodeapi/retryable'
@@ -0,0 +1,42 @@
1
+ module LinodeAPI
2
+ ##
3
+ # HTTP / API parsing helpers
4
+ module Helpers
5
+ def error_check(resp)
6
+ error = create_http_error(resp)
7
+ raise(error) if error
8
+ data = resp.parsed_response
9
+ raise('Invalid API response received') if data.nil?
10
+ parse data
11
+ end
12
+
13
+ def create_http_error(resp)
14
+ code = resp.code
15
+ return nil if code == 200
16
+ delay = resp.headers['Retry-After']
17
+ return RetryableHTTPError.new(code, delay) if delay
18
+ HTTPError.new(code)
19
+ end
20
+
21
+ def parse(resp)
22
+ resp['ERRORARRAY'].reject! { |x| x['ERRORCODE'].zero? }
23
+ raise(APIError, resp) unless resp['ERRORARRAY'].empty?
24
+ data = resp['DATA']
25
+ data.is_a?(Hash) ? clean(data) : data.map { |x| clean x }
26
+ end
27
+
28
+ def clean(object)
29
+ OpenStruct.new(Hash[object.map { |k, v| [k.downcase.to_sym, v] }])
30
+ end
31
+
32
+ def validate(method, mspec, given)
33
+ mspec.each_with_object({}) do |(param, info), options|
34
+ if given.include? param
35
+ options[param] = VALIDATION_METHODS[info[:type]].call given[param]
36
+ elsif info[:required]
37
+ raise ArgumentError, "#{method} requires #{param}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -8,10 +8,11 @@ module LinodeAPI
8
8
  # Raw API object
9
9
  class Raw
10
10
  include HTTParty
11
+ extend Helpers
11
12
 
12
13
  def initialize(params = {})
13
14
  @options = params
14
- self.class.base_uri params.fetch(:endpoint, DEFAULT_ENDPOINT)
15
+ @options[:endpoint] ||= DEFAULT_ENDPOINT
15
16
  @options[:names] ||= []
16
17
  @options[:spec] ||= LinodeAPI.spec
17
18
  @options[:apikey] ||= authenticate(params)
@@ -38,6 +39,10 @@ module LinodeAPI
38
39
  @apikey ||= @options[:apikey]
39
40
  end
40
41
 
42
+ def endpoint
43
+ @endpoint ||= @options[:endpoint]
44
+ end
45
+
41
46
  private
42
47
 
43
48
  def authenticate(params = {})
@@ -75,51 +80,19 @@ module LinodeAPI
75
80
  end
76
81
 
77
82
  def call(method, params = {})
83
+ options = build_call_body(method, params)
84
+ self.class.error_check self.class.post(
85
+ '', body: options, base_uri: endpoint
86
+ )
87
+ end
88
+
89
+ def build_call_body(method, params)
78
90
  mspec = spec[:subs][method]
79
- method = (names + [method.to_s]).join '.'
91
+ mname = (names + [method.to_s]).join '.'
80
92
  options = self.class.validate method, mspec[:params], params
81
93
  options[:api_key] = apikey
82
- options[:api_action] = method
83
- error_check self.class.post('', body: options)
84
- end
85
-
86
- def error_check(resp)
87
- error = create_http_error(resp)
88
- raise(error) if error
89
- data = resp.parsed_response
90
- raise('Invalid API response received') if data.nil?
91
- self.class.parse data
92
- end
93
-
94
- def create_http_error(resp)
95
- code = resp.code
96
- return nil if code == 200
97
- delay = resp.headers['Retry-After']
98
- return RetryableHTTPError.new(code, delay) if delay
99
- HTTPError.new(code)
100
- end
101
-
102
- class << self
103
- def parse(resp)
104
- resp['ERRORARRAY'].reject! { |x| x['ERRORCODE'].zero? }
105
- raise(APIError, resp) unless resp['ERRORARRAY'].empty?
106
- data = resp['DATA']
107
- data.is_a?(Hash) ? clean(data) : data.map { |x| clean x }
108
- end
109
-
110
- def clean(object)
111
- OpenStruct.new(Hash[object.map { |k, v| [k.downcase.to_sym, v] }])
112
- end
113
-
114
- def validate(method, mspec, given)
115
- mspec.each_with_object({}) do |(param, info), options|
116
- if given.include? param
117
- options[param] = VALIDATION_METHODS[info[:type]].call given[param]
118
- elsif info[:required]
119
- raise ArgumentError, "#{method} requires #{param}"
120
- end
121
- end
122
- end
94
+ options[:api_action] = mname
95
+ options
123
96
  end
124
97
  end
125
98
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'linodeapi'
3
- s.version = '2.0.0'
3
+ s.version = '2.0.1'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
 
6
6
  s.summary = 'Linode API wrapper'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linodeapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Aker
@@ -158,6 +158,7 @@ files:
158
158
  - dev/version
159
159
  - lib/linodeapi.rb
160
160
  - lib/linodeapi/errors.rb
161
+ - lib/linodeapi/helpers.rb
161
162
  - lib/linodeapi/raw.rb
162
163
  - lib/linodeapi/retryable.rb
163
164
  - linodeapi.gemspec
@@ -190,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
191
  version: '0'
191
192
  requirements: []
192
193
  rubyforge_project:
193
- rubygems_version: 2.6.13
194
+ rubygems_version: 2.6.11
194
195
  signing_key:
195
196
  specification_version: 4
196
197
  summary: Linode API wrapper