Class: NewgisticsApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/newgistics_api/client.rb

Direct Known Subclasses

Shipment, Tracking

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Instance Method Details

#make_request(method, path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/newgistics_api/client.rb', line 3

def make_request(method, path)
  uri = URI.parse("#{host}#{path}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP.const_get(method.to_s.downcase.capitalize).new(uri.request_uri)
  request.add_field("X-API-Key", api_key)
  request["Accept"] = "application/json"

  if block_given?
    block_output = yield

    raise TypeError, "The output of the block must be a Hash" unless block_output.is_a?(Hash)

    request.body = block_output.to_json
    request["Content-Type"] = "application/json"
  end

  response = http.request(request)

  Response.new(self.class, response)
end