Class: Billplz::Model
- Inherits:
-
Object
- Object
- Billplz::Model
- Defined in:
- lib/billplz/model.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.api_url ⇒ Object
Returns the value of attribute api_url.
Instance Attribute Summary collapse
- #endpoint ⇒ Object
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #api_url ⇒ Object
-
#initialize(payload = {}) ⇒ Model
constructor
A new instance of Model.
- #parsed_json ⇒ Object
- #request(method, body) ⇒ Object
- #success? ⇒ Boolean
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_url ⇒ Object
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
#endpoint ⇒ Object
47 48 49 |
# File 'lib/billplz/model.rb', line 47 def endpoint URI.parse(@api_url) end |
#payload ⇒ Object
Returns the value of attribute payload.
4 5 6 |
# File 'lib/billplz/model.rb', line 4 def payload @payload end |
#response ⇒ Object
Returns the value of attribute response.
5 6 7 |
# File 'lib/billplz/model.rb', line 5 def response @response end |
Instance Method Details
#api_url ⇒ Object
43 44 45 |
# File 'lib/billplz/model.rb', line 43 def api_url self.class.api_url end |
#parsed_json ⇒ Object
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
51 52 53 |
# File 'lib/billplz/model.rb', line 51 def success? @response.is_a?(Net::HTTPOK) end |