Class: PostcodeAnywhere::Response::ParseJson
- Inherits:
-
Faraday::Response::Middleware
- Object
- Faraday::Response::Middleware
- PostcodeAnywhere::Response::ParseJson
- Defined in:
- lib/postcode_anywhere/response/parse_json.rb
Constant Summary collapse
- WHITESPACE_REGEX =
/\A^\s*$\z/
Instance Method Summary collapse
- #breakout_items(response_body) ⇒ Object
- #convert_boolean(value) ⇒ Object
- #convert_hash_keys(value) ⇒ Object
- #error_klass_for(code) ⇒ Object
- #on_complete(response) ⇒ Object
- #parse(body) ⇒ Object
- #raise_errors_in(response_body) ⇒ Object
- #to_snake_case(string) ⇒ Object
- #underscore_key(k) ⇒ Object
- #unparsable_status_codes ⇒ Object
Instance Method Details
#breakout_items(response_body) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 56 def breakout_items(response_body) if response_body.class == Hash && response_body.keys.include?(:items) return response_body[:items] end response_body end |
#convert_boolean(value) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 40 def convert_boolean(value) return nil unless value return value unless value.class == String return true if (value.downcase == 'true') return false if (value.downcase == 'false') value end |
#convert_hash_keys(value) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 29 def convert_hash_keys(value) case value when Array value.map { |v| convert_hash_keys(v) } when Hash Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }] else convert_boolean value end end |
#error_klass_for(code) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 79 def error_klass_for(code) klass = PostcodeAnywhere::Error.postcode_anywhere_errors[code] if !klass if code > 1000 klass = PostcodeAnywhere::Error::ServiceSpecificError else PostcodeAnywhere::Error::UnknownError end end klass end |
#on_complete(response) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 48 def on_complete(response) response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status) raise_errors_in response.body response.body = breakout_items response.body end |
#parse(body) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 8 def parse(body) case body when WHITESPACE_REGEX, nil nil else convert_hash_keys JSON.parse(body) end end |
#raise_errors_in(response_body) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 63 def raise_errors_in(response_body) ( first_response = response_body if response_body.class == Array first_response = response_body.first end if first_response.class == Hash if first_response.keys.include? :error code = first_response[:error].to_i klass = error_klass_for code fail(klass.from_response(first_response)) end end ) if response_body end |
#to_snake_case(string) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 17 def to_snake_case(string) string.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). tr('-', '_'). downcase end |
#underscore_key(k) ⇒ Object
25 26 27 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 25 def underscore_key(k) to_snake_case(k.to_s).to_sym end |
#unparsable_status_codes ⇒ Object
91 92 93 |
# File 'lib/postcode_anywhere/response/parse_json.rb', line 91 def unparsable_status_codes [204, 301, 302, 304, 400] end |