Class: Uk::HsCode::ApiRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/uk/hs_code/api_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, query_params = {}) ⇒ ApiRequest

Returns a new instance of ApiRequest.



13
14
15
16
# File 'lib/uk/hs_code/api_request.rb', line 13

def initialize(url, query_params = {})
  @url = url
  @query_params = query_params
end

Instance Attribute Details

#query_paramsObject (readonly)

Returns the value of attribute query_params.



11
12
13
# File 'lib/uk/hs_code/api_request.rb', line 11

def query_params
  @query_params
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/uk/hs_code/api_request.rb', line 11

def url
  @url
end

Instance Method Details

#getObject

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/uk/hs_code/api_request.rb', line 18

def get
  request = Net::HTTP::Get.new(parsed_uri)

  add_headers(request)

  http = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
  http.use_ssl = true if parsed_uri.scheme == "https"

  response = http.request(request)

  raise Uk::HsCode::Error, "Request failed with status: #{response.code} - #{response.message}" unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body, object_class: Record)&.data
end