Class: Strike::Charge
- Inherits:
-
Object
- Object
- Strike::Charge
- Defined in:
- lib/strike/charge.rb
Constant Summary collapse
- PATH =
"/api/v1/charges".freeze
Instance Attribute Summary collapse
-
#amount ⇒ Object
String Unique identifier of the charge.
-
#amount_satoshi ⇒ Object
String Unique identifier of the charge.
-
#code ⇒ Object
String Unique identifier of the charge.
-
#created ⇒ Object
String Unique identifier of the charge.
-
#currency ⇒ Object
String Unique identifier of the charge.
-
#customer_id ⇒ Object
String Unique identifier of the charge.
-
#description ⇒ Object
String Unique identifier of the charge.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#id ⇒ Object
String Unique identifier of the charge.
-
#message ⇒ Object
String Unique identifier of the charge.
-
#paid ⇒ Object
String Unique identifier of the charge.
-
#payment_request ⇒ Object
String Unique identifier of the charge.
-
#updated ⇒ Object
String Unique identifier of the charge.
Class Method Summary collapse
-
.all(page: 1, per_page: 30) ⇒ Object
Fetch a charge by id.
- .api_url ⇒ Object
- .conn ⇒ Object
-
.create(amount:, currency:, description:, customer_id:) ⇒ Object
Create a new Strike charge.
-
.find(id) ⇒ Object
Fetch a charge by id.
- .handle_errors(response) ⇒ Object
Instance Method Summary collapse
- #fail? ⇒ Boolean
-
#initialize(options) ⇒ Charge
constructor
A new instance of Charge.
- #success? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Charge
Returns a new instance of Charge.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/strike/charge.rb', line 31 def initialize() self.id = ["id"] self.amount = ["amount"] self.amount_satoshi = ["amount_satoshi"] self.currency = ["currency"] self.description = ["description"] self.customer_id = ["customer_id"] self.payment_request = ["payment_request"] self.paid = ["paid"] self.created = ["created"] self.updated = ["updated"] self.code = ["code"] self. = ["message"] end |
Instance Attribute Details
#amount ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def amount @amount end |
#amount_satoshi ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def amount_satoshi @amount_satoshi end |
#code ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def code @code end |
#created ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def created @created end |
#currency ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def currency @currency end |
#customer_id ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def customer_id @customer_id end |
#description ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def description @description end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
29 30 31 |
# File 'lib/strike/charge.rb', line 29 def error @error end |
#id ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def id @id end |
#message ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def end |
#paid ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def paid @paid end |
#payment_request ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def payment_request @payment_request end |
#updated ⇒ Object
String Unique identifier of the charge
16 17 18 |
# File 'lib/strike/charge.rb', line 16 def updated @updated end |
Class Method Details
.all(page: 1, per_page: 30) ⇒ Object
Fetch a charge by id
returns [Strike::Charge]
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/strike/charge.rb', line 60 def self.all(page: 1, per_page: 30) response = conn.get do |req| req.url api_url req.params['page'] = (page - 1).to_i req.params['size'] = per_page.to_i end handle_errors(response) JSON.parse(response.body).map { |attributes| new(attributes) } end |
.api_url ⇒ Object
8 9 10 |
# File 'lib/strike/charge.rb', line 8 def self.api_url @api_url ||= PATH end |
.conn ⇒ Object
12 13 14 |
# File 'lib/strike/charge.rb', line 12 def self.conn Client.conn end |
.create(amount:, currency:, description:, customer_id:) ⇒ Object
Create a new Strike charge
returns [Strike::Charge]
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/strike/charge.rb', line 92 def self.create(amount:, currency:, description:, customer_id:) body = { amount: amount, currency: currency, description: description, customer_id: customer_id } response = conn.post do |req| req.url api_url req.headers['Content-Type'] = 'application/json' req.body = body.to_json end handle_errors(response) new(JSON.parse(response.body)) end |
.find(id) ⇒ Object
Fetch a charge by id
returns [Strike::Charge]
77 78 79 80 81 82 |
# File 'lib/strike/charge.rb', line 77 def self.find(id) response = conn.get("#{api_url}/#{id}") handle_errors(response) new(JSON.parse(response.body)) end |
.handle_errors(response) ⇒ Object
112 113 114 115 |
# File 'lib/strike/charge.rb', line 112 def self.handle_errors(response) error = Strike::Errors::ERROR_MAP[response.status] raise error.new if error end |
Instance Method Details
#fail? ⇒ Boolean
50 51 52 |
# File 'lib/strike/charge.rb', line 50 def fail? !success? end |
#success? ⇒ Boolean
46 47 48 |
# File 'lib/strike/charge.rb', line 46 def success? code.nil? || code == 200 end |