bitpay-sdk 2.3.2 → 2.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5f07e2ece3936d90f1855bf3c7c7c47e1528943
4
- data.tar.gz: 2d7f13e49b0a7a0ce03804c99654c171a176cd86
3
+ metadata.gz: f17aaeed06964a9e0cb8aaa38e8e291b5135e0e2
4
+ data.tar.gz: c84ffdc47caf825006420824e3f63b61a22bc27a
5
5
  SHA512:
6
- metadata.gz: 2041a1d3930606a93371eb07f18af6700b51e95a3c7992093f66a702f3dc2eb4da2a45c104b14387ec3f3ddd5d783c376f112142eaaf24669783095c7140a4d2
7
- data.tar.gz: 6b176eb98b9263f49269db988fe046c914133a62dd08d2c36e1e8d92a66cf234849261c8fda152686b6759aea36394cfcf8b51ea0562f8886486c0d5b7c9907b
6
+ metadata.gz: 66d9265331b654fa308f0ae24782fe62d9a0b113e5a470341400d329edd0e3a4b71c36964f02a9c5e0c6a793a702e8444c98357728c0efa84c6f65ae9c000117
7
+ data.tar.gz: 86f177707cbc8f8c2f71d4a4b031153935ce62e3fef1be0077f26de57dd77b9076f4d1a579a60e6266e537f8e571bdd030defb89dff6a5917b0639dae61ce927
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## [2.3.3] - 2015-03-12
6
+ ### Changed
7
+ - Pull refund functionality from 2.3 spec
8
+ - Bump gem version
9
+
10
+ ### Fixed
11
+ - Fix Bug: GitHub issue #40 - error on return from endpoints that do not have a "data" field
data/config/constants.rb CHANGED
@@ -11,9 +11,9 @@ TEST_PASS = ENV['RCTESTPASSWORD']
11
11
  DASHBOARD_URL = "#{ROOT_ADDRESS}/dashboard/merchant/home"
12
12
 
13
13
  unless
14
- ROOT_ADDRESS &&
15
- TEST_USER &&
16
- TEST_PASS
14
+ ROOT_ADDRESS &&
15
+ TEST_USER &&
16
+ TEST_PASS
17
17
  then
18
- raise "Missing configuration options - see constants.rb"
18
+ raise "Missing configuration options - see constants.rb"
19
19
  end
@@ -21,8 +21,8 @@ Feature: creating an invoice
21
21
  Then they will receive a BitPay::ArgumentError matching <message>
22
22
  Examples:
23
23
  | price | currency | message |
24
- | "5,023" | "USD" | "Price must be formatted as a float" |
25
- | "3.21" | "EaUR" | "Currency is invalid." |
24
+ | "5,023" | "USD" | "Price must be formatted as a float" |
25
+ | "3.21" | "EaUR" | "Currency is invalid." |
26
26
  | "" | "USD" | "Price must be formatted as a float" |
27
27
  | "Ten" | "USD" | "Price must be formatted as a float" |
28
- | "10" | "" | "Currency is invalid." |
28
+ | "10" | "" | "Currency is invalid." |
@@ -61,5 +61,5 @@ Given(/^the user requests a client\-side pairing$/) do
61
61
  end
62
62
 
63
63
  Then(/^they will receive a claim code$/) do
64
- expect(@response["data"].first["pairingCode"] ).not_to be_empty
64
+ expect(@response.first["pairingCode"] ).not_to be_empty
65
65
  end
@@ -66,7 +66,7 @@ def new_client_from_stored_values
66
66
  end
67
67
 
68
68
  def get_token_from_file
69
- token = JSON.parse(File.read(BitPay::TOKEN_FILE_PATH))['data'][0]
69
+ token = JSON.parse(File.read(BitPay::TOKEN_FILE_PATH))[0]
70
70
  {token['facade'] => token['token']}
71
71
  end
72
72
 
data/lib/bitpay/client.rb CHANGED
@@ -1,4 +1,4 @@
1
- # license Copyright 2011-2014 BitPay, Inc., MIT License
1
+ # license Copyright 2011-2015 BitPay, Inc., MIT License
2
2
  # see http://opensource.org/licenses/MIT
3
3
  # or https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
4
4
 
@@ -7,13 +7,14 @@ require 'net/https'
7
7
  require 'json'
8
8
 
9
9
  require_relative 'key_utils'
10
+ require_relative 'rest_connector'
10
11
 
11
12
  module BitPay
12
13
  # This class is used to instantiate a BitPay Client object. It is expected to be thread safe.
13
14
  #
14
15
  module SDK
15
16
  class Client
16
-
17
+ include BitPay::RestConnector
17
18
  # @return [Client]
18
19
  # @example
19
20
  # # Create a client with a pem file created by the bitpay client:
@@ -46,7 +47,8 @@ module BitPay
46
47
  # => Pass {pairingCode: 'WfD01d2'} to claim a server-initiated pairing code
47
48
  #
