Class: Zype::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/zype/client.rb

Defined Under Namespace

Classes: GenericError, NoApiKey, NoAppKey, NotFound, ServerError, Unauthorized, UnprocessableEntity

Constant Summary collapse

ERROR_TYPES =
{
  401 => Unauthorized,
  404 => NotFound,
  422 => UnprocessableEntity,
  500 => ServerError
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_method = 'api_key') ⇒ Client



32
33
34
35
# File 'lib/zype/client.rb', line 32

def initialize(auth_method='api_key')
  @headers = { "Content-Type" => "application/json" }.merge(authentication(auth_method))
  self.class.base_uri Zype.configuration.host
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/zype/client.rb', line 3

def headers
  @headers
end

Instance Method Details

#delete(path:, params: _) ⇒ Object



49
50
51
# File 'lib/zype/client.rb', line 49

def delete(path:, params: _)
  self.class.delete(path, { headers: headers })
end

#execute(method:, path:, params: {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zype/client.rb', line 53

def execute(method:, path:, params: {})
  if Zype.configuration.api_key.to_s.empty?
    raise NoApiKey if Zype.configuration.app_key.to_s.empty?
  end

  resp = self.send(method, path: path, params: params)
  if resp.success?
    resp['response'].nil? ? resp.parsed_response : resp['response']
  else
    error!(code: resp.code, message: resp['message'])
  end
end

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



37
38
39
# File 'lib/zype/client.rb', line 37

def get(path:, params: {})
  self.class.get(path, { query: params, headers: headers })
end

#post(path:, params: {}) ⇒ Object



41
42
43
# File 'lib/zype/client.rb', line 41

def post(path:, params: {})
  self.class.post(path, { query: params, headers: headers })
end

#put(path:, params: {}) ⇒ Object



45
46
47
# File 'lib/zype/client.rb', line 45

def put(path:, params: {})
  self.class.put(path, { query: params, headers: headers })
end