Class: Ptilinopus::API

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

Constant Summary collapse

DEFAULT_HEADER =
{"Content-Type" => "application/json"}
API_PATH =
'/api/v1/'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ API

Returns a new instance of API.



14
15
16
# File 'lib/ptilinopus.rb', line 14

def initialize(api_key = nil)
  @api_key = api_key || self.class.api_key
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/ptilinopus.rb', line 10

def api_key
  @api_key
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



51
52
53
# File 'lib/ptilinopus.rb', line 51

def method_missing(sym, *args, &block)
  new(self.api_key).send(sym, *args, &block)
end

Instance Method Details

#call(type, method, params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ptilinopus.rb', line 18

def call(type, method, params = {})
  ensure_api_key(params)

  params = params.merge({apiKey: @api_key})
  response = self.class.send(type, API_PATH + method, body: params.to_json, headers: DEFAULT_HEADER)

  if response.code != 200
    case response.code
    when 400
      raise MailerliteInvalidMethodError.new
    when 401
      raise MailerliteInvalidApiKeyError.new
    when 404
      raise MailerliteBadRequestItemError.new
    else
      raise MailerliteServerError.new
    end
  end

  return response.body
end