Module: Latitude::API::JSONAPI
- Defined in:
- lib/latitude/api/json_api.rb
Constant Summary collapse
- MEDIA_TYPE =
"application/vnd.api+json"
Class Method Summary collapse
- .encode(type:, attributes: nil, id: nil, relationships: nil) ⇒ Object
- .envelope(type:, attributes: nil, id: nil, relationships: nil) ⇒ Object
- .extract_errors(parsed) ⇒ Object
- .parse(body) ⇒ Object
Class Method Details
.encode(type:, attributes: nil, id: nil, relationships: nil) ⇒ Object
18 19 20 |
# File 'lib/latitude/api/json_api.rb', line 18 def encode(type:, attributes: nil, id: nil, relationships: nil) JSON.generate(envelope(type: type, attributes: attributes, id: id, relationships: relationships)) end |
.envelope(type:, attributes: nil, id: nil, relationships: nil) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/latitude/api/json_api.rb', line 10 def envelope(type:, attributes: nil, id: nil, relationships: nil) data = { "type" => type.to_s } data["id"] = id.to_s if id data["attributes"] = Util.stringify_keys(attributes) if attributes data["relationships"] = Util.stringify_keys(relationships) if relationships { "data" => data } end |
.extract_errors(parsed) ⇒ Object
33 34 35 36 37 |
# File 'lib/latitude/api/json_api.rb', line 33 def extract_errors(parsed) return [] unless parsed.is_a?(Hash) Array(parsed["errors"]) end |
.parse(body) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/latitude/api/json_api.rb', line 22 def parse(body) return nil if body.nil? || body.empty? JSON.parse(body) rescue JSON::ParserError => e raise APIError.new( "Invalid JSON response from server: #{e.}", http_body: body, ) end |