spike_pay 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +7 -0
- data/lib/spike_pay.rb +121 -0
- data/lib/spike_pay/.charge.rb.swp +0 -0
- data/lib/spike_pay/.data_types.rb.swp +0 -0
- data/lib/spike_pay/api_resource.rb +7 -0
- data/lib/spike_pay/charge.rb +45 -0
- data/lib/spike_pay/data_types.rb +400 -0
- data/lib/spike_pay/error.rb +68 -0
- data/lib/spike_pay/version.rb +3 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/spike_pay_spec.rb +17 -0
- data/spike_pay.gemspec +27 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb676847fd23a7bf12d362ef6036be1fdf6115c1
|
4
|
+
data.tar.gz: bce7c32c6d6c5f2ed4417b20240f6811ff1c5659
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 92086731dcf1101f201b84db2ee9877348d5897aa1d59e6ebd757ab0f0c65099ead98e40db75230a9b8f58815c4af0503f1d647b884d00c79c2d14151a6ea82f
|
7
|
+
data.tar.gz: 2fe248e093d4553fda66eec9824f0c3185993851ab166cf7ebb2ca8438e65d140bde37b06a6fa872f79d0edcd05c90e2d445a9275b830fcfbb6186e02c211902
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kiminari Homma
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# SpikePay
|
2
|
+
|
3
|
+
[SPIKE](https://spike.cc/) Payment service API client for Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'spike_pay'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install spike_pay
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
require 'spike_pay'
|
23
|
+
|
24
|
+
spike = SpikePay.new('sk_test_your_private_key')
|
25
|
+
|
26
|
+
param = {
|
27
|
+
currency: 'JPY',
|
28
|
+
amount: 1000,
|
29
|
+
card: 'tok_card_token_from_spike_checkout',
|
30
|
+
products: [{
|
31
|
+
id: '00001',
|
32
|
+
title: 'item',
|
33
|
+
description: 'item description',
|
34
|
+
language: 'JA',
|
35
|
+
price: 1000,
|
36
|
+
currency: 'JPY',
|
37
|
+
count: 1,
|
38
|
+
stock: 1
|
39
|
+
}]
|
40
|
+
}
|
41
|
+
|
42
|
+
# Charge
|
43
|
+
response = spike.charge.create(param)
|
44
|
+
|
45
|
+
# Refund
|
46
|
+
spike.charge.refund(response["id"])
|
47
|
+
|
48
|
+
# Retrieve
|
49
|
+
spike.charge.retrieve(response["id"])
|
50
|
+
|
51
|
+
# All Charges
|
52
|
+
spike.charge.all({limit: 50})
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it ( https://github.com/honkimi/spike_pay/fork )
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create a new Pull Request
|
62
|
+
|
data/Rakefile
ADDED
data/lib/spike_pay.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
begin
|
3
|
+
require 'mutli_json'
|
4
|
+
JSON_CLASS = MultiJson
|
5
|
+
rescue LoadError
|
6
|
+
require 'json'
|
7
|
+
JSON_CLASS = JSON
|
8
|
+
end
|
9
|
+
|
10
|
+
class SpikePay
|
11
|
+
attr_reader :conn
|
12
|
+
attr_accessor :charge
|
13
|
+
attr_accessor :token
|
14
|
+
|
15
|
+
# Initialize client
|
16
|
+
#
|
17
|
+
# @param options [Hash] options
|
18
|
+
def initialize(auth_token, options = {})
|
19
|
+
options = options.each_with_object({}) { |kv, obj| k,v = kv; obj[k.to_s] = v }
|
20
|
+
|
21
|
+
connection_options = options['faraday_options'] || {}
|
22
|
+
connection_options['headers'] = {
|
23
|
+
"Content-Type" => "application/x-www-form-urlencoded",
|
24
|
+
"Accept" => "application/json",
|
25
|
+
"User-Agent" => "Apipa-spike/1.0.0",
|
26
|
+
"Accept-Language" => "ja",
|
27
|
+
}
|
28
|
+
connection_options['url'] = options['api_base'] || 'https://api.spike.cc/v1'
|
29
|
+
@conn = Faraday.new(connection_options) do |builder|
|
30
|
+
builder.request :url_encoded
|
31
|
+
builder.adapter Faraday.default_adapter
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
@conn.basic_auth(auth_token, '')
|
36
|
+
@charge = SpikePay::Charge.new(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def set_accept_language(value)
|
40
|
+
@conn.headers['Accept-Language'] = value
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# Convert faraday response to a hash by decoding JSON.
|
45
|
+
# This raises error if the response indicates error status.
|
46
|
+
#
|
47
|
+
# @api private
|
48
|
+
# @param response [Faraday::Response]
|
49
|
+
# @return [Hash] Raw object
|
50
|
+
# @raise [SpikePay::SpikeError] For invalid requests (4xx) or internal server error (5xx)
|
51
|
+
def handle_response(response)
|
52
|
+
data =
|
53
|
+
begin
|
54
|
+
JSON_CLASS.load(response.body.force_encoding(infer_encoding(response)))
|
55
|
+
rescue JSON_CLASS::ParserError => e
|
56
|
+
raise SpikePay::ApiConnectionError.invalid_json(e)
|
57
|
+
end
|
58
|
+
status = response.status
|
59
|
+
case status
|
60
|
+
when 200..299
|
61
|
+
data
|
62
|
+
else
|
63
|
+
if status == 400
|
64
|
+
raise SpikePay::ErrorResponse::InvalidRequestError.new(status, data)
|
65
|
+
end
|
66
|
+
if status == 401
|
67
|
+
raise SpikePay::ErrorResponse::AuthenticationError.new(status, data)
|
68
|
+
end
|
69
|
+
if status == 402
|
70
|
+
raise SpikePay::ErrorResponse::CardError.new(status, data)
|
71
|
+
end
|
72
|
+
if status == 404
|
73
|
+
raise SpikePay::ErrorResponse::InvalidRequestError.new(status, data)
|
74
|
+
end
|
75
|
+
if true
|
76
|
+
raise SpikePay::ErrorResponse::ApiError.new(status, data)
|
77
|
+
end
|
78
|
+
raise "Unknown error is returned"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def request(method, url_pattern ,params)
|
83
|
+
url = url_pattern.gsub(/:([[:alnum:]]+)/) { |name| params.__send__(name[1..-1]) }
|
84
|
+
begin
|
85
|
+
response = @conn.__send__(method, url, [:get, :delete].include?(method) ? params.to_h : params.to_h.collect do |k,v|
|
86
|
+
if k != "products"
|
87
|
+
"#{k}=#{v}"
|
88
|
+
else
|
89
|
+
"#{k}=[#{JSON.dump(v[0])}]"
|
90
|
+
end
|
91
|
+
end.join('&'))
|
92
|
+
rescue Faraday::Error::ClientError, URI::InvalidURIError => e
|
93
|
+
raise SpikePay::ApiConnectionError.in_request(e)
|
94
|
+
end
|
95
|
+
handle_response(response)
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
# Infer encoding from response
|
101
|
+
#
|
102
|
+
# @param response [Faraday::Response]
|
103
|
+
# @return [Encoding]
|
104
|
+
def infer_encoding(response)
|
105
|
+
unless (type = response.headers['content-type']) &&
|
106
|
+
(charset = type.split(';').find { |field| field.include?('charset=') })
|
107
|
+
return Encoding.default_external
|
108
|
+
end
|
109
|
+
|
110
|
+
encoding_string = charset.split('=', 2).last.strip
|
111
|
+
Encoding.find(encoding_string)
|
112
|
+
rescue
|
113
|
+
Encoding.default_external
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
require 'spike_pay/api_resource'
|
118
|
+
require 'spike_pay/error'
|
119
|
+
require 'spike_pay/charge'
|
120
|
+
require 'spike_pay/data_types'
|
121
|
+
require "spike_pay/version"
|
Binary file
|
Binary file
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class SpikePay
|
2
|
+
class Charge < SpikePay::ApiResource
|
3
|
+
# Create a charge object with given parameters.
|
4
|
+
# In live mode, this issues a transaction.
|
5
|
+
#
|
6
|
+
# @param params [ChargeRequestCreate|Hash] Parameters to API call
|
7
|
+
# @return [ChargeResponse]
|
8
|
+
def create(params = {})
|
9
|
+
req = SpikePay::ChargeRequestCreate.create(params)
|
10
|
+
raw_response = @client.request(:post, 'charges', req)
|
11
|
+
SpikePay::ChargeResponse.new(raw_response)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Retrieve a existing charge object by charge id
|
15
|
+
#
|
16
|
+
# @param params [ChargeIdRequest|Hash] Parameters to API call
|
17
|
+
# @return [ChargeResponse]
|
18
|
+
def retrieve(params = {})
|
19
|
+
req = SpikePay::ChargeIdRequest.create(params)
|
20
|
+
raw_response = @client.request(:get, 'charges/:id', req)
|
21
|
+
SpikePay::ChargeResponse.new(raw_response)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Refund a paid charge specified by charge id.
|
25
|
+
# Optional argument amount is to refund partially.
|
26
|
+
#
|
27
|
+
# @param params [ChargeRequestWithAmount|Hash] Parameters to API call
|
28
|
+
# @return [ChargeResponse]
|
29
|
+
def refund(params = {})
|
30
|
+
req = SpikePay::ChargeRequestWithAmount.create(params)
|
31
|
+
raw_response = @client.request(:post, 'charges/:id/refund', req)
|
32
|
+
SpikePay::ChargeResponse.new(raw_response)
|
33
|
+
end
|
34
|
+
|
35
|
+
# List charges filtered by params
|
36
|
+
#
|
37
|
+
# @param params [ChargeListRequest|Hash] Parameters to API call
|
38
|
+
# @return [ChargeResponseList]
|
39
|
+
def all(params = {})
|
40
|
+
req = SpikePay::ChargeListRequest.create(params)
|
41
|
+
raw_response = @client.request(:get, 'charges', req)
|
42
|
+
SpikePay::ChargeResponseList.new(raw_response)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,400 @@
|
|
1
|
+
class SpikePay
|
2
|
+
class Entity
|
3
|
+
# Remove nil values and stringify keys
|
4
|
+
def normalize_hash(hash)
|
5
|
+
hash.each_with_object({}) { |kv, obj| k,v = kv; obj[k.to_s] = v unless v == nil }
|
6
|
+
end
|
7
|
+
|
8
|
+
# Convert attributes and its children to pure-Ruby hash
|
9
|
+
# @return [Hash] pure ruby hash including no user objects
|
10
|
+
def to_h
|
11
|
+
@attributes.each_with_object({}) do |kv, obj|
|
12
|
+
k, v = kv
|
13
|
+
next if v == nil
|
14
|
+
obj[k] = v.is_a?(Entity) ? v.to_h : v
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :to_hash, :to_h
|
19
|
+
|
20
|
+
# Pretty print object's data
|
21
|
+
def to_s
|
22
|
+
rendered = "#<#{self.class}\n"
|
23
|
+
self.class.fields.each do |k|
|
24
|
+
rendered << " #{k}: " << @attributes[k].inspect.gsub(/(\r?\n)/, '\1 ') << "\n"
|
25
|
+
end
|
26
|
+
rendered << ">"
|
27
|
+
end
|
28
|
+
|
29
|
+
alias_method :inspect, :to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
class ChargeRequestCreate < Entity
|
34
|
+
attr_reader :attributes
|
35
|
+
|
36
|
+
def self.fields
|
37
|
+
['amount', 'currency', 'card', 'products']
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def self.create(params)
|
42
|
+
return params if params.is_a?(self)
|
43
|
+
hash = case params
|
44
|
+
when Hash; params
|
45
|
+
else
|
46
|
+
raise SpikePay::InvalidRequestError.new("#{self} does not accept the given value", params)
|
47
|
+
end
|
48
|
+
self.new(hash)
|
49
|
+
end
|
50
|
+
|
51
|
+
def initialize(hash = {})
|
52
|
+
hash = normalize_hash(hash)
|
53
|
+
@attributes = hash
|
54
|
+
end
|
55
|
+
|
56
|
+
# attributes accessors
|
57
|
+
def amount
|
58
|
+
attributes['amount']
|
59
|
+
end
|
60
|
+
|
61
|
+
def amount=(value)
|
62
|
+
attributes['amount'] = value
|
63
|
+
end
|
64
|
+
|
65
|
+
def currency
|
66
|
+
attributes['currency']
|
67
|
+
end
|
68
|
+
|
69
|
+
def currency=(value)
|
70
|
+
attributes['currency'] = value
|
71
|
+
end
|
72
|
+
|
73
|
+
def card
|
74
|
+
attributes['card']
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def card=(value)
|
79
|
+
attributes['card'] = value
|
80
|
+
end
|
81
|
+
|
82
|
+
def products
|
83
|
+
attributes['products']
|
84
|
+
end
|
85
|
+
|
86
|
+
def procuts=
|
87
|
+
value = value['products'][0].is_a?(Hash) ? SpikePay::ProductsRequest.new(['products'][0]) : value
|
88
|
+
attributes['products'] = value
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class ChargeRequestWithAmount < Entity
|
93
|
+
attr_reader :attributes
|
94
|
+
def self.fields
|
95
|
+
['id']
|
96
|
+
end
|
97
|
+
def self.create(params)
|
98
|
+
return params if params.is_a?(self)
|
99
|
+
hash = case params
|
100
|
+
when Hash; params
|
101
|
+
else
|
102
|
+
raise SpikePay::InvalidRequestError.new("#{self} does not accept the given value", params)
|
103
|
+
end
|
104
|
+
self.new(hash)
|
105
|
+
end
|
106
|
+
|
107
|
+
def initialize(hash = {})
|
108
|
+
hash = normalize_hash(hash)
|
109
|
+
@attributes = hash
|
110
|
+
end
|
111
|
+
|
112
|
+
# attributes accessors
|
113
|
+
def id
|
114
|
+
attributes['id']
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class ProductsRequest < Entity
|
119
|
+
attr_reader :attributes
|
120
|
+
|
121
|
+
def self.fields
|
122
|
+
['id', 'title', 'description', 'language', 'price', 'currency', 'count', 'stock']
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
def self.create(params)
|
127
|
+
return params if params.is_a?(self)
|
128
|
+
hash = case params
|
129
|
+
when Hash; params
|
130
|
+
else
|
131
|
+
raise SpikePay::InvalidRequestError.new("#{self} does not accept the given value", params)
|
132
|
+
end
|
133
|
+
self.new(hash)
|
134
|
+
end
|
135
|
+
|
136
|
+
def initialize(hash = {})
|
137
|
+
hash = normalize_hash(hash)
|
138
|
+
@attributes = hash
|
139
|
+
end
|
140
|
+
|
141
|
+
# attributes accessors
|
142
|
+
def id
|
143
|
+
attributes['id']
|
144
|
+
end
|
145
|
+
|
146
|
+
def title
|
147
|
+
attributes['title']
|
148
|
+
end
|
149
|
+
|
150
|
+
def description
|
151
|
+
attributes['description']
|
152
|
+
end
|
153
|
+
|
154
|
+
def language
|
155
|
+
attributes['language']
|
156
|
+
end
|
157
|
+
|
158
|
+
def price
|
159
|
+
attributes['price']
|
160
|
+
end
|
161
|
+
|
162
|
+
def currency
|
163
|
+
attributes['currency']
|
164
|
+
end
|
165
|
+
|
166
|
+
def count
|
167
|
+
attributes['count']
|
168
|
+
end
|
169
|
+
|
170
|
+
def stock
|
171
|
+
attributes['stock']
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class ChargeResponse < Entity
|
176
|
+
attr_reader :attributes
|
177
|
+
|
178
|
+
def self.fields
|
179
|
+
['id', 'object', 'livemode', 'amount', 'created', 'currency', 'paid', 'captured', 'refunded', 'amount_refunded', 'refunds']
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
def initialize(hash = {})
|
184
|
+
hash = normalize_hash(hash)
|
185
|
+
@attributes = hash
|
186
|
+
end
|
187
|
+
|
188
|
+
# attributes accessors
|
189
|
+
def id
|
190
|
+
attributes['id']
|
191
|
+
end
|
192
|
+
|
193
|
+
def object
|
194
|
+
attributes['object']
|
195
|
+
end
|
196
|
+
|
197
|
+
def livemode
|
198
|
+
attributes['livemode']
|
199
|
+
end
|
200
|
+
|
201
|
+
def amount
|
202
|
+
attributes['amount']
|
203
|
+
end
|
204
|
+
|
205
|
+
def created
|
206
|
+
attributes['created']
|
207
|
+
end
|
208
|
+
|
209
|
+
def currency
|
210
|
+
attributes['currency']
|
211
|
+
end
|
212
|
+
|
213
|
+
def paid
|
214
|
+
attributes['paid']
|
215
|
+
end
|
216
|
+
|
217
|
+
def captured
|
218
|
+
attributes['captured']
|
219
|
+
end
|
220
|
+
|
221
|
+
def refunded
|
222
|
+
attributes['refunded']
|
223
|
+
end
|
224
|
+
|
225
|
+
def amount_refunded
|
226
|
+
attributes['amount_refunded']
|
227
|
+
end
|
228
|
+
|
229
|
+
def refunds
|
230
|
+
attributes['refunds']
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
class ChargeListRequest < Entity
|
235
|
+
attr_reader :attributes
|
236
|
+
|
237
|
+
def self.fields
|
238
|
+
['limit']
|
239
|
+
end
|
240
|
+
|
241
|
+
|
242
|
+
def self.create(params)
|
243
|
+
return params if params.is_a?(self)
|
244
|
+
hash = case params
|
245
|
+
when Hash; params
|
246
|
+
else
|
247
|
+
raise WebPay::InvalidRequestError.new("#{self} does not accept the given value", params)
|
248
|
+
end
|
249
|
+
self.new(hash)
|
250
|
+
end
|
251
|
+
|
252
|
+
def initialize(hash = {})
|
253
|
+
hash = normalize_hash(hash)
|
254
|
+
@attributes = hash
|
255
|
+
end
|
256
|
+
|
257
|
+
# attributes accessors
|
258
|
+
def limit
|
259
|
+
attributes['limit']
|
260
|
+
end
|
261
|
+
end
|
262
|
+
class ChargeResponseList < Entity
|
263
|
+
attr_reader :attributes
|
264
|
+
|
265
|
+
def self.fields
|
266
|
+
['object', 'url', 'has_more', 'data']
|
267
|
+
end
|
268
|
+
|
269
|
+
|
270
|
+
def initialize(hash = {})
|
271
|
+
hash = normalize_hash(hash)
|
272
|
+
hash['data'] = hash['data'].is_a?(Array) ? hash['data'].map { |x| SpikePay::ChargeResponse.new(x) if x.is_a?(Hash) } : hash['data']
|
273
|
+
@attributes = hash
|
274
|
+
end
|
275
|
+
|
276
|
+
# attributes accessors
|
277
|
+
def object
|
278
|
+
attributes['object']
|
279
|
+
end
|
280
|
+
|
281
|
+
def url
|
282
|
+
attributes['url']
|
283
|
+
end
|
284
|
+
|
285
|
+
def has_more
|
286
|
+
attributes['has_more']
|
287
|
+
end
|
288
|
+
|
289
|
+
def data
|
290
|
+
attributes['data']
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
class ChargeIdRequest < Entity
|
295
|
+
attr_reader :attributes
|
296
|
+
|
297
|
+
def self.fields
|
298
|
+
['id']
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
def self.create(params)
|
303
|
+
return params if params.is_a?(self)
|
304
|
+
hash = case params
|
305
|
+
when Hash; params
|
306
|
+
when SpikePay::ChargeResponse; {'id' => params.id}
|
307
|
+
when String; {'id' => params}
|
308
|
+
else
|
309
|
+
raise SpikePay::InvalidRequestError.new("#{self} does not accept the given value", params)
|
310
|
+
end
|
311
|
+
self.new(hash)
|
312
|
+
end
|
313
|
+
|
314
|
+
def initialize(hash = {})
|
315
|
+
hash = normalize_hash(hash)
|
316
|
+
@attributes = hash
|
317
|
+
end
|
318
|
+
|
319
|
+
# attributes accessors
|
320
|
+
def id
|
321
|
+
attributes['id']
|
322
|
+
end
|
323
|
+
|
324
|
+
|
325
|
+
def id=(value)
|
326
|
+
attributes['id'] = value
|
327
|
+
end
|
328
|
+
|
329
|
+
end
|
330
|
+
class DeletedResponse < Entity
|
331
|
+
attr_reader :attributes
|
332
|
+
|
333
|
+
def self.fields
|
334
|
+
['deleted']
|
335
|
+
end
|
336
|
+
|
337
|
+
|
338
|
+
def initialize(hash = {})
|
339
|
+
hash = normalize_hash(hash)
|
340
|
+
@attributes = hash
|
341
|
+
end
|
342
|
+
|
343
|
+
# attributes accessors
|
344
|
+
def deleted
|
345
|
+
attributes['deleted']
|
346
|
+
end
|
347
|
+
|
348
|
+
end
|
349
|
+
class ErrorBody < Entity
|
350
|
+
attr_reader :attributes
|
351
|
+
|
352
|
+
def self.fields
|
353
|
+
['message', 'type', 'code', 'param']
|
354
|
+
end
|
355
|
+
|
356
|
+
|
357
|
+
def initialize(hash = {})
|
358
|
+
hash = normalize_hash(hash)
|
359
|
+
@attributes = hash
|
360
|
+
end
|
361
|
+
|
362
|
+
# attributes accessors
|
363
|
+
def message
|
364
|
+
attributes['message']
|
365
|
+
end
|
366
|
+
|
367
|
+
def type
|
368
|
+
attributes['type']
|
369
|
+
end
|
370
|
+
|
371
|
+
def code
|
372
|
+
attributes['code']
|
373
|
+
end
|
374
|
+
|
375
|
+
def param
|
376
|
+
attributes['param']
|
377
|
+
end
|
378
|
+
|
379
|
+
end
|
380
|
+
class ErrorData < Entity
|
381
|
+
attr_reader :attributes
|
382
|
+
|
383
|
+
def self.fields
|
384
|
+
['error']
|
385
|
+
end
|
386
|
+
|
387
|
+
|
388
|
+
def initialize(hash = {})
|
389
|
+
hash = normalize_hash(hash)
|
390
|
+
hash['error'] = SpikePay::ErrorBody.new(hash['error']) if hash['error'].is_a?(Hash)
|
391
|
+
@attributes = hash
|
392
|
+
end
|
393
|
+
|
394
|
+
# attributes accessors
|
395
|
+
def error
|
396
|
+
attributes['error']
|
397
|
+
end
|
398
|
+
|
399
|
+
end
|
400
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class SpikePay
|
2
|
+
class ApiError < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class InvalidRequestError < ApiError
|
6
|
+
attr_reader :bad_value
|
7
|
+
|
8
|
+
def initialize(message, bad_value)
|
9
|
+
super(message)
|
10
|
+
@bad_value = bad_value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class InvalidResponseError < ApiError
|
15
|
+
end
|
16
|
+
|
17
|
+
class ApiConnectionError < ApiError
|
18
|
+
attr_reader :cause
|
19
|
+
|
20
|
+
def self.in_request(cause)
|
21
|
+
self.new("API request failed with #{cause}", cause)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.invalid_json(cause)
|
25
|
+
self.new("Server responded invalid JSON string", cause)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(message, cause)
|
29
|
+
@cause = cause
|
30
|
+
super(message)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module ErrorResponse
|
35
|
+
class InvalidRequestError < SpikePay::ApiError
|
36
|
+
attr_reader :status, :data
|
37
|
+
def initialize(status, raw_data)
|
38
|
+
@status = status
|
39
|
+
@data = SpikePay::ErrorData.new(raw_data || {})
|
40
|
+
super(sprintf('%s: %s', 'InvalidRequestError', data.error.message))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
class AuthenticationError < SpikePay::ApiError
|
44
|
+
attr_reader :status, :data
|
45
|
+
def initialize(status, raw_data)
|
46
|
+
@status = status
|
47
|
+
@data = SpikePay::ErrorData.new(raw_data)
|
48
|
+
super(sprintf('%s: %s', 'AuthenticationError', data.error.message))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
class CardError < SpikePay::ApiError
|
52
|
+
attr_reader :status, :data
|
53
|
+
def initialize(status, raw_data)
|
54
|
+
@status = status
|
55
|
+
@data = SpikePay::ErrorData.new(raw_data)
|
56
|
+
super(sprintf('%s: %s', 'CardError', data.error.message))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
class ApiError < SpikePay::ApiError
|
60
|
+
attr_reader :status, :data
|
61
|
+
def initialize(status, raw_data)
|
62
|
+
@status = status
|
63
|
+
@data = SpikePay::ErrorData.new(raw_data)
|
64
|
+
super(sprintf('%s: %s', 'ApiError', data.error.message))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SpikePay do
|
4
|
+
before :each do
|
5
|
+
@spike = SpikePay.new('sk_test_key')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'has a version number' do
|
9
|
+
expect(SpikePay::VERSION).not_to be nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should initalize correctly' do
|
13
|
+
expect(@spike.conn.host).to eq "api.spike.cc"
|
14
|
+
expect(@spike.charge).not_to be nil
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/spike_pay.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spike_pay/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spike_pay"
|
8
|
+
spec.version = SpikePay::VERSION
|
9
|
+
spec.authors = ["Kiminari Homma"]
|
10
|
+
spec.email = ["u533u778@gmail.com"]
|
11
|
+
spec.summary = %q{SPIKE API client for Ruby}
|
12
|
+
spec.description = %q{SPIKE pay enable you to delvelop SPIKE API access easy.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'faraday', '~> 0.8'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "pry-doc"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spike_pay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kiminari Homma
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-doc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: SPIKE pay enable you to delvelop SPIKE API access easy.
|
84
|
+
email:
|
85
|
+
- u533u778@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/spike_pay.rb
|
98
|
+
- lib/spike_pay/.charge.rb.swp
|
99
|
+
- lib/spike_pay/.data_types.rb.swp
|
100
|
+
- lib/spike_pay/api_resource.rb
|
101
|
+
- lib/spike_pay/charge.rb
|
102
|
+
- lib/spike_pay/data_types.rb
|
103
|
+
- lib/spike_pay/error.rb
|
104
|
+
- lib/spike_pay/version.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/spike_pay_spec.rb
|
107
|
+
- spike_pay.gemspec
|
108
|
+
homepage: ''
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.2.2
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: SPIKE API client for Ruby
|
132
|
+
test_files:
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- spec/spike_pay_spec.rb
|
135
|
+
has_rdoc:
|