Class: PapierkramApi::V1::Endpoints::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/papierkram_api/v1/endpoints/base.rb

Overview

This class is the base class for all the API calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



10
11
12
13
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 10

def initialize(client)
  @client = client
  @url_api_path = '/api/v1'
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 8

def client
  @client
end

#url_api_pathObject (readonly)

Returns the value of attribute url_api_path.



8
9
10
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 8

def url_api_path
  @url_api_path
end

Instance Method Details

#http_delete(url, params = {}, headers = {}) ⇒ Object



42
43
44
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 42

def http_delete(url, params = {}, headers = {})
  call_wrapper!(:delete, url, params, headers)
end

#http_get(url, params = {}, headers = {}) ⇒ Object



25
26
27
28
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 25

def http_get(url, params = {}, headers = {})
  validate_get!(params)
  call_wrapper!(:get, url, params, headers)
end

#http_patch(url, params = {}, headers = {}) ⇒ Object



38
39
40
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 38

def http_patch(url, params = {}, headers = {})
  call_wrapper!(:patch, url, params, headers)
end

#http_post(url, params = {}, headers = {}) ⇒ Object



30
31
32
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 30

def http_post(url, params = {}, headers = {})
  call_wrapper!(:post, url, params, headers)
end

#http_put(url, params = {}, headers = {}) ⇒ Object



34
35
36
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 34

def http_put(url, params = {}, headers = {})
  call_wrapper!(:put, url, params, headers)
end

#remaining_quota(response) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
# File 'lib/papierkram_api/v1/endpoints/base.rb', line 15

def remaining_quota(response)
  return response.headers['x-remaining-quota'].to_i if response.is_a?(Faraday::Response)
  raise ArgumentError, 'Invalid response object' unless response.is_a?(Hash)

  quota = response.dig('headers', 'x-remaining-quota') || response['x-remaining-quota']
  return Integer(quota) if quota

  raise ArgumentError, "No remaining quota found in response: #{response}"
end