square 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/Rakefile +17 -0
- data/VERSION +1 -1
- data/lib/square.rb +6 -1
- data/lib/square/connect.rb +15 -0
- data/lib/square/connect/connections.rb +10 -0
- data/lib/square/connect/connections/bank_accounts.rb +8 -0
- data/lib/square/connect/connections/payments.rb +19 -0
- data/lib/square/connect/connections/refunds.rb +8 -0
- data/lib/square/connect/connections/settlements.rb +8 -0
- data/lib/square/connect/device.rb +12 -0
- data/lib/square/connect/error.rb +13 -0
- data/lib/square/connect/itemization.rb +11 -0
- data/lib/square/connect/merchant.rb +29 -0
- data/lib/square/connect/money.rb +12 -0
- data/lib/square/connect/node.rb +79 -0
- data/lib/square/connect/payment.rb +75 -0
- data/lib/square/connect/refund.rb +26 -0
- data/lib/square/connect/tax.rb +16 -0
- data/lib/square/connect/tender.rb +25 -0
- data/lib/square/exception.rb +4 -0
- data/lib/square/oauth2.rb +8 -0
- data/lib/square/oauth2/access_token.rb +11 -0
- data/lib/square/oauth2/client.rb +24 -0
- data/spec/helpers/webmock_helper.rb +58 -0
- data/spec/mock_response/error/unauthorized.json +4 -0
- data/spec/mock_response/invalid_grant.json +3 -0
- data/spec/mock_response/merchant/me.json +7 -0
- data/spec/mock_response/payments/list.json +185 -0
- data/spec/mock_response/payments/single.json +61 -0
- data/spec/mock_response/token.json +6 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/square/connect/connections/payments_spec.rb +19 -0
- data/spec/square/connect/merchant_spec.rb +50 -0
- data/spec/square/connect/node_spec.rb +70 -0
- data/spec/square/connect/payment_spec.rb +58 -0
- data/spec/square/oauth2/access_token_spec.rb +31 -0
- data/spec/square/oauth2/client_spec.rb +90 -0
- data/square.gemspec +13 -9
- metadata +82 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51faae3f912bb913a6fe96bba108f96c8bd1c7a9
|
4
|
+
data.tar.gz: a9965b78a6b816ba7c917c031708cae7ed167ca3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29c980d752c6233bfdcf61944a0a9f1ef8067aae82bb32e4a96de13f5e6cf16eeec74ee7821c08e46ec416dbe9bb1280f7d1955effa3392d8fff86a37d334ec9
|
7
|
+
data.tar.gz: 7bdf1820ef76298b907e3f41dd7daf52151915445dd2ece4a049d050c8eb9c49b94bc8b28e72b54e6384e80cf2731afd47a98ea1c8f6f6da2794a40543f4de83
|
data/.rspec
ADDED
data/Rakefile
CHANGED
@@ -1 +1,18 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
namespace :cover_me do
|
7
|
+
desc "Generates and opens code coverage report."
|
8
|
+
task :report do
|
9
|
+
require 'cover_me'
|
10
|
+
CoverMe.complete!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
task :spec do
|
15
|
+
Rake::Task['cover_me:report'].invoke unless ENV['TRAVIS_RUBY_VERSION']
|
16
|
+
end
|
17
|
+
|
18
|
+
task :default => :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/lib/square.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
ROOT_URL = 'https://connect.squareup.com/v1'
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'square/connect/error'
|
8
|
+
require 'square/connect/node'
|
9
|
+
|
10
|
+
require 'square/connect/device'
|
11
|
+
require 'square/connect/itemization'
|
12
|
+
require 'square/connect/money'
|
13
|
+
require 'square/connect/refund'
|
14
|
+
require 'square/connect/tax'
|
15
|
+
require 'square/connect/tender'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
module Connections
|
4
|
+
module Payments
|
5
|
+
def payments
|
6
|
+
access_token_required!
|
7
|
+
payments = handle_response do
|
8
|
+
access_token.get endpoint_for(identifier, :payments)
|
9
|
+
end
|
10
|
+
payments.collect do |payment|
|
11
|
+
Payment.new payment.merge(
|
12
|
+
access_token: access_token
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
class Itemization
|
4
|
+
attr_accessor :name, :quantity, :item_detail, :notes, :item_variation_name, :total_money, :single_quantity_money, :gross_sales_money, :discount_money, :taxes, :discounts
|
5
|
+
|
6
|
+
def initialize(attributes = {})
|
7
|
+
# TODO
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'square/connect/connections'
|
2
|
+
|
3
|
+
module Square
|
4
|
+
module Connect
|
5
|
+
class Merchant < Node
|
6
|
+
include Connections::Payments
|
7
|
+
include Connections::Refunds
|
8
|
+
include Connections::Settlements
|
9
|
+
include Connections::BankAccounts
|
10
|
+
|
11
|
+
attr_accessor :name, :email, :country_code, :language_code
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
super do |attributes|
|
15
|
+
self.name = attributes[:name]
|
16
|
+
self.email = attributes[:email]
|
17
|
+
self.country_code = attributes[:country_code]
|
18
|
+
self.language_code = attributes[:language_code]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def me(access_token)
|
24
|
+
new :me, access_token
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
class Node
|
4
|
+
attr_accessor :identifier, :endpoint, :access_token
|
5
|
+
|
6
|
+
def initialize(*args)
|
7
|
+
attributes = args.extract_options!
|
8
|
+
self.identifier = attributes[:id] || attributes[:identifier] || args.first
|
9
|
+
self.access_token = tokenize attributes[:access_token] || args.second
|
10
|
+
self.endpoint = endpoint_for identifier
|
11
|
+
yield attributes if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def fetch
|
15
|
+
access_token_required!
|
16
|
+
attributes = handle_response do
|
17
|
+
access_token.get endpoint
|
18
|
+
end
|
19
|
+
self.class.new attributes.merge(
|
20
|
+
access_token: access_token
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def access_token_required!
|
27
|
+
raise Exception.new('access_token required') unless access_token
|
28
|
+
end
|
29
|
+
|
30
|
+
def endpoint_for(*path_elements)
|
31
|
+
File.join ROOT_URL, *path_elements.collect(&:to_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
def tokenize(access_token)
|
35
|
+
case access_token
|
36
|
+
when OAuth2::AccessToken
|
37
|
+
access_token
|
38
|
+
when String
|
39
|
+
OAuth2::AccessToken.new access_token
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def handle_response
|
44
|
+
response = yield
|
45
|
+
case response.status
|
46
|
+
when 200..201
|
47
|
+
handle_success_response response
|
48
|
+
else
|
49
|
+
handle_error_response response
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def handle_success_response(response)
|
54
|
+
parse_json response.body
|
55
|
+
end
|
56
|
+
|
57
|
+
def handle_error_response(response)
|
58
|
+
error = parse_json response.body
|
59
|
+
raise Error.new(response.status, error)
|
60
|
+
end
|
61
|
+
|
62
|
+
def parse_json(raw_json)
|
63
|
+
json = MultiJson.load(raw_json)
|
64
|
+
case json
|
65
|
+
when Hash
|
66
|
+
json.with_indifferent_access
|
67
|
+
when Array
|
68
|
+
json.collect(&:with_indifferent_access)
|
69
|
+
else
|
70
|
+
{}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
require 'square/connect/connections'
|
78
|
+
require 'square/connect/merchant'
|
79
|
+
require 'square/connect/payment'
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
class Payment < Node
|
4
|
+
attr_accessor(
|
5
|
+
:merchant,
|
6
|
+
:creator,
|
7
|
+
:created_at,
|
8
|
+
:description,
|
9
|
+
:device,
|
10
|
+
:inclusive_tax_money,
|
11
|
+
:additive_tax_money,
|
12
|
+
:tax_money,
|
13
|
+
:tip_money,
|
14
|
+
:discount_money,
|
15
|
+
:total_collected_money,
|
16
|
+
:processing_fee_money,
|
17
|
+
:net_total_money,
|
18
|
+
:refunded_money,
|
19
|
+
:inclusive_tax,
|
20
|
+
:additive_tax,
|
21
|
+
:tender,
|
22
|
+
:refunds,
|
23
|
+
:itemizations
|
24
|
+
)
|
25
|
+
|
26
|
+
def initialize(*args)
|
27
|
+
super do |attributes|
|
28
|
+
self.merchant = attributes[:merchant] || Merchant.new(attributes[:merchant_id] || :me)
|
29
|
+
self.creator = attributes[:creator] || attributes[:creator_id] && Merchant.new(attributes[:creator_id])
|
30
|
+
self.created_at = if attributes[:created_at]
|
31
|
+
Time.parse attributes[:created_at]
|
32
|
+
end
|
33
|
+
self.description = attributes[:description]
|
34
|
+
self.device = if attributes[:device].present?
|
35
|
+
Device.new attributes[:device]
|
36
|
+
end
|
37
|
+
[
|
38
|
+
:inclusive_tax_money,
|
39
|
+
:additive_tax_money,
|
40
|
+
:tax_money,
|
41
|
+
:tip_money,
|
42
|
+
:discount_money,
|
43
|
+
:total_collected_money,
|
44
|
+
:processing_fee_money,
|
45
|
+
:net_total_money,
|
46
|
+
:refunded_money
|
47
|
+
].each do |money_key|
|
48
|
+
if attributes[money_key].present?
|
49
|
+
self.send "#{money_key}=", Money.new(attributes[money_key])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
[
|
53
|
+
:inclusive_tax,
|
54
|
+
:additive_tax
|
55
|
+
].each do |tax_key|
|
56
|
+
taxes = Array(attributes[tax_key]).collect do |tax_attributes|
|
57
|
+
Tax.new tax_attributes
|
58
|
+
end
|
59
|
+
self.send "#{tax_key}=", taxes
|
60
|
+
end
|
61
|
+
self.tender = Array(attributes[:tender]).collect do |tender_attributes|
|
62
|
+
Tender.new tender_attributes
|
63
|
+
end
|
64
|
+
self.refunds = Array(attributes[:refunds]).collect do |refund_attributes|
|
65
|
+
Refund.new refund_attributes
|
66
|
+
end
|
67
|
+
self.itemizations = Array(attributes[:itemizations]).collect do |itemization_attributes|
|
68
|
+
Itemization.new itemization_attributes
|
69
|
+
end
|
70
|
+
self.endpoint = endpoint_for merchant.identifier, :payments, identifier
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
class Refund < Node
|
4
|
+
attr_accessor :type, :reason, :refunded_money, :created_at, :processed_at, :payment
|
5
|
+
|
6
|
+
def initialize(*args)
|
7
|
+
super do |attributes|
|
8
|
+
self.type = attributes[:type]
|
9
|
+
self.reason = attributes[:reason]
|
10
|
+
self.refunded_money = if attributes[:refunded_money].present?
|
11
|
+
Money.new attributes[:refunded_money]
|
12
|
+
end
|
13
|
+
[
|
14
|
+
:created_at,
|
15
|
+
:processed_at
|
16
|
+
].each do |time_key|
|
17
|
+
if attributes[time_key]
|
18
|
+
self.send "#{time_key}=", Time.parse(attributes[time_key])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
self.payment = attributes[:payment] || attributes[:payment_id] && Payment.new(attributes[:payment_id])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
class Tax
|
4
|
+
attr_accessor :name, :applied_money, :rate, :inclusion_type
|
5
|
+
|
6
|
+
def initialize(attributes = {})
|
7
|
+
self.name = attributes[:name]
|
8
|
+
self.applied_money = if attributes[:applied_money].present?
|
9
|
+
Money.new attributes[:applied_money]
|
10
|
+
end
|
11
|
+
self.rate = attributes[:rate]
|
12
|
+
self.inclusion_type = attributes[:inclusion_type]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Square
|
2
|
+
module Connect
|
3
|
+
class Tender
|
4
|
+
attr_accessor :type, :name, :card_brand, :pan_suffix, :entry_method, :payment_note, :total_money, :tendered_money, :change_back_money
|
5
|
+
|
6
|
+
def initialize(attributes = {})
|
7
|
+
self.type = attributes[:type]
|
8
|
+
self.name = attributes[:name]
|
9
|
+
self.card_brand = attributes[:card_brand]
|
10
|
+
self.pan_suffix = attributes[:pan_suffix]
|
11
|
+
self.entry_method = attributes[:entry_method]
|
12
|
+
self.payment_note = attributes[:payment_note]
|
13
|
+
[
|
14
|
+
:total_money,
|
15
|
+
:tendered_money,
|
16
|
+
:change_back_money
|
17
|
+
].each do |money_attr|
|
18
|
+
if attributes[money_attr].present?
|
19
|
+
self.send "#{money_attr}=", Money.new(attributes[money_attr])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|