square 0.0.0 → 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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/Rakefile +17 -0
  4. data/VERSION +1 -1
  5. data/lib/square.rb +6 -1
  6. data/lib/square/connect.rb +15 -0
  7. data/lib/square/connect/connections.rb +10 -0
  8. data/lib/square/connect/connections/bank_accounts.rb +8 -0
  9. data/lib/square/connect/connections/payments.rb +19 -0
  10. data/lib/square/connect/connections/refunds.rb +8 -0
  11. data/lib/square/connect/connections/settlements.rb +8 -0
  12. data/lib/square/connect/device.rb +12 -0
  13. data/lib/square/connect/error.rb +13 -0
  14. data/lib/square/connect/itemization.rb +11 -0
  15. data/lib/square/connect/merchant.rb +29 -0
  16. data/lib/square/connect/money.rb +12 -0
  17. data/lib/square/connect/node.rb +79 -0
  18. data/lib/square/connect/payment.rb +75 -0
  19. data/lib/square/connect/refund.rb +26 -0
  20. data/lib/square/connect/tax.rb +16 -0
  21. data/lib/square/connect/tender.rb +25 -0
  22. data/lib/square/exception.rb +4 -0
  23. data/lib/square/oauth2.rb +8 -0
  24. data/lib/square/oauth2/access_token.rb +11 -0
  25. data/lib/square/oauth2/client.rb +24 -0
  26. data/spec/helpers/webmock_helper.rb +58 -0
  27. data/spec/mock_response/error/unauthorized.json +4 -0
  28. data/spec/mock_response/invalid_grant.json +3 -0
  29. data/spec/mock_response/merchant/me.json +7 -0
  30. data/spec/mock_response/payments/list.json +185 -0
  31. data/spec/mock_response/payments/single.json +61 -0
  32. data/spec/mock_response/token.json +6 -0
  33. data/spec/spec_helper.rb +4 -0
  34. data/spec/square/connect/connections/payments_spec.rb +19 -0
  35. data/spec/square/connect/merchant_spec.rb +50 -0
  36. data/spec/square/connect/node_spec.rb +70 -0
  37. data/spec/square/connect/payment_spec.rb +58 -0
  38. data/spec/square/oauth2/access_token_spec.rb +31 -0
  39. data/spec/square/oauth2/client_spec.rb +90 -0
  40. data/square.gemspec +13 -9
  41. metadata +82 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4af0de4c8ca0b30d6a9016dbaeca0c1e14150972
4
- data.tar.gz: 3ffac80d44746f500bf5eca525a389ddf517ef41
3
+ metadata.gz: 51faae3f912bb913a6fe96bba108f96c8bd1c7a9
4
+ data.tar.gz: a9965b78a6b816ba7c917c031708cae7ed167ca3
5
5
  SHA512:
6
- metadata.gz: bafabb5eb1c1ac1b01212f305bca5874a8776ac784488dbfa78eca0c62388c3936b53075fab524114baafb7f3b12eeb014447182cf6ac31b0bc31eff181cc9d3
7
- data.tar.gz: 90090c0b2c93cf236723178e37f50ae7ee39ba9ad79e186fbae523fe2b747dfc58b0c19e12d461aec9767531b4a1cb679ba0a504a2c14281443dda84dd60c44e
6
+ metadata.gz: 29c980d752c6233bfdcf61944a0a9f1ef8067aae82bb32e4a96de13f5e6cf16eeec74ee7821c08e46ec416dbe9bb1280f7d1955effa3392d8fff86a37d334ec9
7
+ data.tar.gz: 7bdf1820ef76298b907e3f41dd7daf52151915445dd2ece4a049d050c8eb9c49b94bc8b28e72b54e6384e80cf2731afd47a98ea1c8f6f6da2794a40543f4de83
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
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.0
1
+ 0.0.1
@@ -1,3 +1,8 @@
1
+ require 'rack/oauth2'
2
+
1
3
  module Square
2
- # Your code goes here...
3
4
  end
5
+
6
+ require 'square/exception'
7
+ require 'square/oauth2'
8
+ require 'square/connect'
@@ -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,10 @@
1
+ module Square
2
+ module Connect
3
+ module Connections
4
+ end
5
+ end
6
+ end
7
+
8
+ Dir[File.dirname(__FILE__) + '/connections/*.rb'].each do |file|
9
+ require file
10
+ end
@@ -0,0 +1,8 @@
1
+ module Square
2
+ module Connect
3
+ module Connections
4
+ module BankAccounts
5
+ end
6
+ end
7
+ end
8
+ end
@@ -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,8 @@
1
+ module Square
2
+ module Connect
3
+ module Connections
4
+ module Refunds
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Square
2
+ module Connect
3
+ module Connections
4
+ module Settlements
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ module Connect
3
+ class Device
4
+ attr_accessor :identifier, :name
5
+
6
+ def initialize(attributes = {})
7
+ self.identifier = attributes[:id]
8
+ self.name = attributes[:name]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Square
2
+ module Connect
3
+ class Error < StandardError
4
+ attr_accessor :status, :type
5
+
6
+ def initialize(status, error)
7
+ self.status = status
8
+ self.type = error[:type]
9
+ super error[:message]
10
+ end
11
+ end
12
+ end
13
+ 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,12 @@
1
+ module Square
2
+ module Connect
3
+ class Money
4
+ attr_accessor :amount, :currency_code
5
+
6
+ def initialize(attributes = {})
7
+ self.amount = attributes[:amount]
8
+ self.currency_code = attributes[:currency_code]
9
+ end
10
+ end
11
+ end
12
+ 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
@@ -0,0 +1,4 @@
1
+ module Square
2
+ class Exception < StandardError
3
+ end
4
+ end