Class: Billplz::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/billplz/model.rb

Direct Known Subclasses

Bill, Collection

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ Model

Returns a new instance of Model.



7
8
9
10
# File 'lib/billplz/model.rb', line 7

def initialize(payload={})
  @api_url  = self.api_url
  @payload  = payload
end

Class Attribute Details

.api_urlObject

Returns the value of attribute api_url.



40
41
42
# File 'lib/billplz/model.rb', line 40

def api_url
  @api_url
end

Instance Attribute Details

#endpointObject



47
48
49
# File 'lib/billplz/model.rb', line 47

def endpoint
  URI.parse(@api_url)
end

#payloadObject

Returns the value of attribute payload.



4
5
6
# File 'lib/billplz/model.rb', line 4

def payload
  @payload
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/billplz/model.rb', line 5

def response
  @response
end

Instance Method Details

#api_urlObject



43
44
45
# File 'lib/billplz/model.rb', line 43

def api_url
  self.class.api_url
end

#parsed_jsonObject



55
56
57
# File 'lib/billplz/model.rb', line 55

def parsed_json
  JSON.parse(@response.body)
end

#request(method, body) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/billplz/model.rb', line 12

def request(method, body)
  headers = {
    "Authorization" => "Basic " + Base64.encode64(Billplz.configuration.api_key + ":").strip,
    "Content-Type"  => "application/json",
    "Accept"        => "application/json"
  }

  @response = case method
  when :get
    raise ArgumentError, "GET requests do not support a request body" if body
    http.get(endpoint.request_uri, headers)
  when :post
    http.post(endpoint.request_uri, body.to_json, headers)
  when :put
    http.put(endpoint.request_uri, body.to_json, headers)
  when :patch
    http.patch(endpoint.request_uri, body, headers)
  when :delete
    raise ArgumentError, "DELETE requests do not support a request body" if body
    http.delete(endpoint.request_uri, headers)
  else
    raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
  end

  @response
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  @response.is_a?(Net::HTTPOK)
end