Class: Cufinder::BaseApiClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cufinder_ruby/base_api_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, timeout: 30, max_retries: 3) ⇒ BaseApiClient

Returns a new instance of BaseApiClient.



11
12
13
14
15
16
17
18
19
# File 'lib/cufinder_ruby/base_api_client.rb', line 11

def initialize(api_key:, timeout: 30, max_retries: 3)
  @api_key = api_key
  @base_url = "https://api.cufinder.io/v2"
  @timeout = timeout
  @max_retries = max_retries
  
  self.class.base_uri @base_url
  self.class.headers "User-Agent" => "cufinder-ruby/#{Cufinder::VERSION}"
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/cufinder_ruby/base_api_client.rb', line 9

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/cufinder_ruby/base_api_client.rb', line 9

def base_url
  @base_url
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



9
10
11
# File 'lib/cufinder_ruby/base_api_client.rb', line 9

def max_retries
  @max_retries
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



9
10
11
# File 'lib/cufinder_ruby/base_api_client.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#post(endpoint, data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cufinder_ruby/base_api_client.rb', line 21

def post(endpoint, data)
  response = self.class.post(
    endpoint,
    body: form_encode(data),
    headers: {
      "x-api-key" => api_key,
      "Content-Type" => "application/x-www-form-urlencoded"
    },
    timeout: @timeout
  )
  
  handle_response(response)
end