fangkuai.rb 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 (61) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +10 -0
  3. data/README.md +1 -0
  4. data/lib/square.rb +61 -0
  5. data/lib/square/api/apple_pay_api.rb +50 -0
  6. data/lib/square/api/bank_accounts_api.rb +136 -0
  7. data/lib/square/api/base_api.rb +43 -0
  8. data/lib/square/api/cash_drawers_api.rb +150 -0
  9. data/lib/square/api/catalog_api.rb +572 -0
  10. data/lib/square/api/checkout_api.rb +49 -0
  11. data/lib/square/api/customer_groups_api.rb +182 -0
  12. data/lib/square/api/customer_segments_api.rb +78 -0
  13. data/lib/square/api/customers_api.rb +418 -0
  14. data/lib/square/api/devices_api.rb +120 -0
  15. data/lib/square/api/disputes_api.rb +398 -0
  16. data/lib/square/api/employees_api.rb +87 -0
  17. data/lib/square/api/inventory_api.rb +296 -0
  18. data/lib/square/api/invoices_api.rb +358 -0
  19. data/lib/square/api/labor_api.rb +630 -0
  20. data/lib/square/api/locations_api.rb +151 -0
  21. data/lib/square/api/loyalty_api.rb +543 -0
  22. data/lib/square/api/merchants_api.rb +83 -0
  23. data/lib/square/api/mobile_authorization_api.rb +52 -0
  24. data/lib/square/api/o_auth_api.rb +163 -0
  25. data/lib/square/api/orders_api.rb +280 -0
  26. data/lib/square/api/payments_api.rb +279 -0
  27. data/lib/square/api/refunds_api.rb +145 -0
  28. data/lib/square/api/subscriptions_api.rb +251 -0
  29. data/lib/square/api/team_api.rb +326 -0
  30. data/lib/square/api/terminal_api.rb +141 -0
  31. data/lib/square/api/transactions_api.rb +369 -0
  32. data/lib/square/api/v1_employees_api.rb +723 -0
  33. data/lib/square/api/v1_items_api.rb +1686 -0
  34. data/lib/square/api/v1_locations_api.rb +65 -0
  35. data/lib/square/api/v1_transactions_api.rb +572 -0
  36. data/lib/square/api_helper.rb +276 -0
  37. data/lib/square/client.rb +211 -0
  38. data/lib/square/configuration.rb +101 -0
  39. data/lib/square/exceptions/api_exception.rb +15 -0
  40. data/lib/square/http/api_response.rb +45 -0
  41. data/lib/square/http/auth/o_auth2.rb +12 -0
  42. data/lib/square/http/faraday_client.rb +55 -0
  43. data/lib/square/http/http_call_back.rb +19 -0
  44. data/lib/square/http/http_client.rb +99 -0
  45. data/lib/square/http/http_method_enum.rb +8 -0
  46. data/lib/square/http/http_request.rb +45 -0
  47. data/lib/square/http/http_response.rb +24 -0
  48. data/lib/square/utilities/file_wrapper.rb +12 -0
  49. data/spec/user_journey_spec.rb +148 -0
  50. data/test/api/api_test_base.rb +24 -0
  51. data/test/api/test_catalog_api.rb +59 -0
  52. data/test/api/test_customers_api.rb +45 -0
  53. data/test/api/test_employees_api.rb +36 -0
  54. data/test/api/test_labor_api.rb +74 -0
  55. data/test/api/test_locations_api.rb +35 -0
  56. data/test/api/test_merchants_api.rb +40 -0
  57. data/test/api/test_payments_api.rb +42 -0
  58. data/test/api/test_refunds_api.rb +41 -0
  59. data/test/http_response_catcher.rb +19 -0
  60. data/test/test_helper.rb +94 -0
  61. metadata +199 -0
