Class: PostalCodesRubyClient::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token: nil, base_url: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/postal_codes_ruby_client/client.rb', line 11

def initialize(api_token: nil, base_url: nil)
  @api_token = api_token || PostalCodesRubyClient.configuration.api_token
  @base_url  = base_url  || PostalCodesRubyClient.configuration.base_url
  @timeout   = PostalCodesRubyClient.configuration.timeout

  @postal_codes = Resources::PostalCodes.new(self)
end

Instance Attribute Details

#postal_codesObject (readonly)

Returns the value of attribute postal_codes.



9
10
11
# File 'lib/postal_codes_ruby_client/client.rb', line 9

def postal_codes
  @postal_codes
end

Instance Method Details

#api_token=(token) ⇒ Object

Update the API token (e.g. after login or token regeneration).



20
21
22
# File 'lib/postal_codes_ruby_client/client.rb', line 20

def api_token=(token)
  @api_token = token
end

#get(path, params = {}) ⇒ Object

Perform a GET request.



25
26
27
28
29
# File 'lib/postal_codes_ruby_client/client.rb', line 25

def get(path, params = {})
  uri = build_uri(path, params)
  request = Net::HTTP::Get.new(uri)
  execute(uri, request)
end

#post(path, body = nil) ⇒ Object

Perform a POST request with a JSON body.



32
33
34
35
36
37
38
39
40
# File 'lib/postal_codes_ruby_client/client.rb', line 32

def post(path, body = nil)
  uri = build_uri(path)
  request = Net::HTTP::Post.new(uri)
  if body
    request.body = JSON.generate(body)
    request["Content-Type"] = "application/json"
  end
  execute(uri, request)
end