48
49
  def pair_client(params={})
49
- pairing_request(params)
50
+ tokens = post(path: 'tokens', params: params)
51
+ return tokens["data"]
50
52
  end
51
53
 
52
54
  ## Compatibility method for pos pairing
@@ -67,24 +69,16 @@ module BitPay
67
69
  currency == 'BTC' && /^[[:digit:]]+(\.[[:digit:]]{1,6})?$/.match(price)
68
70
  raise BitPay::ArgumentError, "Illegal Argument: Currency is invalid." unless /^[[:upper:]]{3}$/.match(currency)
69
71
  params.merge!({price: price, currency: currency})
70
- response = send_request("POST", "invoices", facade: facade, params: params)
71
- response["data"]
72
+ token = get_token(facade)
73
+ invoice = post(path: "invoices", token: token, params: params)
74
+ invoice["data"]
72
75
  end
73
76
 
74
- ## Gets the privileged merchant-version of the invoice
75
- # Requires merchant facade token
76
- #
77
- def get_invoice(id:)
78
- response = send_request("GET", "invoices/#{id}", facade: 'merchant')
79
- response["data"]
80
- end
81
-
82
77
  ## Gets the public version of the invoice
83
78
  #
84
79
  def get_public_invoice(id:)
85
- request = Net::HTTP::Get.new("/invoices/#{id}")
86
- response = process_request(request)
87
- response["data"]
80
+ invoice = get(path: "invoices/#{id}", public: true)
81
+ invoice["data"]
88
82
  end
89
83
 
90
84
  ## Checks that the passed tokens are valid by
@@ -98,116 +92,12 @@ module BitPay
98
92
  tokens.each{|key, value| return false if server_tokens[key] != value}
99
93
  return true
100
94
  end
101
-
102
- ## Generates REST request to api endpoint
103
- # => Defaults to merchant facade unless token or facade is explicitly provided
104
- #
105
- def send_request(verb, path, facade: 'merchant', params: {}, token: nil)
106
- token ||= get_token(facade)
107
-
108
- # Verb-specific logic
109
- case verb.upcase
110
- when "GET"
111
- urlpath = '/' + path + '?token=' + token
112
- request = Net::HTTP::Get.new urlpath
113
- request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key)
114
-
115
- when "PUT"
116
-
117
- when "POST" # Requires a GUID
118
-
119
- urlpath = '/' + path
120
- request = Net::HTTP::Post.new urlpath
121
- params[:token] = token
122
- params[:guid] = SecureRandom.uuid
123
- params[:id] = @client_id
124
- request.body = params.to_json
125
- request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath + request.body, @priv_key)
126
-
127
- when "DELETE"
128
-
129
- raise(BitPayError, "Invalid HTTP verb: #{verb.upcase}")
130
- end
131
-
132
- # Build request headers and submit
133
- request['X-Identity'] = @pub_key
134
-
135
- response = process_request(request)
136
- end
137
95
 
138
- ##### PRIVATE METHODS #####
139
96
  private
140
97
 
141
- ## Processes HTTP Request and returns parsed response
142
- # Otherwise throws error
143
- #
144
- def process_request(request)
145
-
146
- request['User-Agent'] = @user_agent
147
- request['Content-Type'] = 'application/json'
148
- request['X-BitPay-Plugin-Info'] = 'Rubylib' + VERSION
149
-
150
- begin
151
- response = @https.request request
152
- rescue => error
153
- raise BitPay::ConnectionError, "#{error.message}"
154
- end
155
-
156
- if response.kind_of? Net::HTTPSuccess
157
- return JSON.parse(response.body)
158
- elsif JSON.parse(response.body)["error"]
159
- raise(BitPayError, "#{response.code}: #{JSON.parse(response.body)['error']}")
160
- else
161
- raise BitPayError, "#{response.code}: #{JSON.parse(response.body)}"
162
- end
163
-
164
- end
165
-
166
- ## Fetches the tokens hash from the server and
167
- # updates @tokens
168
- #
169
- def refresh_tokens
170
- urlpath = '/tokens'
171
-
172
- request = Net::HTTP::Get.new(urlpath)
173
- request['X-Identity'] = @pub_key
174
- request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key)
175
-
176
- response = process_request(request)
177
- token_array = response["data"] || {}
178
-
179
- tokens = {}
180
- token_array.each do |t|
181
- tokens[t.keys.first] = t.values.first
182
- end
183
-
184
- @tokens = tokens
185
- return tokens
186
-
187
- end
188
-
189
- ## Makes a request to /tokens for pairing
190
- # Adds passed params as post parameters
191
- # If empty params, retrieves server-generated pairing code
192
- # If pairingCode key/value is passed, will pair client ID to this account
193
- # Returns response hash
194
- #
195
- def pairing_request(params)
196
- urlpath = '/tokens'
197
- request = Net::HTTP::Post.new urlpath
198
- params[:guid] = SecureRandom.uuid
199
- params[:id] = @client_id
200
- request.body = params.to_json
201
- process_request(request)
202
- end
203
-
204
- def get_token(facade)
205
- token = @tokens[facade] || refresh_tokens[facade] || raise(BitPayError, "Not authorized for facade: #{facade}")
206
- end
207
-
208
98
  def verify_claim_code(claim_code)