@@ -0,0 +1,101 @@
1
+ module Square
2
+ # All configuration including auth info and base URI for the API access
3
+ # are configured in this class.
4
+ class Configuration
5
+ # The attribute readers for properties.
6
+ attr_reader :http_client
7
+ attr_reader :timeout
8
+ attr_reader :max_retries
9
+ attr_reader :retry_interval
10
+ attr_reader :backoff_factor
11
+ attr_reader :environment
12
+ attr_reader :square_version
13
+ attr_reader :access_token
14
+
15
+ def additional_headers
16
+ @additional_headers.clone
17
+ end
18
+
19
+ class << self
20
+ attr_reader :environments
21
+ end
22
+
23
+ def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
24
+ backoff_factor: 1, environment: 'production',
25
+ square_version: '2020-08-26', access_token: 'TODO: Replace',
26
+ additional_headers: {})
27
+ # The value to use for connection timeout
28
+ @timeout = timeout
29
+
30
+ # The number of times to retry an endpoint call if it fails
31
+ @max_retries = max_retries
32
+
33
+ # Pause in seconds between retries
34
+ @retry_interval = retry_interval
35
+
36
+ # The amount to multiply each successive retry's interval amount
37
+ # by in order to provide backoff
38
+ @backoff_factor = backoff_factor
39
+
40
+ # Current API environment
41
+ @environment = String(environment)
42
+
43
+ # Square Connect API versions
44
+ @square_version = square_version
45
+
46
+ # OAuth 2.0 Access Token
47
+ @access_token = access_token
48
+
49
+ # Additional headers to add to each API request
50
+ @additional_headers = additional_headers.clone
51
+
52
+ # The Http Client to use for making requests.
53
+ @http_client = create_http_client
54
+ end
55
+
56
+ def clone_with(timeout: nil, max_retries: nil, retry_interval: nil,
57
+ backoff_factor: nil, environment: nil, square_version: nil,
58
+ access_token: nil, additional_headers: nil)
59
+ timeout ||= self.timeout
60
+ max_retries ||= self.max_retries
61
+ retry_interval ||= self.retry_interval
62
+ backoff_factor ||= self.backoff_factor
63
+ environment ||= self.environment
64
+ square_version ||= self.square_version
65
+ access_token ||= self.access_token
66
+ additional_headers ||= self.additional_headers
67
+
68
+ Configuration.new(timeout: timeout, max_retries: max_retries,
69
+ retry_interval: retry_interval,
70
+ backoff_factor: backoff_factor,
71
+ environment: environment,
72
+ square_version: square_version,
73
+ access_token: access_token,
74
+ additional_headers: additional_headers)
75
+ end
76
+
77
+ def create_http_client
78
+ FaradayClient.new(timeout: timeout, max_retries: max_retries,
79
+ retry_interval: retry_interval,
80
+ backoff_factor: backoff_factor)
81
+ end
82
+
83
+ # All the environments the SDK can run in.
84
+ ENVIRONMENTS = {
85
+ 'production' => {
86
+ 'default' => 'https://connect.squareup.com'
87
+ },
88
+ 'sandbox' => {
89
+ 'default' => 'https://connect.squareupsandbox.com'
90
+ }
91
+ }.freeze
92
+
93
+ # Generates the appropriate base URI for the environment and the server.
94
+ # @param [Configuration::Server] The server enum for which the base URI is
95
+ # required.
96
+ # @return [String] The base URI.
97
+ def get_base_uri(server = 'default')
98
+ ENVIRONMENTS[environment][server].clone
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,15 @@
1
+ module Square
2
+ # Class for exceptions when there is a network error, status code error, etc.
3
+ class APIException < StandardError
4
+ attr_reader :response, :response_code
5
+
6
+ # The constructor.
7
+ # @param [String] The reason for raising an exception.
8
+ # @param [HttpResponse] The HttpReponse of the API call.
9
+ def initialize(reason, response)
10
+ super(reason)
11
+ @response = response
12
+ @response_code = response.status_code
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+ module Square
2
+ # Http response received.
3
+ class ApiResponse
4
+ attr_reader(:status_code, :reason_phrase, :headers, :raw_body, :request,
5
+ :data, :errors, :body, :cursor)
6
+
7
+ # The constructor
8
+ # @param [HttpResponse] The original, raw response from the api.
9
+ # @param [Object] The data field specified for the response.
10
+ # @param [Array<String>] Any errors returned by the server.
11
+ def initialize(http_response,
12
+ data: nil,
13
+ errors: nil)
14
+ @status_code = http_response.status_code
15
+ @reason_phrase = http_response.reason_phrase
16
+ @headers = http_response.headers
17
+ @raw_body = http_response.raw_body
18
+ @request = http_response.request
19
+ @errors = errors
20
+
21
+ if data.is_a? Hash
22
+ if data.keys.any?
23
+ @body = Struct.new(*data.keys) do
24
+ define_method(:to_s) { http_response.raw_body }
25
+ end.new(*data.values)
26
+
27
+ @cursor = data.fetch(:cursor, nil)
28
+ data.reject! { |k| k == :cursor || k == :errors }
29
+ @data = Struct.new(*data.keys).new(*data.values) if data.keys.any?
30
+ end
31
+ else
32
+ @data = data
33
+ @body = data
34
+ end
35
+ end
36
+
37
+ def success?
38
+ status_code >= 200 && status_code < 300
39
+ end
40
+
41
+ def error?
42
+ status_code >= 400 && status_code < 600
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ # Utility class for OAuth 2 authorization and token management.
3
+ class OAuth2
4
+ # Add OAuth2 authentication to the http request.
5
+ # @param [HttpRequest] The HttpRequest object to which authentication will
6
+ # be added.
7
+ def self.apply(config, http_request)
8
+ token = config.access_token
9
+ http_request.headers['Authorization'] = "Bearer #{token}"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,55 @@
1
+ require 'faraday/http_cache'
2
+ require 'faraday_middleware'
3
+
4
+ module Square
5
+ # An implementation of HttpClient.
6
+ class FaradayClient < HttpClient
7
+ # The constructor.
8
+ def initialize(timeout:, max_retries:, retry_interval:,
9
+ backoff_factor:, cache: false, verify: true)
10
+ @connection = Faraday.new do |faraday|
11
+ faraday.use Faraday::HttpCache, serializer: Marshal if cache
12
+ faraday.use FaradayMiddleware::FollowRedirects
13
+ faraday.request :multipart
14
+ faraday.request :url_encoded
15
+ faraday.ssl[:ca_file] = Certifi.where
16
+ faraday.ssl[:verify] = verify
17
+ faraday.request :retry, max: max_retries, interval: retry_interval,
18
+ backoff_factor: backoff_factor
19
+ faraday.adapter Faraday.default_adapter
20
+ faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
21
+ faraday.options[:timeout] = timeout if timeout > 0
22
+ end
23
+ end
24
+
25
+ # Method overridden from HttpClient.
26
+ def execute_as_string(http_request)
27
+ response = @connection.send(
28
+ http_request.http_method.downcase,
29
+ http_request.query_url
30
+ ) do |request|
31
+ request.headers = http_request.headers
32
+ request.body = http_request.parameters
33
+ end
34
+ convert_response(response, http_request)
35
+ end
36
+
37
+ # Method overridden from HttpClient.
38
+ def execute_as_binary(http_request)
39
+ response = @connection.send(
40
+ http_request.http_method.downcase,
41
+ http_request.query_url
42
+ ) do |request|
43
+ request.headers = http_request.headers
44
+ request.body = http_request.parameters
45
+ end
46
+ convert_response(response, http_request)
47
+ end
48
+
49
+ # Method overridden from HttpClient.
50
+ def convert_response(response, http_request)
51
+ HttpResponse.new(response.status, response.reason_phrase,
52
+ response.headers, response.body, http_request)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ module Square
2
+ # HttpCallBack allows defining callables for pre and post API calls.
3
+ class HttpCallBack
4
+ # A controller will call this method before making an HTTP Request.
5
+ # @param [HttpRequest] The HttpRequest object which the HttpClient
6
+ # will execute.
7
+ def on_before_request(_http_request)
8
+ raise NotImplementedError, 'This method needs
9
+ to be implemented in a child class.'
10
+ end
11
+
12
+ # A controller will call this method after making an HTTP Request.
13
+ # @param [HttpResponse] The HttpReponse of the API call.
14
+ def on_after_response(_http_response)
15
+ raise NotImplementedError, 'This method needs
16
+ to be implemented in a child class.'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,99 @@
1
+ module Square
2
+ # An interface for the methods that an HTTP Client must implement.
3
+ #
4
+ # This class should not be instantiated but should be used as a base class
5
+ # for HTTP Client classes.
6
+ class HttpClient
7
+ # Execute an HttpRequest when the response is expected to be a string.
8
+ # @param [HttpRequest] The HttpRequest to be executed.
9
+ def execute_as_string(_http_request)
10
+ raise NotImplementedError, 'This method needs
11
+ to be implemented in a child class.'
12
+ end
13
+
14
+ # Execute an HttpRequest when the response is expected to be binary.
15
+ # @param [HttpRequest] The HttpRequest to be executed.
16
+ def execute_as_binary(_http_request)
17
+ raise NotImplementedError, 'This method needs
18
+ to be implemented in a child class.'
19
+ end
20
+
21
+ # Converts the HTTP Response from the client to an HttpResponse object.
22
+ # @param [Dynamic] The response object received from the client.
23
+ def convert_response(_response)
24
+ raise NotImplementedError, 'This method needs
25
+ to be implemented in a child class.'
26
+ end
27
+
28
+ # Get a GET HttpRequest object.
29
+ # @param [String] The URL to send the request to.
30
+ # @param [Hash, Optional] The headers for the HTTP Request.
31
+ def get(query_url,
32
+ headers: {})
33
+ HttpRequest.new(HttpMethodEnum::GET,
34
+ query_url,
35
+ headers: headers)
36
+ end
37
+
38
+ # Get a HEAD HttpRequest object.
39
+ # @param [String] The URL to send the request to.
40
+ # @param [Hash, Optional] The headers for the HTTP Request.
41
+ def head(query_url,
42
+ headers: {})
43
+ HttpRequest.new(HttpMethodEnum::HEAD,
44
+ query_url,
45
+ headers: headers)
46
+ end
47
+
48
+ # Get a POST HttpRequest object.
49
+ # @param [String] The URL to send the request to.
50
+ # @param [Hash, Optional] The headers for the HTTP Request.
51
+ # @param [Hash, Optional] The parameters for the HTTP Request.
52
+ def post(query_url,
53
+ headers: {},
54
+ parameters: {})
55
+ HttpRequest.new(HttpMethodEnum::POST,
56
+ query_url,
57
+ headers: headers,
58
+ parameters: parameters)
59
+ end
60
+
61
+ # Get a PUT HttpRequest object.
62
+ # @param [String] The URL to send the request to.
63
+ # @param [Hash, Optional] The headers for the HTTP Request.
64
+ # @param [Hash, Optional] The parameters for the HTTP Request.
65
+ def put(query_url,
66
+ headers: {},
67
+ parameters: {})
68
+ HttpRequest.new(HttpMethodEnum::PUT,
69
+ query_url,
70
+ headers: headers,
71
+ parameters: parameters)
72
+ end
73
+
74
+ # Get a PATCH HttpRequest object.
75
+ # @param [String] The URL to send the request to.
76
+ # @param [Hash, Optional] The headers for the HTTP Request.
77
+ # @param [Hash, Optional] The parameters for the HTTP Request.
78
+ def patch(query_url,
79
+ headers: {},
80
+ parameters: {})
81
+ HttpRequest.new(HttpMethodEnum::PATCH,
82
+ query_url,
83
+ headers: headers,
84
+ parameters: parameters)
85
+ end
86
+
87
+ # Get a DELETE HttpRequest object.
88
+ # @param [String] The URL to send the request to.
89
+ # @param [Hash, Optional] The headers for the HTTP Request.
90
+ def delete(query_url,
91
+ headers: {},
92
+ parameters: {})
93
+ HttpRequest.new(HttpMethodEnum::DELETE,
94
+ query_url,
95
+ headers: headers,
96
+ parameters: parameters)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,8 @@
1
+ module Square
2
+ # HTTP Methods Enumeration.
3
+ class HttpMethodEnum
4
+ HTTPMETHODENUM = [GET = 'GET'.freeze, POST = 'POST'.freeze,
5
+ PUT = 'PUT'.freeze, PATCH = 'PATCH'.freeze,
6
+ DELETE = 'DELETE'.freeze, HEAD = 'HEAD'.freeze].freeze
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ module Square
2
+ # Represents a single Http Request.
3
+ class HttpRequest
4
+ attr_accessor :http_method, :query_url, :headers,
5
+ :parameters, :username, :password
6
+
7
+ # The constructor.
8
+ # @param [HttpMethodEnum] The HTTP method.
9
+ # @param [String] The URL to send the request to.
10
+ # @param [Hash, Optional] The headers for the HTTP Request.
11
+ # @param [Hash, Optional] The parameters for the HTTP Request.
12
+ def initialize(http_method,
13
+ query_url,
14
+ headers: {},
15
+ parameters: {})
16
+ @http_method = http_method
17
+ @query_url = query_url
18
+ @headers = headers
19
+ @parameters = parameters
20
+ end
21
+
22
+ # Add a header to the HttpRequest.
23
+ # @param [String] The name of the header.
24
+ # @param [String] The value of the header.
25
+ def add_header(name, value)
26
+ @headers[name] = value
27
+ end
28
+
29
+ # Add a parameter to the HttpRequest.
30
+ # @param [String] The name of the parameter.
31
+ # @param [String] The value of the parameter.
32
+ def add_parameter(name, value)
33
+ @parameters[name] = value
34
+ end
35
+
36
+ # Add a query parameter to the HttpRequest.
37
+ # @param [String] The name of the query parameter.
38
+ # @param [String] The value of the query parameter.
39
+ def add_query_parameter(name, value)
40
+ @query_url = APIHelper.append_url_with_query_parameters(@query_url,
41
+ name => value)
42
+ @query_url = APIHelper.clean_url(@query_url)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ module Square
2
+ # Http response received.
3
+ class HttpResponse
4
+ attr_reader :status_code, :reason_phrase, :headers, :raw_body, :request
5
+
6
+ # The constructor
7
+ # @param [Integer] The status code returned by the server.
8
+ # @param [String] The reason phrase returned by the server.
9
+ # @param [Hash] The headers sent by the server in the response.
10
+ # @param [String] The raw body of the response.
11
+ # @param [HttpRequest] The request that resulted in this response.
12
+ def initialize(status_code,
13
+ reason_phrase,
14
+ headers,
15
+ raw_body,
16
+ request)
17
+ @status_code = status_code
18
+ @reason_phrase = reason_phrase
19
+ @headers = headers
20
+ @raw_body = raw_body
21
+ @request = request
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module Square
2
+ # A utility to allow users to set the content-type for files
3
+ class FileWrapper
4
+ attr_reader :content_type
5
+ attr_reader :file
6
+
7
+ def initialize(file, content_type: 'application/octet-stream')
8
+ @file = file
9
+ @content_type = content_type
10
+ end
11
+ end
12
+ end