square-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +13 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +73 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +7 -0
  11. data/lib/square.rb +206 -0
  12. data/lib/square/api_operations/create.rb +30 -0
  13. data/lib/square/api_operations/delete.rb +24 -0
  14. data/lib/square/api_operations/list.rb +21 -0
  15. data/lib/square/api_operations/retrieve.rb +21 -0
  16. data/lib/square/api_operations/update.rb +25 -0
  17. data/lib/square/api_resource.rb +55 -0
  18. data/lib/square/bank_accounts.rb +10 -0
  19. data/lib/square/category.rb +12 -0
  20. data/lib/square/data_type.rb +7 -0
  21. data/lib/square/data_types/bank_account.rb +32 -0
  22. data/lib/square/data_types/category.rb +12 -0
  23. data/lib/square/data_types/coordinates.rb +12 -0
  24. data/lib/square/data_types/device.rb +12 -0
  25. data/lib/square/data_types/discount.rb +36 -0
  26. data/lib/square/data_types/fee.rb +39 -0
  27. data/lib/square/data_types/global_address.rb +67 -0
  28. data/lib/square/data_types/inventory_entry.rb +12 -0
  29. data/lib/square/data_types/item.rb +79 -0
  30. data/lib/square/data_types/item_image.rb +12 -0
  31. data/lib/square/data_types/item_variation.rb +49 -0
  32. data/lib/square/data_types/merchant.rb +61 -0
  33. data/lib/square/data_types/merchant_location_details.rb +11 -0
  34. data/lib/square/data_types/modifier_list.rb +19 -0
  35. data/lib/square/data_types/modifier_option.rb +30 -0
  36. data/lib/square/data_types/money.rb +40 -0
  37. data/lib/square/data_types/payment.rb +87 -0
  38. data/lib/square/data_types/payment_discount.rb +17 -0
  39. data/lib/square/data_types/payment_item_detail.rb +18 -0
  40. data/lib/square/data_types/payment_itemization.rb +53 -0
  41. data/lib/square/data_types/payment_modifier.rb +16 -0
  42. data/lib/square/data_types/payment_tax.rb +23 -0
  43. data/lib/square/data_types/phone_number.rb +13 -0
  44. data/lib/square/data_types/refund.rb +31 -0
  45. data/lib/square/data_types/settlement.rb +28 -0
  46. data/lib/square/data_types/settlement_entry.rb +21 -0
  47. data/lib/square/data_types/tender.rb +50 -0
  48. data/lib/square/discount.rb +12 -0
  49. data/lib/square/fee.rb +49 -0
  50. data/lib/square/inventory.rb +26 -0
  51. data/lib/square/item.rb +40 -0
  52. data/lib/square/list_response.rb +79 -0
  53. data/lib/square/merchant.rb +13 -0
  54. data/lib/square/payment.rb +10 -0
  55. data/lib/square/refund.rb +10 -0
  56. data/lib/square/settlement.rb +10 -0
  57. data/lib/square/variation.rb +12 -0
  58. data/lib/square/version.rb +3 -0
  59. data/lib/square/webhook.rb +10 -0
  60. data/square-ruby.gemspec +31 -0
  61. metadata +219 -0
