Class: OpenMeteo::ResponseWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/open_meteo/response_wrapper.rb

Overview

Wrap the JSON body response from the OpenMeteo request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: OpenMeteo::Client::Config.new) ⇒ ResponseWrapper

Returns a new instance of ResponseWrapper.



8
9
10
# File 'lib/open_meteo/response_wrapper.rb', line 8

def initialize(config: OpenMeteo::Client::Config.new)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/open_meteo/response_wrapper.rb', line 6

def config
  @config
end

Instance Method Details

#wrap(response, entity:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/open_meteo/response_wrapper.rb', line 12

def wrap(response, entity:)
  raise OpenMeteo::Errors::ResponseError, "Empty body" if response.body.nil?

  json_body = JSON.parse(response.body)
  check_for_error(response, json_body)

  entity.new(json_body)
rescue JSON::ParserError
  raise OpenMeteo::Errors::ResponseError, "Unable to parse the response body: #{response.body}"
end