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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fb70a8ab623fe06ffc5bacf8a0f73162b7ae080
4
+ data.tar.gz: f1a82cef0bd9b6cc60e3038246c03854b2db6f18
5
+ SHA512:
6
+ metadata.gz: 0de23e6cabf0cbf6bf84ae037c94f9c704ed33711ddebc11a2a739db73b2db951cbfa3067b3bf7d5246edf29ce965480327b4dbd3279a64220a6e630515eacd9
7
+ data.tar.gz: 1fc688b1c832c39ac3b587e16e68b90276e9a71e90cfbd4be334c6258a44e8b73b412b96e70c39451b15ed8c10a1caf6f0e6f2e1634106764b9653ad6cedd9a4
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2.2
5
+
6
+ before_install: gem install bundler -v 1.10.6
7
+
8
+ script:
9
+ - bundle exec rspec
10
+
11
+ addons:
12
+ code_climate:
13
+ repo_token: a5d17f579fe85df5c2f4cd7cc895c30463b763d42d48dd0e09a44adc10dd7356
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in square-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Brian McAllister
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # Square Connect API
2
+
3
+ [![Code Climate](https://codeclimate.com/github/giantmachines/square-ruby/badges/gpa.svg)](https://codeclimate.com/github/giantmachines/square-ruby) [![Test Coverage](https://codeclimate.com/github/giantmachines/square-ruby/badges/coverage.svg)](https://codeclimate.com/github/giantmachines/square-ruby/coverage) [![Build Status](https://travis-ci.org/giantmachines/square-ruby.svg?branch=master)](https://travis-ci.org/giantmachines/square-ruby)
4
+
5
+ The Square Connect API gem provides access to the Square Connect API.
6
+
7
+ TODO:
8
+
9
+ - [ ] Update RestClient.
10
+ - [ ] Add all data types.
11
+ - [ ] Add all API resources.
12
+ - [ ] OAuth.
13
+ - [x] Batching.
14
+ - [x] Handle multi location.
15
+ - [ ] Employee management.
16
+ - [ ] Register API?
17
+
18
+ ## Documentation
19
+
20
+ [Square Connect](https://docs.connect.squareup.com/)
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'square-ruby', require: 'square'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install square-ruby
37
+
38
+ ## Usage
39
+
40
+ TODO
41
+
42
+ ...but quickly, use one of the methods on one of the `APIResource` subclasses, e.g.
43
+
44
+ `items = Square::Item.list`
45
+
46
+ You'll now have an array of `Square::DataType::Item`s.
47
+
48
+ #### Paging
49
+
50
+ Paging only works for `APIResource#list`.
51
+
52
+ ```
53
+ resp = Square::Payment.list({limit: 10})
54
+ resp.each {|p| puts p.id }
55
+
56
+ puts "got #{resp.count}. getting more"
57
+
58
+ while resp.has_more?
59
+ puts "has more"
60
+ more = resp.more
61
+ more.each {|p| puts p.id }
62
+ end
63
+
64
+ puts 'done'
65
+ ```
66
+
67
+ ## Development
68
+
69
+ TODO
70
+
71
+ ## Contributing
72
+
73
+ TODO
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "square/ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,206 @@
1
+ # Libs.
2
+ require 'rest-client'
3
+ require 'hashie'
4
+ require 'open-uri'
5
+ require 'net/http/post/multipart'
6
+
7
+ # Version.
8
+ require 'square/version'
9
+
10
+ # Data types.
11
+ require 'square/data_type'
12
+ require 'square/data_types/money'
13
+ require 'square/data_types/device'
14
+ require 'square/data_types/payment_discount'
15
+ require 'square/data_types/payment_tax'
16
+ require 'square/data_types/payment_modifier'
17
+ require 'square/data_types/payment_item_detail'
18
+ require 'square/data_types/payment_itemization'
19
+ require 'square/data_types/refund'
20
+ require 'square/data_types/tender'
21
+ require 'square/data_types/payment'
22
+ require 'square/data_types/fee'
23
+ require 'square/data_types/item_image'
24
+ require 'square/data_types/category'
25
+ require 'square/data_types/item_variation'
26
+ require 'square/data_types/modifier_list'
27
+ require 'square/data_types/modifier_option'
28
+ require 'square/data_types/item'
29
+ require 'square/data_types/settlement_entry'
30
+ require 'square/data_types/settlement'
31
+ require 'square/data_types/coordinates'
32
+ require 'square/data_types/global_address'
33
+ require 'square/data_types/phone_number'
34
+ require 'square/data_types/merchant_location_details'
35
+ require 'square/data_types/merchant'
36
+ require 'square/data_types/discount'
37
+ require 'square/data_types/inventory_entry'
38
+ require 'square/data_types/bank_account'
39
+
40
+ # API Operations.
41
+ require 'square/list_response'
42
+ require 'square/api_operations/list'
43
+ require 'square/api_operations/retrieve'
44
+ require 'square/api_operations/create'
45
+ require 'square/api_operations/update'
46
+ require 'square/api_operations/delete'
47
+
48
+ # API Resources.
49
+ require 'square/api_resource'
50
+ require 'square/category'
51
+ require 'square/bank_accounts'
52
+ require 'square/discount'
53
+ require 'square/item'
54
+ require 'square/merchant'
55
+ require 'square/payment'
56
+ require 'square/inventory'
57
+ require 'square/fee'
58
+ require 'square/refund'
59
+ require 'square/settlement'
60
+ require 'square/variation'
61
+ require 'square/webhook'
62
+
63
+ RestClient.log = Logger.new(STDOUT)
64
+
65
+ module Square
66
+ @api_host = 'https://connect.squareup.com'
67
+ @access_token = nil
68
+ @api_version = 'v1'
69
+ @merchant_id = 'me'
70
+
71
+ class << self
72
+ attr_accessor :api_host, :access_token, :api_version, :merchant_id
73
+ end
74
+
75
+ # Make an API call to Square.
76
+ #
77
+ # @param options [Hash] Hash of options. Expects something like this:
78
+ #
79
+ # {
80
+ # method: 'POST',
81
+ # endpoint: 'items',
82
+ # request: {...}
83
+ # }
84
+ #
85
+ # @return [RestClient::Response]
86
+ def self.make_request(options = {}, &block)
87
+ if access_token.nil?
88
+ raise StandardError.new('No access token set.')
89
+ end
90
+
91
+ # Default to a GET request.
92
+ method = (options[:method] || :get).downcase.to_sym
93
+
94
+ # Allow passing in a fully formed URL.
95
+ if !options[:url].nil?
96
+ url = options[:url]
97
+ else
98
+ merchant = options[:merchant] || merchant_id
99
+
100
+ # Special handling of the merchant param.
101
+ if !options[:params].nil? && !options[:params][:merchant].nil?
102
+ merchant = options[:params].delete(:merchant)
103
+ end
104
+
105
+ if !options[:payload].nil? && !options[:payload][:merchant].nil?
106
+ merchant = options[:payload].delete(:merchant)
107
+ end
108
+
109
+ path_args = [api_host, api_version, merchant, options[:endpoint]].compact
110
+ url = File.join(path_args)
111
+ end
112
+
113
+ # Build up the RestClient request object.
114
+ request_params = {
115
+ headers: request_headers(access_token).merge(options[:headers] || {}),
116
+ method: method,
117
+ url: url
118
+ }
119
+
120
+ # Merge in a payload hash.
121
+ payload = options[:payload] || nil
122
+
123
+ if !payload.nil? && payload.respond_to?(:to_json) && !payload.empty?
124
+ payload = payload.to_json
125
+ request_params.merge!(payload: payload)
126
+ end
127
+
128
+ # Merge in a params hash.
129
+ params = options[:params] || nil
130
+
131
+ if !params.nil? && !params.empty?
132
+ request_params[:headers].merge!(params: params)
133
+ end
134
+
135
+ # Perform the request.
136
+ self.request(request_params, &block)
137
+ end
138
+
139
+ # Request helper. Makes testing and switching out the http client easy.
140
+ def self.request(request, &block)
141
+ RestClient::Request.execute(request, &block)
142
+ end
143
+
144
+ # Parse a response.
145
+ #
146
+ # @param response [RestClient::Response]
147
+ #
148
+ # @return [#to_json]
149
+ def self.parse_response(response)
150
+ begin
151
+ JSON.parse(response, symbolize_names: true)
152
+ rescue RestClient::Exception => e
153
+ Logger.new(STDOUT).error(e.http_body)
154
+ raise e
155
+ end
156
+ end
157
+
158
+ # {
159
+ # method: "POST",
160
+ # path: "categories",
161
+ # body: {
162
+ # "name": "Beverages"
163
+ # }
164
+ # }
165
+ def self.batch(requests = [{}], options = {}, &block)
166
+ merchant = options[:merchant] || 'me'
167
+
168
+ requests = requests.map {|request|
169
+ path = request.delete(:path)
170
+ request[:relative_path] = File.join('/', 'v1', merchant, path)
171
+ request[:access_token] = access_token
172
+ request[:request_id] ||= SecureRandom.uuid
173
+ request
174
+ }
175
+
176
+ request_params = {
177
+ headers: request_headers(access_token),
178
+ method: :post,
179
+ url: File.join(api_host, 'v1', 'batch'),
180
+ payload: {
181
+ requests: requests
182
+ }.to_json
183
+ }
184
+
185
+ begin
186
+ response = RestClient::Request.execute(request_params, &block)
187
+ JSON.parse(response)
188
+ rescue RestClient::Exception => e
189
+ Logger.new(STDOUT).error(e.http_body)
190
+ raise e
191
+ end
192
+ end
193
+
194
+ # Get the request headers.
195
+ #
196
+ # @param [String] Square access token.
197
+ #
198
+ # @return [Hash] Hash of headers.
199
+ def self.request_headers(access_token)
200
+ {
201
+ authorization: "Bearer #{access_token}",
202
+ accept: 'application/json',
203
+ content_type: 'application/json'
204
+ }
205
+ end
206
+ end
@@ -0,0 +1,30 @@
1
+ module Square
2
+ module APIOperations
3
+ module Create
4
+ # Create resource.
5
+ #
6
+ # @param parent_id [String] ID of the 'parent' to update. Optional.
7
+ # @param params [Hash] Payload. Optional.
8
+ #
9
+ # @return [DataType]
10
+ def create(*args)
11
+ if args.count == 1
12
+ parent_id = nil
13
+ params = args[0]
14
+ elsif args.count == 2
15
+ parent_id = args[0]
16
+ params = args[1]
17
+ end
18
+
19
+ response = Square.make_request(
20
+ method: 'POST',
21
+ endpoint: self.generate_endpoint_url(nil, parent_id),
22
+ payload: params
23
+ )
24
+
25
+ response = Square.parse_response(response)
26
+ @data_type.new(response)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ module Square
2
+ module APIOperations
3
+ module Delete
4
+ # Delete 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 [#to_json]
11
+ def delete(*args, params)
12
+ id, parent_id = args
13
+
14
+ response = Square.make_request(
15
+ method: 'DELETE',
16
+ endpoint: self.generate_endpoint_url(id, parent_id),
17
+ payload: params
18
+ )
19
+
20
+ Square.parse_response(response)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module Square
2
+ module APIOperations
3
+ module List
4
+ # List resource.
5
+ #
6
+ # @param params [Hash] Hash of query params. Optional.
7
+ #
8
+ # @return [Square::ListResponse]
9
+ def list(params = {})
10
+ # The list endpoint makes use of the ListResponse to handle paging.
11
+ response = Square.make_request(
12
+ method: 'GET',
13
+ endpoint: self.generate_endpoint_url,
14
+ params: params
15
+ )
16
+
17
+ Square::ListResponse.new(response, @data_type)
18
+ end
19
+ end
20
+ end
21
+ end