Class: Zype::Client

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

Direct Known Subclasses

ApiClient, PlayerClient

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

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



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

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

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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zype/client.rb', line 48

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



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

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

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



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

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

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



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

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