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,21 @@
1
+ module Square
2
+ module APIOperations
3
+ module Retrieve
4
+ # Retrieve a resource.
5
+ #
6
+ # @param id [String] ID of the resource to retrieve.
7
+ # @param params [Hash] Hash of query params. Optional.
8
+ #
9
+ # @return [Square::DataType]
10
+ def retrieve(id, params = {})
11
+ response = Square.make_request(
12
+ endpoint: self.generate_endpoint_url(id),
13
+ params: params
14
+ )
15
+
16
+ response = Square.parse_response(response)
17
+ @data_type.new(response)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module Square
2
+ module APIOperations
3
+ module Update
4
+ # Update a resource.
5
+ #
6
+ # @param id [String] ID of the resource to update.
7
+ # @param parent_id [String] ID of the 'parent' to update. Optional.
8
+ # @param params [Hash] Payload. Optional.
9
+ #
10
+ # @return [Square::DataType]
11
+ def update(*args, params)
12
+ id, parent_id = args
13
+
14
+ response = Square.make_request(
15
+ method: 'PUT',
16
+ endpoint: self.generate_endpoint_url(id, parent_id),
17
+ payload: params
18
+ )
19
+
20
+ response = Square.parse_response(response)
21
+ @data_type.new(response)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ module Square
2
+ class APIResource
3
+ # Set a data_type property for this resource.
4
+ #
5
+ # @param data_type [Square::DataType] Data type. Optional.
6
+ #
7
+ # @return [Square::DataType]
8
+ def self.data_type(data_type = nil)
9
+ if !data_type.nil?
10
+ @data_type = data_type
11
+ end
12
+
13
+ @data_type
14
+ end
15
+
16
+ # Set an endpoint base for this resource.
17
+ #
18
+ # @param base [String] API endpoint. Optional.
19
+ #
20
+ # @return [String]
21
+ def self.endpoint_base(base = nil)
22
+ if !base.nil?
23
+ @endpoint_base = base
24
+ end
25
+
26
+ @endpoint_base
27
+ end
28
+
29
+ # Set a property for nested resources.
30
+ #
31
+ # @param parent [String] API 'parent' endpoint. Optional.
32
+ #
33
+ # @return [String]
34
+ def self.nested_under(parent = nil)
35
+ if !parent.nil?
36
+ @nested_under = parent
37
+ end
38
+
39
+ @nested_under
40
+ end
41
+
42
+ private
43
+
44
+ # Generate an endpoint based on provided arguments.
45
+ #
46
+ # @param id [String] Resource ID. Optional.
47
+ # @param parent_id [String] ID of the 'parent' resource. Optional.
48
+ #
49
+ # @return [String] Endpoint URL.
50
+ def self.generate_endpoint_url(*args)
51
+ id, parent_id = args
52
+ File.join([@nested_under, parent_id, @endpoint_base, id].compact)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-bankaccounts
3
+ class BankAccount < APIResource
4
+ extend Square::APIOperations::List
5
+ extend Square::APIOperations::Retrieve
6
+
7
+ endpoint_base 'bank-accounts'
8
+ data_type Square::DataTypes::BankAccount
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ # https://docs.connect.squareup.com/api/connect/v1/#navsection-categories
3
+ class Category < 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 'categories'
10
+ data_type Square::DataTypes::Category
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Square
2
+ class DataType < Hashie::Dash
3
+ include Hashie::Extensions::Dash::Coercion
4
+ include Hashie::Extensions::IndifferentAccess
5
+ include Hashie::Extensions::IgnoreUndeclared
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatypes
4
+ class BankAccount < Square::DataType
5
+ # The bank account's Square-issued ID.
6
+ property :id
7
+
8
+ # The Square-issued ID of the merchant associated with the bank account.
9
+ property :merchant_id
10
+
11
+ # The name of the bank that manages the account.
12
+ property :bank_name
13
+
14
+ # The name associated with the bank account.
15
+ property :name
16
+
17
+ # The bank account's type (for example, savings or checking).
18
+ property :type # BankAccount.Type
19
+
20
+ # The bank account's routing number.
21
+ property :routing_number
22
+
23
+ # The last few digits of the bank account number.
24
+ property :account_number_suffix
25
+
26
+ # The currency code of the currency associated with the bank account,
27
+ # in ISO 4217 format. For example, the currency code for US dollars
28
+ # is USD.
29
+ property :currency_code
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-category
4
+ class Category < Square::DataType
5
+ # The category's unique ID.
6
+ property :id
7
+
8
+ # The category's name.
9
+ property :name
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-coordinates
4
+ class Coordinates < Square::DataType
5
+ # The latitude coordinate, in degrees.
6
+ property :latitude # number
7
+
8
+ # The longitude coordinate, in degrees.
9
+ property :longitude # number
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-device
4
+ class Device < DataType
5
+ # The device's merchant-specified name.
6
+ property :name
7
+
8
+ # The device's Square-issued ID.
9
+ property :id
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-discount
4
+ class Discount < Square::DataType
5
+ # The discount's unique ID.
6
+ property :id
7
+
8
+ # The discount's name.
9
+ property :name
10
+
11
+ # The rate of the discount, as a string representation of a decimal
12
+ # number. A value of 0.07 corresponds to a rate of 7%. This rate is
13
+ # 0 if discount_type is VARIABLE_PERCENTAGE.
14
+ # This field is not included for amount-based discounts.
15
+ property :rate
16
+
17
+ # The amount of the discount. This amount is 0 if discount_type is
18
+ # VARIABLE_AMOUNT.
19
+ # This field is not included for rate-based discounts.
20
+ property :amount_money, coerce: Square::DataTypes::Money
21
+
22
+ # Indicates whether the discount is a FIXED value or entered at the
23
+ # time of sale.
24
+ property :discount_type # Discount.Type
25
+
26
+ # Indicates whether a mobile staff member needs to enter their PIN to
27
+ # apply the discount to a payment.
28
+ property :pin_required # boolean
29
+
30
+ # The color of the discount's display label in Square Register, if not
31
+ # the default color.
32
+ # The default color is 9da2a6.
33
+ property :color # Item.Color
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-fee
4
+ class Fee < Square::DataType
5
+ # The fee's unique ID.
6
+ property :id
7
+
8
+ # The fee's name.
9
+ property :name
10
+
11
+ # The rate of the fee, as a string representation of a decimal number.
12
+ # A value of 0.07 corresponds to a rate of 7%.
13
+ property :rate, coerce: Float
14
+
15
+ # Forthcoming.
16
+ property :calculation_phase # Fee.CalculationPhase
17
+
18
+ # The type of adjustment the fee applies to a payment. Currently,
19
+ # this value is TAX for all fees.
20
+ property :adjustment_type # Fee.AdjustmentType
21
+
22
+ # If true, the fee applies to custom amounts entered into Square
23
+ # Register that are not associated with a particular item.
24
+ property :applies_to_custom_amounts # boolean
25
+
26
+ # If true, the fee is applied to all appropriate items. If false,
27
+ # the fee is not applied at all.
28
+ property :enabled # boolean
29
+
30
+ # Whether the fee is ADDITIVE or INCLUSIVE.
31
+ property :inclusion_type # Fee.InclusionType
32
+
33
+ # In countries with multiple classifications for sales taxes, indicates
34
+ # which classification the fee falls under. Currently relevant only
35
+ # to Canadian merchants.
36
+ property :type # Fee.Type
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,67 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-globaladdress
4
+ class GlobalAddress < Square::DataType
5
+ # The first line of the address.
6
+ # Fields that start with address_line provide the address's most
7
+ # specific details, like street number, street name, and building name.
8
+ # They do not provide less specific details like city, state/province,
9
+ # or country (these details are provided in other fields).
10
+ property :address_line_1
11
+
12
+ # The second line of the address, if any.
13
+ property :address_line_2
14
+
15
+ # The third line of the address, if any.
16
+ property :address_line_3
17
+
18
+ # The fourth line of the address, if any.
19
+ property :address_line_4
20
+
21
+ # The fifth line of the address, if any.
22
+ property :address_line_5
23
+
24
+ # The city or town of the address.
25
+ property :locality
26
+
27
+ # A civil region within the address's locality, if any.
28
+ property :sublocality
29
+
30
+ # A civil region within the address's sublocality, if any.
31
+ property :sublocality_1
32
+
33
+ # A civil region within the address's sublocality_1, if any.
34
+ property :sublocality_2
35
+
36
+ # A civil region within the address's sublocality_2, if any.
37
+ property :sublocality_3
38
+
39
+ # A civil region within the address's sublocality_3, if any.
40
+ property :sublocality_4
41
+
42
+ # A civil region within the address's sublocality_4, if any.
43
+ property :sublocality_5
44
+
45
+ # A civil entity within the address's country. In the United States,
46
+ # this is the state.
47
+ property :administrative_district_level_1
48
+
49
+ # A civil entity within the address's administrative_district_level_1,
50
+ # if any. In the United States, this is the county.
51
+ property :administrative_district_level_2
52
+
53
+ # A civil entity within the address's administrative_district_level_2,
54
+ # if any.
55
+ property :administrative_district_level_3
56
+
57
+ # The address's postal code.
58
+ property :postal_code
59
+
60
+ # The address's country, in ISO 3166-1-alpha-2 format.
61
+ property :country_code
62
+
63
+ # The coordinates of the address.
64
+ property :address_coordinates, coerce: Square::DataTypes::Coordinates
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-inventoryentry
4
+ class InventoryEntry < Square::DataType
5
+ # The variation that the entry corresponds to.
6
+ property :variation_id
7
+
8
+ # The current available quantity of the item variation.
9
+ property :quantity_on_hand # number
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,79 @@
1
+ module Square
2
+ module DataTypes
3
+ # https://docs.connect.squareup.com/api/connect/v1/#datatype-item
4
+ class Item < Square::DataType
5
+ # The item's unique ID.
6
+ property :id
7
+
8
+ # The item's name.
9
+ property :name
10
+
11
+ # The item's description, if any.
12
+ property :description
13
+
14
+ # The item's type. This value is NORMAL for almost all items.
15
+ property :type # Item.Type
16
+
17
+ # The text of the item's display label in Square Register. This value
18
+ # is present only if an abbreviation other than the default has been set.
19
+ property :abbreviation
20
+
21
+ # The color of the item's display label in Square Register, if not the
22
+ # default color.
23
+ # The default color is 9da2a6.
24
+ property :color # Item.Color
25
+
26
+ # Indicates whether the item is viewable in the merchant's online
27
+ # store (PUBLIC) or PRIVATE.
28
+ property :visibility # Item.Visibility
29
+
30
+ # If true, the item is available for purchase from the merchant's
31
+ # online store.
32
+ property :available_online # boolean
33
+
34
+ # The item's master image, if any.
35
+ property :master_image, coerce: Square::DataTypes::ItemImage
36
+
37
+ # The category the item belongs to, if any.
38
+ property :category, coerce: Square::DataTypes::Category
39
+
40
+ # The item's variations.
41
+ property :variations, coerce: Array[Square::DataTypes::ItemVariation]
42
+
43
+ # The modifier lists that apply to the item, if any.
44
+ property :modifier_lists, coerce: Array[Square::DataTypes::ModifierList]
45
+
46
+ # The fees that apply to the item, if any.
47
+ property :fees, coerce: Array[Square::DataTypes::Fee]
48
+
49
+ # Deprecated. This field is not used.
50
+ property :taxable, required: false # boolean
51
+
52
+ # undocumented
53
+ property :available_for_pickup
54
+ property :images
55
+
56
+ # Get this item's variation. There should only ever be one.
57
+ #
58
+ # @return [Square::DataTypes::ItemVariation] First variation.
59
+ def variation
60
+ self.variations.first
61
+ end
62
+
63
+ def remove_fees(params = {})
64
+ fees.each do |fee|
65
+ remove_fee(fee.id, params)
66
+ end
67
+ end
68
+
69
+ def remove_fee(fee_id, params = {})
70
+ Square::Fee.remove(self.id, fee_id, params)
71
+ end
72
+
73
+ def apply_fee(fee_id, params = {})
74
+ Square::Fee.apply(self.id, fee_id, params)
75
+ end
76
+
77
+ end
78
+ end
79
+ end