square.rb 8.0.0.20201216 → 26.1.0.20230119
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.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +79 -221
- data/lib/square/api/apple_pay_api.rb +15 -8
- data/lib/square/api/bank_accounts_api.rb +5 -5
- data/lib/square/api/base_api.rb +27 -9
- data/lib/square/api/booking_custom_attributes_api.rb +555 -0
- data/lib/square/api/bookings_api.rb +107 -15
- data/lib/square/api/cards_api.rb +171 -0
- data/lib/square/api/cash_drawers_api.rb +2 -2
- data/lib/square/api/catalog_api.rb +167 -73
- data/lib/square/api/checkout_api.rb +205 -3
- data/lib/square/api/customer_custom_attributes_api.rb +561 -0
- data/lib/square/api/customer_groups_api.rb +17 -8
- data/lib/square/api/customer_segments_api.rb +15 -6
- data/lib/square/api/customers_api.rb +67 -33
- data/lib/square/api/devices_api.rb +3 -2
- data/lib/square/api/disputes_api.rb +109 -105
- data/lib/square/api/gift_card_activities_api.rb +132 -0
- data/lib/square/api/gift_cards_api.rb +298 -0
- data/lib/square/api/inventory_api.rb +263 -24
- data/lib/square/api/invoices_api.rb +21 -21
- data/lib/square/api/labor_api.rb +70 -68
- data/lib/square/api/location_custom_attributes_api.rb +584 -0
- data/lib/square/api/locations_api.rb +21 -14
- data/lib/square/api/loyalty_api.rb +333 -50
- data/lib/square/api/merchants_api.rb +11 -9
- data/lib/square/api/mobile_authorization_api.rb +4 -4
- data/lib/square/api/o_auth_api.rb +78 -25
- data/lib/square/api/order_custom_attributes_api.rb +601 -0
- data/lib/square/api/orders_api.rb +84 -45
- data/lib/square/api/payments_api.rb +72 -24
- data/lib/square/api/payouts_api.rb +173 -0
- data/lib/square/api/refunds_api.rb +18 -7
- data/lib/square/api/sites_api.rb +43 -0
- data/lib/square/api/snippets_api.rb +146 -0
- data/lib/square/api/subscriptions_api.rb +190 -15
- data/lib/square/api/team_api.rb +46 -46
- data/lib/square/api/terminal_api.rb +172 -22
- data/lib/square/api/transactions_api.rb +15 -191
- data/lib/square/api/v1_transactions_api.rb +52 -124
- data/lib/square/api/vendors_api.rb +257 -0
- data/lib/square/api/webhook_subscriptions_api.rb +327 -0
- data/lib/square/api_helper.rb +217 -57
- data/lib/square/client.rb +90 -18
- data/lib/square/configuration.rb +64 -20
- data/lib/square/exceptions/validation_exception.rb +13 -0
- data/lib/square/http/api_response.rb +7 -9
- data/lib/square/http/faraday_client.rb +40 -9
- data/lib/square/http/http_client.rb +31 -12
- data/lib/square/http/http_request.rb +6 -2
- data/lib/square/utilities/date_time_helper.rb +151 -0
- data/lib/square/utilities/file_wrapper.rb +1 -2
- data/lib/square.rb +56 -44
- data/test/api/test_locations_api.rb +2 -5
- data/test/test_helper.rb +2 -2
- metadata +83 -15
- data/lib/square/api/v1_employees_api.rb +0 -751
- data/lib/square/api/v1_items_api.rb +0 -1766
data/lib/square/configuration.rb
CHANGED
@@ -3,14 +3,9 @@ module Square
|
|
3
3
|
# are configured in this class.
|
4
4
|
class Configuration
|
5
5
|
# The attribute readers for properties.
|
6
|
-
attr_reader :http_client
|
7
|
-
|
8
|
-
|
9
|
-
attr_reader :retry_interval
|
10
|
-
attr_reader :backoff_factor
|
11
|
-
attr_reader :environment
|
12
|
-
attr_reader :square_version
|
13
|
-
attr_reader :access_token
|
6
|
+
attr_reader :http_client, :connection, :adapter, :timeout, :max_retries, :retry_interval,
|
7
|
+
:backoff_factor, :retry_statuses, :retry_methods, :environment, :custom_url,
|
8
|
+
:square_version, :access_token, :user_agent_detail
|
14
9
|
|
15
10
|
def additional_headers
|
16
11
|
@additional_headers.clone
|
@@ -20,10 +15,19 @@ module Square
|
|
20
15
|
attr_reader :environments
|
21
16
|
end
|
22
17
|
|
23
|
-
def initialize(
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
def initialize(connection: nil, adapter: :net_http_persistent, timeout: 60,
|
19
|
+
max_retries: 0, retry_interval: 1, backoff_factor: 2,
|
20
|
+
retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
|
21
|
+
retry_methods: %i[get put], environment: 'production',
|
22
|
+
custom_url: 'https://connect.squareup.com',
|
23
|
+
square_version: '2023-01-19', access_token: '',
|
24
|
+
user_agent_detail: '', additional_headers: {})
|
25
|
+
# The Faraday connection object passed by the SDK user for making requests
|
26
|
+
@connection = connection
|
27
|
+
|
28
|
+
# The Faraday adapter object passed by the SDK user for performing http requests
|
29
|
+
@adapter = adapter
|
30
|
+
|
27
31
|
# The value to use for connection timeout
|
28
32
|
@timeout = timeout
|
29
33
|
|
@@ -37,9 +41,18 @@ module Square
|
|
37
41
|
# by in order to provide backoff
|
38
42
|
@backoff_factor = backoff_factor
|
39
43
|
|
44
|
+
# A list of HTTP statuses to retry
|
45
|
+
@retry_statuses = retry_statuses
|
46
|
+
|
47
|
+
# A list of HTTP methods to retry
|
48
|
+
@retry_methods = retry_methods
|
49
|
+
|
40
50
|
# Current API environment
|
41
51
|
@environment = String(environment)
|
42
52
|
|
53
|
+
# Sets the base URL requests are made to. Defaults to `https://connect.squareup.com`
|
54
|
+
@custom_url = custom_url
|
55
|
+
|
43
56
|
# Square Connect API versions
|
44
57
|
@square_version = square_version
|
45
58
|
|
@@ -51,33 +64,56 @@ module Square
|
|
51
64
|
|
52
65
|
# The Http Client to use for making requests.
|
53
66
|
@http_client = create_http_client
|
67
|
+
|
68
|
+
# User agent detail, to be appended with user-agent header.
|
69
|
+
@user_agent_detail = get_user_agent(user_agent_detail)
|
54
70
|
end
|
55
71
|
|
56
|
-
def clone_with(
|
57
|
-
|
58
|
-
|
72
|
+
def clone_with(connection: nil, adapter: nil, timeout: nil,
|
73
|
+
max_retries: nil, retry_interval: nil, backoff_factor: nil,
|
74
|
+
retry_statuses: nil, retry_methods: nil, environment: nil,
|
75
|
+
custom_url: nil, square_version: nil, access_token: nil,
|
76
|
+
user_agent_detail: nil, additional_headers: nil)
|
77
|
+
connection ||= self.connection
|
78
|
+
adapter ||= self.adapter
|
59
79
|
timeout ||= self.timeout
|
60
80
|
max_retries ||= self.max_retries
|
61
81
|
retry_interval ||= self.retry_interval
|
62
82
|
backoff_factor ||= self.backoff_factor
|
83
|
+
retry_statuses ||= self.retry_statuses
|
84
|
+
retry_methods ||= self.retry_methods
|
63
85
|
environment ||= self.environment
|
86
|
+
custom_url ||= self.custom_url
|
64
87
|
square_version ||= self.square_version
|
65
88
|
access_token ||= self.access_token
|
89
|
+
user_agent_detail ||= self.user_agent_detail
|
66
90
|
additional_headers ||= self.additional_headers
|
67
91
|
|
68
|
-
Configuration.new(
|
92
|
+
Configuration.new(connection: connection, adapter: adapter,
|
93
|
+
timeout: timeout, max_retries: max_retries,
|
69
94
|
retry_interval: retry_interval,
|
70
95
|
backoff_factor: backoff_factor,
|
71
|
-
|
72
|
-
|
96
|
+
retry_statuses: retry_statuses,
|
97
|
+
retry_methods: retry_methods, environment: environment,
|
98
|
+
custom_url: custom_url, square_version: square_version,
|
73
99
|
access_token: access_token,
|
100
|
+
user_agent_detail: user_agent_detail,
|
74
101
|
additional_headers: additional_headers)
|
75
102
|
end
|
76
103
|
|
77
104
|
def create_http_client
|
78
105
|
FaradayClient.new(timeout: timeout, max_retries: max_retries,
|
79
106
|
retry_interval: retry_interval,
|
80
|
-
backoff_factor: backoff_factor
|
107
|
+
backoff_factor: backoff_factor,
|
108
|
+
retry_statuses: retry_statuses,
|
109
|
+
retry_methods: retry_methods, connection: connection,
|
110
|
+
adapter: adapter)
|
111
|
+
end
|
112
|
+
|
113
|
+
def get_user_agent(user_agent_detail)
|
114
|
+
raise ArgumentError, 'The length of user-agent detail should not exceed 128 characters.' unless user_agent_detail.length < 128
|
115
|
+
|
116
|
+
user_agent_detail
|
81
117
|
end
|
82
118
|
|
83
119
|
# All the environments the SDK can run in.
|
@@ -87,6 +123,9 @@ module Square
|
|
87
123
|
},
|
88
124
|
'sandbox' => {
|
89
125
|
'default' => 'https://connect.squareupsandbox.com'
|
126
|
+
},
|
127
|
+
'custom' => {
|
128
|
+
'default' => '{custom_url}'
|
90
129
|
}
|
91
130
|
}.freeze
|
92
131
|
|
@@ -95,7 +134,12 @@ module Square
|
|
95
134
|
# required.
|
96
135
|
# @return [String] The base URI.
|
97
136
|
def get_base_uri(server = 'default')
|
98
|
-
|
137
|
+
parameters = {
|
138
|
+
'custom_url' => { 'value' => custom_url, 'encode' => false }
|
139
|
+
}
|
140
|
+
APIHelper.append_url_with_template_parameters(
|
141
|
+
ENVIRONMENTS[environment][server], parameters
|
142
|
+
)
|
99
143
|
end
|
100
144
|
end
|
101
145
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Square
|
2
|
+
# Class for exceptions when there is a schema validation error.
|
3
|
+
class ValidationException < StandardError
|
4
|
+
attr_reader :reason
|
5
|
+
|
6
|
+
# The constructor.
|
7
|
+
# @param [String] The reason for raising an exception.
|
8
|
+
def initialize(value, template)
|
9
|
+
@reason = "The value #{value} provided doesn't validate against the schema #{template}"
|
10
|
+
super(reason)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -18,16 +18,14 @@ module Square
|
|
18
18
|
@request = http_response.request
|
19
19
|
@errors = errors
|
20
20
|
|
21
|
-
if data.is_a? Hash
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end.new(*data.values)
|
21
|
+
if (data.is_a? Hash) && data.keys.any?
|
22
|
+
@body = Struct.new(*data.keys) do
|
23
|
+
define_method(:to_s) { http_response.raw_body }
|
24
|
+
end.new(*data.values)
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
26
|
+
@cursor = data.fetch(:cursor, nil)
|
27
|
+
data.reject! { |k| %i[cursor errors].include?(k) }
|
28
|
+
@data = Struct.new(*data.keys).new(*data.values) if data.keys.any?
|
31
29
|
else
|
32
30
|
@data = data
|
33
31
|
@body = data
|
@@ -1,25 +1,52 @@
|
|
1
1
|
require 'faraday/http_cache'
|
2
|
-
require '
|
2
|
+
require 'faraday/retry'
|
3
|
+
require 'faraday/multipart'
|
4
|
+
require 'faraday/follow_redirects'
|
5
|
+
require 'faraday/gzip'
|
6
|
+
require 'faraday/net_http_persistent'
|
3
7
|
|
4
8
|
module Square
|
5
9
|
# An implementation of HttpClient.
|
6
10
|
class FaradayClient < HttpClient
|
11
|
+
# The attribute readers for properties.
|
12
|
+
attr_reader :connection
|
13
|
+
|
7
14
|
# The constructor.
|
8
15
|
def initialize(timeout:, max_retries:, retry_interval:,
|
9
|
-
backoff_factor:,
|
10
|
-
|
16
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
17
|
+
connection:, adapter:, cache: false, verify: true)
|
18
|
+
@connection = if connection.nil?
|
19
|
+
create_connection(timeout: timeout, max_retries: max_retries,
|
20
|
+
retry_interval: retry_interval, backoff_factor: backoff_factor,
|
21
|
+
retry_statuses: retry_statuses, retry_methods: retry_methods,
|
22
|
+
adapter: adapter, cache: cache, verify: verify)
|
23
|
+
else
|
24
|
+
connection
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Method to initialize connection.
|
29
|
+
def create_connection(timeout:, max_retries:, retry_interval:,
|
30
|
+
backoff_factor:, retry_statuses:, retry_methods:,
|
31
|
+
adapter:, cache: false, verify: true)
|
32
|
+
Faraday.new do |faraday|
|
11
33
|
faraday.use Faraday::HttpCache, serializer: Marshal if cache
|
12
|
-
faraday.use
|
13
|
-
faraday.
|
34
|
+
faraday.use Faraday::FollowRedirects::Middleware
|
35
|
+
faraday.request :gzip
|
14
36
|
faraday.request :multipart
|
15
37
|
faraday.request :url_encoded
|
16
38
|
faraday.ssl[:ca_file] = Certifi.where
|
17
39
|
faraday.ssl[:verify] = verify
|
18
40
|
faraday.request :retry, max: max_retries, interval: retry_interval,
|
19
|
-
backoff_factor: backoff_factor
|
20
|
-
|
41
|
+
backoff_factor: backoff_factor,
|
42
|
+
retry_statuses: retry_statuses,
|
43
|
+
methods: retry_methods,
|
44
|
+
retry_if: proc { |env, _exc|
|
45
|
+
env.request.context['forced_retry'] ||= false
|
46
|
+
}
|
47
|
+
faraday.adapter adapter
|
21
48
|
faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
|
22
|
-
faraday.options[:timeout] = timeout if timeout
|
49
|
+
faraday.options[:timeout] = timeout if timeout.positive?
|
23
50
|
end
|
24
51
|
end
|
25
52
|
|
@@ -29,7 +56,9 @@ module Square
|
|
29
56
|
http_request.http_method.downcase,
|
30
57
|
http_request.query_url
|
31
58
|
) do |request|
|
32
|
-
request.headers = http_request.headers
|
59
|
+
request.headers = http_request.headers.map { |k, v| [k.to_s, v.to_s] }
|
60
|
+
request.options.context ||= {}
|
61
|
+
request.options.context.merge!(http_request.context)
|
33
62
|
unless http_request.http_method == HttpMethodEnum::GET &&
|
34
63
|
http_request.parameters.empty?
|
35
64
|
request.body = http_request.parameters
|
@@ -45,6 +74,8 @@ module Square
|
|
45
74
|
http_request.query_url
|
46
75
|
) do |request|
|
47
76
|
request.headers = http_request.headers
|
77
|
+
request.options.context ||= {}
|
78
|
+
request.options.context.merge!(http_request.context)
|
48
79
|
unless http_request.http_method == HttpMethodEnum::GET &&
|
49
80
|
http_request.parameters.empty?
|
50
81
|
request.body = http_request.parameters
|
@@ -28,72 +28,91 @@ module Square
|
|
28
28
|
# Get a GET HttpRequest object.
|
29
29
|
# @param [String] The URL to send the request to.
|
30
30
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
31
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
31
32
|
def get(query_url,
|
32
|
-
headers: {}
|
33
|
+
headers: {},
|
34
|
+
context: {})
|
33
35
|
HttpRequest.new(HttpMethodEnum::GET,
|
34
36
|
query_url,
|
35
|
-
headers: headers
|
37
|
+
headers: headers,
|
38
|
+
context: context)
|
36
39
|
end
|
37
40
|
|
38
41
|
# Get a HEAD HttpRequest object.
|
39
42
|
# @param [String] The URL to send the request to.
|
40
43
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
44
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
41
45
|
def head(query_url,
|
42
|
-
headers: {}
|
46
|
+
headers: {},
|
47
|
+
context: {})
|
43
48
|
HttpRequest.new(HttpMethodEnum::HEAD,
|
44
49
|
query_url,
|
45
|
-
headers: headers
|
50
|
+
headers: headers,
|
51
|
+
context: context)
|
46
52
|
end
|
47
53
|
|
48
54
|
# Get a POST HttpRequest object.
|
49
55
|
# @param [String] The URL to send the request to.
|
50
56
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
51
57
|
# @param [Hash, Optional] The parameters for the HTTP Request.
|
58
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
52
59
|
def post(query_url,
|
53
60
|
headers: {},
|
54
|
-
parameters: {}
|
61
|
+
parameters: {},
|
62
|
+
context: {})
|
55
63
|
HttpRequest.new(HttpMethodEnum::POST,
|
56
64
|
query_url,
|
57
65
|
headers: headers,
|
58
|
-
parameters: parameters
|
66
|
+
parameters: parameters,
|
67
|
+
context: context)
|
59
68
|
end
|
60
69
|
|
61
70
|
# Get a PUT HttpRequest object.
|
62
71
|
# @param [String] The URL to send the request to.
|
63
72
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
64
73
|
# @param [Hash, Optional] The parameters for the HTTP Request.
|
74
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
65
75
|
def put(query_url,
|
66
76
|
headers: {},
|
67
|
-
parameters: {}
|
77
|
+
parameters: {},
|
78
|
+
context: {})
|
68
79
|
HttpRequest.new(HttpMethodEnum::PUT,
|
69
80
|
query_url,
|
70
81
|
headers: headers,
|
71
|
-
parameters: parameters
|
82
|
+
parameters: parameters,
|
83
|
+
context: context)
|
72
84
|
end
|
73
85
|
|
74
86
|
# Get a PATCH HttpRequest object.
|
75
87
|
# @param [String] The URL to send the request to.
|
76
88
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
77
89
|
# @param [Hash, Optional] The parameters for the HTTP Request.
|
90
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
78
91
|
def patch(query_url,
|
79
92
|
headers: {},
|
80
|
-
parameters: {}
|
93
|
+
parameters: {},
|
94
|
+
context: {})
|
81
95
|
HttpRequest.new(HttpMethodEnum::PATCH,
|
82
96
|
query_url,
|
83
97
|
headers: headers,
|
84
|
-
parameters: parameters
|
98
|
+
parameters: parameters,
|
99
|
+
context: context)
|
85
100
|
end
|
86
101
|
|
87
102
|
# Get a DELETE HttpRequest object.
|
88
103
|
# @param [String] The URL to send the request to.
|
89
104
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
105
|
+
# @param [Hash, Optional] The parameters for the HTTP Request.
|
106
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
90
107
|
def delete(query_url,
|
91
108
|
headers: {},
|
92
|
-
parameters:
|
109
|
+
parameters: nil,
|
110
|
+
context: {})
|
93
111
|
HttpRequest.new(HttpMethodEnum::DELETE,
|
94
112
|
query_url,
|
95
113
|
headers: headers,
|
96
|
-
parameters: parameters
|
114
|
+
parameters: parameters,
|
115
|
+
context: context)
|
97
116
|
end
|
98
117
|
end
|
99
118
|
end
|
@@ -2,21 +2,25 @@ module Square
|
|
2
2
|
# Represents a single Http Request.
|
3
3
|
class HttpRequest
|
4
4
|
attr_accessor :http_method, :query_url, :headers,
|
5
|
-
:parameters, :username, :password
|
5
|
+
:parameters, :username, :password,
|
6
|
+
:context
|
6
7
|
|
7
8
|
# The constructor.
|
8
9
|
# @param [HttpMethodEnum] The HTTP method.
|
9
10
|
# @param [String] The URL to send the request to.
|
10
11
|
# @param [Hash, Optional] The headers for the HTTP Request.
|
11
12
|
# @param [Hash, Optional] The parameters for the HTTP Request.
|
13
|
+
# @param [Hash, Optional] The context for the HTTP Request.
|
12
14
|
def initialize(http_method,
|
13
15
|
query_url,
|
14
16
|
headers: {},
|
15
|
-
parameters: {}
|
17
|
+
parameters: {},
|
18
|
+
context: {})
|
16
19
|
@http_method = http_method
|
17
20
|
@query_url = query_url
|
18
21
|
@headers = headers
|
19
22
|
@parameters = parameters
|
23
|
+
@context = context
|
20
24
|
end
|
21
25
|
|
22
26
|
# Add a header to the HttpRequest.
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'date'
|
2
|
+
module Square
|
3
|
+
# A utility that supports dateTime conversion to different formats
|
4
|
+
class DateTimeHelper
|
5
|
+
# Safely converts a DateTime object into a rfc1123 format string
|
6
|
+
# @param [DateTime] The DateTime object
|
7
|
+
# @return [String] The rfc1123 formatted datetime string
|
8
|
+
def self.to_rfc1123(date_time)
|
9
|
+
date_time&.httpdate
|
10
|
+
end
|
11
|
+
|
12
|
+
# Safely converts a map of DateTime objects into a map of rfc1123 format string
|
13
|
+
# @param [hash] a map of DateTime objects
|
14
|
+
# @return [hash] a map of rfc1123 formatted datetime string
|
15
|
+
def self.to_rfc1123_map(date_time, hash, key)
|
16
|
+
return if date_time.nil?
|
17
|
+
|
18
|
+
hash[key] = {}
|
19
|
+
date_time.each do |k, v|
|
20
|
+
hash[key][k] =
|
21
|
+
if v.is_a?(BaseModel)
|
22
|
+
v.to_hash
|
23
|
+
else
|
24
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
|
25
|
+
end
|
26
|
+
end
|
27
|
+
hash[key]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Safely converts an array of DateTime objects into an array of rfc1123 format string
|
31
|
+
# @param [Array] an array of DateTime objects
|
32
|
+
# @return [Array] an array of rfc1123 formatted datetime string
|
33
|
+
def self.to_rfc1123_array(date_time, hash, key)
|
34
|
+
return if date_time.nil?
|
35
|
+
|
36
|
+
hash[key] = date_time.map do |v|
|
37
|
+
if v.is_a?(BaseModel)
|
38
|
+
v.to_hash
|
39
|
+
else
|
40
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc1123(v) : v
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Safely converts a DateTime object into a unix format string
|
46
|
+
# @param [DateTime] The DateTime object
|
47
|
+
# @return [String] The unix formatted datetime string
|
48
|
+
def self.to_unix(date_time)
|
49
|
+
date_time.to_time.utc.to_i unless date_time.nil?
|
50
|
+
end
|
51
|
+
|
52
|
+
# Safely converts a map of DateTime objects into a map of unix format string
|
53
|
+
# @param [hash] a map of DateTime objects
|
54
|
+
# @return [hash] a map of unix formatted datetime string
|
55
|
+
def self.to_unix_map(date_time, hash, key)
|
56
|
+
return if date_time.nil?
|
57
|
+
|
58
|
+
hash[key] = {}
|
59
|
+
date_time.each do |k, v|
|
60
|
+
hash[key][k] =
|
61
|
+
if v.is_a?(BaseModel)
|
62
|
+
v.to_hash
|
63
|
+
else
|
64
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
|
65
|
+
end
|
66
|
+
end
|
67
|
+
hash[key]
|
68
|
+
end
|
69
|
+
|
70
|
+
# Safely converts an array of DateTime objects into a map of unix format string
|
71
|
+
# @param [hash] an array of DateTime objects
|
72
|
+
# @return [hash] an array of unix formatted datetime string
|
73
|
+
def self.to_unix_array(date_time, hash, key)
|
74
|
+
return if date_time.nil?
|
75
|
+
|
76
|
+
hash[key] = date_time.map do |v|
|
77
|
+
if v.is_a?(BaseModel)
|
78
|
+
v.to_hash
|
79
|
+
else
|
80
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_unix(v) : v
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Safely converts a DateTime object into a rfc3339 format string
|
86
|
+
# @param [DateTime] The DateTime object
|
87
|
+
# @return [String] The rfc3339 formatted datetime string
|
88
|
+
def self.to_rfc3339(date_time)
|
89
|
+
date_time&.rfc3339
|
90
|
+
end
|
91
|
+
|
92
|
+
# Safely converts a map of DateTime objects into a map of rfc1123 format string
|
93
|
+
# @param [hash] a map of DateTime objects
|
94
|
+
# @return [hash] a map of rfc1123 formatted datetime string
|
95
|
+
def self.to_rfc3339_map(date_time, hash, key)
|
96
|
+
return if date_time.nil?
|
97
|
+
|
98
|
+
hash[key] = {}
|
99
|
+
date_time.each do |k, v|
|
100
|
+
hash[key][k] =
|
101
|
+
if v.is_a?(BaseModel)
|
102
|
+
v.to_hash
|
103
|
+
else
|
104
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
|
105
|
+
end
|
106
|
+
end
|
107
|
+
hash[key]
|
108
|
+
end
|
109
|
+
|
110
|
+
# Safely converts an array of DateTime objects into an array of rfc1123 format string
|
111
|
+
# @param [Array] an array of DateTime objects
|
112
|
+
# @return [Array] an array of rfc1123 formatted datetime string
|
113
|
+
def self.to_rfc3339_array(date_time, hash, key)
|
114
|
+
return if date_time.nil?
|
115
|
+
|
116
|
+
hash[key] = date_time.map do |v|
|
117
|
+
if v.is_a?(BaseModel)
|
118
|
+
v.to_hash
|
119
|
+
else
|
120
|
+
v.is_a?(DateTime) ? DateTimeHelper.to_rfc3339(v) : v
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Safely converts a rfc1123 format string into a DateTime object
|
126
|
+
# @param [String] The rfc1123 formatted datetime string
|
127
|
+
# @return [DateTime] A DateTime object
|
128
|
+
def self.from_rfc1123(date_time)
|
129
|
+
DateTime.httpdate(date_time)
|
130
|
+
end
|
131
|
+
|
132
|
+
# Safely converts a unix format string into a DateTime object
|
133
|
+
# @param [String] The unix formatted datetime string
|
134
|
+
# @return [DateTime] A DateTime object
|
135
|
+
def self.from_unix(date_time)
|
136
|
+
Time.at(date_time.to_i).utc.to_datetime
|
137
|
+
end
|
138
|
+
|
139
|
+
# Safely converts a rfc3339 format string into a DateTime object
|
140
|
+
# @param [String] The rfc3339 formatted datetime string
|
141
|
+
# @return [DateTime] A DateTime object
|
142
|
+
def self.from_rfc3339(date_time)
|
143
|
+
# missing timezone information
|
144
|
+
if date_time.end_with?('Z') || date_time.index('+')
|
145
|
+
DateTime.rfc3339(date_time)
|
146
|
+
else
|
147
|
+
DateTime.rfc3339("#{date_time}Z")
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|