Class: Zype::Client

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

Direct Known Subclasses

ApiClient, LoginClient, 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



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

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

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



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

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 = 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



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

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

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



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

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

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



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

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