Module: LinodeAPI::Helpers
- Included in:
- Raw
- Defined in:
- lib/linodeapi/helpers.rb
Overview
HTTP / API parsing helpers
Instance Method Summary collapse
- #clean(object) ⇒ Object
- #create_http_error(resp) ⇒ Object
- #error_check(resp) ⇒ Object
- #parse(resp) ⇒ Object
- #validate(method, mspec, given) ⇒ Object
Instance Method Details
#clean(object) ⇒ Object
28 29 30 |
# File 'lib/linodeapi/helpers.rb', line 28 def clean(object) OpenStruct.new(Hash[object.map { |k, v| [k.downcase.to_sym, v] }]) end |
#create_http_error(resp) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/linodeapi/helpers.rb', line 13 def create_http_error(resp) code = resp.code return nil if code == 200 delay = resp.headers['Retry-After'] return RetryableHTTPError.new(code, delay) if delay HTTPError.new(code) end |
#error_check(resp) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/linodeapi/helpers.rb', line 5 def error_check(resp) error = create_http_error(resp) raise(error) if error data = resp.parsed_response raise('Invalid API response received') if data.nil? parse data end |
#parse(resp) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/linodeapi/helpers.rb', line 21 def parse(resp) resp['ERRORARRAY'].reject! { |x| x['ERRORCODE'].zero? } raise(APIError, resp) unless resp['ERRORARRAY'].empty? data = resp['DATA'] data.is_a?(Hash) ? clean(data) : data.map { |x| clean x } end |
#validate(method, mspec, given) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/linodeapi/helpers.rb', line 32 def validate(method, mspec, given) mspec.each_with_object({}) do |(param, info), | if given.include? param [param] = VALIDATION_METHODS[info[:type]].call given[param] elsif info[:required] raise ArgumentError, "#{method} requires #{param}" end end end |