@@ -0,0 +1,13 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-phonenumber
4
+ class PhoneNumber < Square::DataType
5
+ # The phone number's international calling code. For US phone numbers,
6
+ # this value is +1.
7
+ property :calling_code
8
+
9
+ # The phone number.
10
+ property :number
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-refund
4
+ class Refund < DataType
5
+ # The type of refund (FULL or PARTIAL).
6
+ property :type # Refund.Type
7
+
8
+ # The merchant-specified reason for the refund.
9
+ property :reason
10
+
11
+ # The amount of money refunded. This amount is always negative.
12
+ property :refunded_money, coerce: Square::DataTypes::Money
13
+
14
+ # The time when the merchant initiated the refund for Square to process,
15
+ # in ISO 8601 format.
16
+ property :created_at
17
+
18
+ # The time when Square processed the refund on behalf of the merchant,
19
+ # in ISO 8601 format.
20
+ property :processed_at
21
+
22
+ # The Square-issued ID of the payment the refund is applied to.
23
+ property :payment_id
24
+
25
+ # undocumented
26
+ property :merchant_id
27
+ property :refunded_processing_fee_money
28
+ property :refunded_additive_tax_money
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-settlement
4
+ class Settlement < Square::DataType
5
+ # The settlement's unique identifier.
6
+ property :id
7
+
8
+ # The settlement's current status.
9
+ property :status # Settlement.Status
10
+
11
+ # The time when the settlement was submitted for deposit or withdrawal,
12
+ # in ISO 8601 format.
13
+ property :initiated_at
14
+
15
+ # The Square-issued unique identifier for the bank account associated
16
+ # with the settlement.
17
+ property :bank_account_id
18
+
19
+ # The amount of money involved in the settlement. A positive amount
20
+ # indicates a deposit, and a negative amount indicates a withdrawal.
21
+ # This amount is never zero.
22
+ property :total_money, coerce: Square::DataTypes::Money
23
+
24
+ # The entries included in this settlement.
25
+ property :entries, coerce: Array[Square::DataTypes::SettlementEntry]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-settlemententry
4
+ class SettlementEntry < Square::DataType
5
+ # The type of activity this entry represents.
6
+ property :type # SettlementEntry.Type
7
+
8
+ # The payment associated with the settlement entry, if any.
9
+ property :payment_id
10
+
11
+ # The total amount of money this entry contributes to the total
12
+ # settlement amount.
13
+ property :amount_money, coerce: Square::DataTypes::Money
14
+
15
+ # The amount of all Square fees associated with this settlement entry.
16
+ # This value is always negative or zero.
17
+ # This amount has already been applied to amount_money.
18
+ property :fee_money, coerce: Square::DataTypes::Money
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,50 @@
1
+ module Square
2
+ module DataTypes
3
+ class Tender < DataType
4
+ # The tender's unique ID.
5
+ property :id
6
+
7
+ # The type of tender.
8
+ property :type # Tender.Type
9
+
10
+ # A human-readable description of the tender.
11
+ property :name
12
+
13
+ # The ID of the employee that processed the tender.
14
+ # This field is included only if the associated merchant had employee
15
+ # management features enabled at the time the tender was processed.
16
+ property :employee_id
17
+
18
+ # The URL of the receipt for the tender.
19
+ property :receipt_url
20
+
21
+ # The brand of credit card provided.
22
+ # Only present if the tender's type is CREDIT_CARD.
23
+ property :card_brand # Tender.CardBrand
24
+
25
+ # The last four digits of the provided credit card's account number.
26
+ # Only present if the tender's type is CREDIT_CARD.
27
+ property :pan_suffix
28
+
29
+ # The method with which the tender was entered.
30
+ property :entry_method # Tender.EntryMethod
31
+
32
+ # Notes entered by the merchant about the tender at the time of payment,
33
+ # if any. Typically only present for tender with the type OTHER.
34
+ property :payment_note
35
+
36
+ # The total amount of money provided in this form of tender.
37
+ property :total_money, coerce: Square::DataTypes::Money
38
+
39
+ # The amount of total_money applied to the payment.
40
+ property :tendered_money, coerce: Square::DataTypes::Money
41
+
42
+ # The amount of total_money returned to the buyer as change.
43
+ property :change_back_money, coerce: Square::DataTypes::Money
44
+
45
+ # The total of all refunds applied to this tender.
46
+ # This amount is always negative or zero.
47
+ property :refunded_money, coerce: Square::DataTypes::Money
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-discounts
3
+ class Discount < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Create
6
+ extend Square::APIOperations::Update
7
+ extend Square::APIOperations::Delete
8
+
9
+ endpoint_base 'discounts'
10
+ data_type Square::DataTypes::Discount
11
+ end
12
+ end
@@ -0,0 +1,49 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-fees
3
+ class Fee < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Retrieve
6
+ extend Square::APIOperations::Create
7
+ extend Square::APIOperations::Update
8
+ extend Square::APIOperations::Delete
9
+
10
+ endpoint_base 'fees'
11
+ data_type Square::DataTypes::Fee
12
+
13
+ # Adjust inventory for a varation.
14
+ #
15
+ # @param item_id [String] Item ID.
16
+ # @param fee_id [String] Fee ID.
17
+ # @param params [Hash] Params hash. Optional.
18
+ #
19
+ # @return [Square::DataType]
20
+ def self.apply(item_id, fee_id, params = {})
21
+ response = Square.make_request(
22
+ method: 'PUT',
23
+ endpoint: "items/#{item_id}/fees/#{fee_id}",
24
+ params: params
25
+ )
26
+
27
+ response = Square.parse_response(response)
28
+ Square::DataTypes::Item.new(response)
29
+ end
30
+
31
+ # Adjust inventory for a varation.
32
+ #
33
+ # @param item_id [String] Item ID.
34
+ # @param fee_id [String] Fee ID.
35
+ # @param params [Hash] Params hash. Optional.
36
+ #
37
+ # @return [Square::DataType]
38
+ def self.remove(item_id, fee_id, params = {})
39
+ response = Square.make_request(
40
+ method: 'DELETE',
41
+ endpoint: "items/#{item_id}/fees/#{fee_id}",
42
+ params: params
43
+ )
44
+
45
+ response = Square.parse_response(response)
46
+ Square::DataTypes::Item.new(response)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-inventory
3
+ class Inventory < APIResource
4
+ extend Square::APIOperations::List
5
+
6
+ endpoint_base 'inventory'
7
+ data_type Square::DataTypes::InventoryEntry
8
+
9
+ # Adjust inventory for a varation.
10
+ #
11
+ # @param variation_id [String] Variation ID.
12
+ # @param params [Hash] Params hash. Optional.
13
+ #
14
+ # @return [Square::DataType]
15
+ def self.adjust(variation_id, params = {})
16
+ response = Square.make_request(
17
+ method: 'POST',
18
+ endpoint: self.generate_endpoint_url(variation_id),
19
+ payload: params
20
+ )
21
+
22
+ response = Square.parse_response(response)
23
+ @data_type.new(response)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-itemmanagement
3
+ class Item < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Retrieve
6
+ extend Square::APIOperations::Create
7
+ extend Square::APIOperations::Update
8
+ extend Square::APIOperations::Delete
9
+
10
+ endpoint_base 'items'
11
+ data_type Square::DataTypes::Item
12
+
13
+ # Upload an image for an item.
14
+ # https://docs.connect.squareup.com/api/connect/v1/#post-image
15
+ #
16
+ # @param image_url [String] Image URL.
17
+ # @param item_id [String] Square item id.
18
+ #
19
+ # @return [Net::HTTPResponse] API response.
20
+ def self.upload_image(image_url, item_id)
21
+ image = open(image_url)
22
+ filename = File.basename(image_url)
23
+
24
+ uri = URI.parse("#{Square.api_host}/v1/me/#{instance_variable_get('@endpoint_base')}/#{item_id}/image")
25
+ connection = Net::HTTP.new(uri.host, uri.port)
26
+ connection.use_ssl = (uri.scheme == 'https')
27
+
28
+ # Making this request here because RestClient doesn't do multipart
29
+ # requests the way Square wants it.
30
+ request = Net::HTTP::Post::Multipart.new(
31
+ uri.path,
32
+ image_data: UploadIO.new(image, 'image/jpeg', filename)
33
+ )
34
+
35
+ # Copy the auth header.
36
+ request['Authorization'] = Square.request_headers(Square.access_token)[:authorization]
37
+ connection.request(request)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,79 @@
1
+ module Square
2
+ class ListResponse
3
+ include Enumerable
4
+
5
+ attr_accessor :response
6
+
7
+ # RegExp used for parsing Link headers when the API paginates data. Don't
8
+ # care about rel attributes right now because this is the only thing this is
9
+ # used for.
10
+ LINK_REGEXP = /^<([ -~]+)>;/i
11
+
12
+ # Initialize.
13
+ #
14
+ # @param response [RestClient::Response] Raw resonse object.
15
+ # @param data_type [Square::DataType] Data type to new up records.
16
+ #
17
+ # @return [ListResponse]
18
+ def initialize(response, data_type)
19
+ @response = response
20
+ @data_type = data_type
21
+ @values = {}
22
+ parse_response(@response)
23
+ end
24
+
25
+ # Each.
26
+ #
27
+ # @param block [Block]
28
+ def each(&block)
29
+ @values.each(&block)
30
+ end
31
+
32
+ # Get more records.
33
+ #
34
+ # @return [Array] parsed records.
35
+ def more
36
+ if !has_more?
37
+ return nil
38
+ end
39
+
40
+ @response = Square.make_request(url: @next_link)
41
+ self.class.new(@response, @data_type)
42
+ end
43
+
44
+ # Check if there are more pages.
45
+ #
46
+ # @return [Boolean]
47
+ def has_more?
48
+ !@next_link.nil?
49
+ end
50
+
51
+ # Pass through methods to the original response object.
52
+ def method_missing(name, *args, &block)
53
+ @response.send(name, *args, &block)
54
+ end
55
+
56
+ private
57
+
58
+ # Parse a response.
59
+ #
60
+ # @return [Array] parsed records.
61
+ def parse_response(response)
62
+ # Detect a Link header.
63
+ if !response.headers[:link].nil?
64
+ match = LINK_REGEXP.match(response.headers[:link])
65
+
66
+ if match.nil?
67
+ @next_link = nil
68
+ else
69
+ @next_link = match.captures[0]
70
+ end
71
+ else
72
+ @next_link = nil
73
+ end
74
+
75
+ parsed_response = JSON.parse(response)
76
+ @values = parsed_response.map {|record| @data_type.new(record)}
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,13 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-merchant
3
+ class Merchant < APIResource
4
+ extend Square::APIOperations::Retrieve
5
+
6
+ data_type Square::DataTypes::Merchant
7
+
8
+ def self.retrieve(merchant_id = nil)
9
+ response = Square.get(merchant_id)
10
+ @data_type.new(response)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-payments
3
+ class Payment < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Retrieve
6
+
7
+ endpoint_base 'payments'
8
+ data_type Square::DataTypes::Payment
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-refunds
3
+ class Refund < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Create
6
+
7
+ endpoint_base 'refunds'
8
+ data_type Square::DataTypes::Refund
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-settlements
3
+ class Settlement < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Retrieve
6
+
7
+ endpoint_base 'settlements'
8
+ data_type Square::DataTypes::Settlement
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-variations
3
+ class Variation < APIResource
4
+ extend Square::APIOperations::Create
5
+ extend Square::APIOperations::Update
6
+ extend Square::APIOperations::Delete
7
+
8
+ endpoint_base 'variations'
9
+ nested_under 'items'
10
+ data_type Square::DataTypes::ItemVariation
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Square
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-webhooks
3
+ class Webhook < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Update
6
+
7
+ endpoint_base 'webhooks'
8
+ data_type Array
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'square/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'square-ruby'
8
+ spec.version = Square::VERSION
9
+ spec.authors = ['Brian Wm. McAllister', 'David Michael', 'Brianne King']
10
+ spec.email = ['brian.mcallister@giantmachines.com', 'david.michael@giantmachines.com', 'brianne.king@giantmachines.com']
11
+
12
+ spec.summary = 'Square Connect API library'
13
+ spec.description = 'Square Connect API'
14
+ spec.homepage = 'https://github.com/giantmachines/square-ruby'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ # spec.bindir = "exe"
19
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.3'
25
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
26
+ spec.add_development_dependency 'awesome_print', '~> 1.6'
27
+
28
+ spec.add_runtime_dependency 'hashie', '~> 3.4'
29
+ spec.add_runtime_dependency 'rest-client', '~> 1.8'
30
+ spec.add_runtime_dependency 'multipart-post', '~> 2.0'
31
+ end