Class: Uk::HsCode::ApiRequest
- Inherits:
-
Object
- Object
- Uk::HsCode::ApiRequest
- Defined in:
- lib/uk/hs_code/api_request.rb
Instance Attribute Summary collapse
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(url, query_params = {}) ⇒ ApiRequest
constructor
A new instance of ApiRequest.
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_params ⇒ Object (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 |
#url ⇒ Object (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
#get ⇒ Object
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.}" unless response.is_a?(Net::HTTPSuccess) JSON.parse(response.body, object_class: Record)&.data end |