209
99
  regex = /^[[:alnum:]]{7}$/
210
- matches = regex.match(claim_code)
100
+ matches = regex.match(claim_code)
211
101
  !(matches.nil?)
212
102
  end
213
103
  end
@@ -0,0 +1,96 @@
1
+ # license Copyright 2011-2015 BitPay, Inc., MIT License
2
+ # see http://opensource.org/licenses/MIT
3
+ # or https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
4
+
5
+ module BitPay
6
+ module RestConnector
7
+ def send_request(verb, path, facade: 'merchant', params: {}, token: nil)
8
+ token ||= get_token(facade)
9
+ case verb.upcase
10
+ when "GET"
11
+ return get(path: path, token:token)
12
+ when "POST"
13
+ return post(path: path, token: token, params: params)
14
+ else
15
+ raise(BitPayError, "Invalid HTTP verb: #{verb.upcase}")
16
+ end
17
+ end
18
+
19
+ def get(path:, token: nil, public: false)
20
+ urlpath = '/' + path
21
+ urlpath = urlpath + '?token=' + token if token
22
+ request = Net::HTTP::Get.new urlpath
23
+ unless public
24
+ request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key)
25
+ request['X-Identity'] = @pub_key
26
+ end
27
+ process_request(request)
28
+ end
29
+
30
+ def post(path:, token: nil, params:)
31
+ urlpath = '/' + path
32
+ request = Net::HTTP::Post.new urlpath
33
+ params[:token] = token if token
34
+ params[:guid] = SecureRandom.uuid
35
+ params[:id] = @client_id
36
+ request.body = params.to_json
37
+ if token
38
+ request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath + request.body, @priv_key)
39
+ request['X-Identity'] = @pub_key
40
+ end
41
+ process_request(request)
42
+ end
43
+
44
+ private
45
+
46
+ ## Processes HTTP Request and returns parsed response
47
+ # Otherwise throws error
48
+ #
49
+ def process_request(request)
50
+ request['User-Agent'] = @user_agent
51
+ request['Content-Type'] = 'application/json'
52
+ request['X-BitPay-Plugin-Info'] = 'Rubylib' + VERSION
53
+
54
+ begin
55
+ response = @https.request request
56
+ rescue => error
57
+ raise BitPay::ConnectionError, "#{error.message}"
58
+ end
59
+
60
+ if response.kind_of? Net::HTTPSuccess
61
+ return JSON.parse(response.body)
62
+ elsif JSON.parse(response.body)["error"]
63
+ raise(BitPayError, "#{response.code}: #{JSON.parse(response.body)['error']}")
64
+ else
65
+ raise BitPayError, "#{response.code}: #{JSON.parse(response.body)}"
66
+ end
67
+
68
+ end
69
+
70
+ ## Fetches the tokens hash from the server and
71
+ # updates @tokens
72
+ #
73
+ def refresh_tokens
74
+ response = get(path: 'tokens')["data"]
75
+ token_array = response || {}
76
+ tokens = {}
77
+ token_array.each do |t|
78
+ tokens[t.keys.first] = t.values.first
79
+ end
80
+ @tokens = tokens
81
+ return tokens
82
+ end
83
+
84
+ ## Makes a request to /tokens for pairing
85
+ # Adds passed params as post parameters
86
+ # If empty params, retrieves server-generated pairing code
87
+ # If pairingCode key/value is passed, will pair client ID to this account
88
+ # Returns response hash
89
+ #
90
+
91
+ def get_token(facade)
92
+ token = @tokens[facade] || refresh_tokens[facade] || raise(BitPayError, "Not authorized for facade: #{facade}")
93
+ end
94
+
95
+ end
96
+ end
@@ -3,5 +3,5 @@
3
3
  # or https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
4
4
 
5
5
  module BitPay
6
- VERSION = '2.3.2'
6
+ VERSION = '2.3.3'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitpay-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.2
4
+ version: 2.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bitpay, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -215,6 +215,7 @@ extra_rdoc_files: []
215
215
  files:
216
216
  - ".gitignore"
217
217
  - ".travis.yml"
218
+ - CHANGELOG.md
218
219
  - GUIDE.md
219
220
  - Gemfile
220
221
  - LICENSE.md
@@ -232,6 +233,7 @@ files:
232
233
  - lib/bitpay/cacert.pem
233
234
  - lib/bitpay/client.rb
234
235
  - lib/bitpay/key_utils.rb
236
+ - lib/bitpay/rest_connector.rb
235
237
  - lib/bitpay/version.rb
236
238
  - lib/bitpay_sdk.rb
237
239
  - spec/client_spec.rb