terminal-shop 0.1.0.pre.alpha.11 → 0.1.0.pre.alpha.13

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/terminal-shop/base_client.rb +28 -19
  3. data/lib/terminal-shop/base_model.rb +0 -2
  4. data/lib/terminal-shop/client.rb +0 -1
  5. data/lib/terminal-shop/models/order_create_params.rb +39 -0
  6. data/lib/terminal-shop/models/order_create_response.rb +20 -0
  7. data/lib/terminal-shop/models/subscription.rb +1 -42
  8. data/lib/terminal-shop/models/token.rb +6 -23
  9. data/lib/terminal-shop/pooled_net_requester.rb +72 -65
  10. data/lib/terminal-shop/resources/order.rb +25 -0
  11. data/lib/terminal-shop/resources/subscription.rb +0 -2
  12. data/lib/terminal-shop/util.rb +45 -3
  13. data/lib/terminal-shop/version.rb +1 -1
  14. data/lib/terminal-shop.rb +3 -0
  15. data/manifest.yaml +1 -0
  16. data/rbi/lib/terminal-shop/base_client.rbi +2 -1
  17. data/rbi/lib/terminal-shop/models/order_create_params.rbi +60 -0
  18. data/rbi/lib/terminal-shop/models/order_create_response.rbi +23 -0
  19. data/rbi/lib/terminal-shop/models/subscription.rbi +1 -34
  20. data/rbi/lib/terminal-shop/models/token.rbi +7 -25
  21. data/rbi/lib/terminal-shop/pooled_net_requester.rbi +7 -28
  22. data/rbi/lib/terminal-shop/resources/order.rbi +12 -0
  23. data/rbi/lib/terminal-shop/resources/subscription.rbi +0 -2
  24. data/rbi/lib/terminal-shop/util.rbi +8 -0
  25. data/rbi/lib/terminal-shop/version.rbi +1 -1
  26. data/sig/terminal-shop/base_client.rbs +2 -1
  27. data/sig/terminal-shop/models/order_create_params.rbs +32 -0
  28. data/sig/terminal-shop/models/order_create_response.rbs +18 -0
  29. data/sig/terminal-shop/models/subscription.rbs +0 -16
  30. data/sig/terminal-shop/models/token.rbs +3 -22
  31. data/sig/terminal-shop/pooled_net_requester.rbs +5 -15
  32. data/sig/terminal-shop/resources/order.rbs +11 -0
  33. data/sig/terminal-shop/resources/subscription.rbs +0 -1
  34. data/sig/terminal-shop/util.rbs +4 -0
  35. data/sig/terminal-shop/version.rbs +1 -1
  36. metadata +7 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d58dd761db47d982b5155d7be8408a23954f54addd5ccc35c52558b0f4cf115
4
- data.tar.gz: 6038f407195ab9dbc4eb9204e1db159d0ac76fa57ea6820d509b67cb7e0ee62c
3
+ metadata.gz: 4d0377b351b29fd528cf8cfac1ca0709534478162601101eff9c1cec288167ac
4
+ data.tar.gz: 9d0178a0d2193ebf30b3d87b056e9958de8f70921088c6877ac1b40c59537a2c
5
5
  SHA512:
6
- metadata.gz: dc784c472eaf5de540bc4d9f627ce9d10f6e655f6ce53283c2b88a6ffb2564ea62ac14dede92bfdc5dabaaa7a011634b5d6a6f7bf57912a1ff31f92cd9d45c75
7
- data.tar.gz: 2bd9880f38253a082afc54d9cba099b7248dfa008c4324d6e6f5c78ee7e5c783e8b4066bbed021b9095bf860f97646ad44a204d910120d13b00b91799e1d1fa8
6
+ metadata.gz: d47c80cc764a70c4d04ce0ec5f0f82c7a374976334608d0bc2579cf58db62637f0c3def0a701756aef8b81817a840c78e7e6236cc52b48e81921fbd537ddf1c0
7
+ data.tar.gz: 0daab830e1738889aa702da0b82b02b9bc5750c0b36dc7bf0e7a6c932e4430926b29047e038a335c3a3fc199b0547bd9f045e4d57b4fff7edd4370ad51896b7a
@@ -9,6 +9,17 @@ module TerminalShop
9
9
  # from whatwg fetch spec
10
10
  MAX_REDIRECTS = 20
11
11
 
12
+ # rubocop:disable Style/MutableConstant
13
+ PLATFORM_HEADERS = {
14
+ "x-stainless-arch" => TerminalShop::Util.arch,
15
+ "x-stainless-lang" => "ruby",
16
+ "x-stainless-os" => TerminalShop::Util.os,
17
+ "x-stainless-package-version" => TerminalShop::VERSION,
18
+ "x-stainless-runtime" => ::RUBY_ENGINE,
19
+ "x-stainless-runtime-version" => ::RUBY_ENGINE_VERSION
20
+ }
21
+ # rubocop:enable Style/MutableConstant
22
+
12
23
  class << self
13
24
  # @private
14
25
  #
@@ -66,8 +77,6 @@ module TerminalShop
66
77
  #
67
78
  # @option request [Object] :body
68
79
  #
69
- # @option request [Boolean] :streaming
70
- #
71
80
  # @option request [Integer] :max_retries
72
81
  #
73
82
  # @option request [Float] :timeout
@@ -149,13 +158,10 @@ module TerminalShop
149
158
  )
150
159
  @requester = TerminalShop::PooledNetRequester.new
151
160
  @headers = TerminalShop::Util.normalized_headers(
161
+ self.class::PLATFORM_HEADERS,
152
162
  {
153
- "X-Stainless-Lang" => "ruby",
154
- "X-Stainless-Package-Version" => TerminalShop::VERSION,
155
- "X-Stainless-Runtime" => RUBY_ENGINE,
156
- "X-Stainless-Runtime-Version" => RUBY_ENGINE_VERSION,
157
- "Content-Type" => "application/json",
158
- "Accept" => "application/json"
163
+ "accept" => "application/json",
164
+ "content-type" => "application/json"
159
165
  },
160
166
  headers
161
167
  )
@@ -220,10 +226,7 @@ module TerminalShop
220
226
 
221
227
  path = TerminalShop::Util.interpolate_path(uninterpolated_path)
222
228
 
223
- query = TerminalShop::Util.deep_merge(
224
- req[:query].to_h,
225
- opts[:extra_query].to_h
226
- )
229
+ query = TerminalShop::Util.deep_merge(req[:query].to_h, opts[:extra_query].to_h)
227
230
 
228
231
  headers = TerminalShop::Util.normalized_headers(
229
232
  @headers,
@@ -263,7 +266,6 @@ module TerminalShop
263
266
  url: TerminalShop::Util.join_parsed_uri(@base_url, {**req, path: path, query: query}),
264
267
  headers: headers,
265
268
  body: encoded,
266
- streaming: false,
267
269
  max_retries: opts.fetch(:max_retries, @max_retries),
268
270
  timeout: timeout
269
271
  }
@@ -306,8 +308,6 @@ module TerminalShop
306
308
  #
307
309
  # @option request [Object] :body
308
310
  #
309
- # @option request [Boolean] :streaming
310
- #
311
311
  # @option request [Integer] :max_retries
312
312
  #
313
313
  # @option request [Float] :timeout
@@ -336,14 +336,22 @@ module TerminalShop
336
336
  status = e
337
337
  end
338
338
 
339
+ # normally we want to drain the response body and reuse the HTTP session by clearing the socket buffers
340
+ # unless we hit a server error
341
+ srv_fault = (500...).include?(status)
342
+
339
343
  case status
340
344
  in ..299
341
345
  [response, stream]
342
- in 300..399 if redirect_count >= MAX_REDIRECTS
343
- message = "Failed to complete the request within #{MAX_REDIRECTS} redirects."
346
+ in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
347
+ message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
348
+
349
+ stream.each { next }
344
350
  raise TerminalShop::APIConnectionError.new(url: url, message: message)
345
351
  in 300..399
346
352
  request = self.class.follow_redirect(request, status: status, response_headers: response)
353
+
354
+ stream.each { next }
347
355
  send_request(
348
356
  request,
349
357
  redirect_count: redirect_count + 1,
@@ -358,6 +366,7 @@ module TerminalShop
358
366
  ))
359
367
  decoded = TerminalShop::Util.decode_content(response, stream: stream, suppress_error: true)
360
368
 
369
+ stream.each { srv_fault ? break : next }
361
370
  raise TerminalShop::APIStatusError.for(
362
371
  url: url,
363
372
  status: status,
@@ -367,6 +376,8 @@ module TerminalShop
367
376
  )
368
377
  in (400..) | TerminalShop::APIConnectionError
369
378
  delay = retry_delay(response, retry_count: retry_count)
379
+
380
+ stream&.each { srv_fault ? break : next }
370
381
  sleep(delay)
371
382
 
372
383
  send_request(
@@ -376,8 +387,6 @@ module TerminalShop
376
387
  send_retry_header: send_retry_header
377
388
  )
378
389
  end
379
- ensure
380
- stream&.each { break } unless status.is_a?(Integer) && status < 300
381
390
  end
382
391
 
383
392
  # @private
@@ -503,9 +503,7 @@ module TerminalShop
503
503
  end
504
504
  end
505
505
 
506
- # rubocop:disable Style/NumberedParametersLimit
507
506
  _, variant = matches.sort! { _2.first <=> _1.first }.find { |score,| !score.zero? }
508
- # rubocop:enable Style/NumberedParametersLimit
509
507
  variant.nil? ? value : TerminalShop::Converter.coerce(variant, value)
510
508
  end
511
509
 
@@ -19,7 +19,6 @@ module TerminalShop
19
19
  ENVIRONMENTS = {production: "https://api.terminal.shop/", dev: "https://api.dev.terminal.shop/"}
20
20
  # rubocop:enable Style/MutableConstant
21
21
 
22
- # Client option
23
22
  # @return [String]
24
23
  attr_reader :bearer_token
25
24
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TerminalShop
4
+ module Models
5
+ class OrderCreateParams < TerminalShop::BaseModel
6
+ # @!parse
7
+ # extend TerminalShop::RequestParameters::Converter
8
+ include TerminalShop::RequestParameters
9
+
10
+ # @!attribute address_id
11
+ # Shipping address ID.
12
+ #
13
+ # @return [String]
14
+ required :address_id, String, api_name: :addressID
15
+
16
+ # @!attribute card_id
17
+ # Card ID.
18
+ #
19
+ # @return [String]
20
+ required :card_id, String, api_name: :cardID
21
+
22
+ # @!attribute variants
23
+ # Product variants to include in the order, along with their quantities.
24
+ #
25
+ # @return [Hash{Symbol=>Integer}]
26
+ required :variants, TerminalShop::HashOf[Integer]
27
+
28
+ # @!parse
29
+ # # @param address_id [String]
30
+ # # @param card_id [String]
31
+ # # @param variants [Hash{Symbol=>Integer}]
32
+ # # @param request_options [TerminalShop::RequestOptions, Hash{Symbol=>Object}]
33
+ # #
34
+ # def initialize(address_id:, card_id:, variants:, request_options: {}, **) = super
35
+
36
+ # def initialize: (Hash | TerminalShop::BaseModel) -> void
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TerminalShop
4
+ module Models
5
+ class OrderCreateResponse < TerminalShop::BaseModel
6
+ # @!attribute data
7
+ # Order ID.
8
+ #
9
+ # @return [String]
10
+ required :data, String
11
+
12
+ # @!parse
13
+ # # @param data [String]
14
+ # #
15
+ # def initialize(data:, **) = super
16
+
17
+ # def initialize: (Hash | TerminalShop::BaseModel) -> void
18
+ end
19
+ end
20
+ end
@@ -21,12 +21,6 @@ module TerminalShop
21
21
  # @return [String]
22
22
  required :card_id, String, api_name: :cardID
23
23
 
24
- # @!attribute frequency
25
- # Frequency of the subscription.
26
- #
27
- # @return [Symbol, TerminalShop::Models::SubscriptionAPI::Frequency]
28
- required :frequency, enum: -> { TerminalShop::Models::SubscriptionAPI::Frequency }
29
-
30
24
  # @!attribute product_variant_id
31
25
  # ID of the product variant being subscribed to.
32
26
  #
@@ -65,50 +59,15 @@ module TerminalShop
65
59
  # # @param id [String]
66
60
  # # @param address_id [String]
67
61
  # # @param card_id [String]
68
- # # @param frequency [Symbol, TerminalShop::Models::SubscriptionAPI::Frequency]
69
62
  # # @param product_variant_id [String]
70
63
  # # @param quantity [Integer]
71
64
  # # @param next_ [String]
72
65
  # # @param schedule [TerminalShop::Models::SubscriptionAPI::Schedule::Fixed, TerminalShop::Models::SubscriptionAPI::Schedule::Weekly]
73
66
  # #
74
- # def initialize(id:, address_id:, card_id:, frequency:, product_variant_id:, quantity:, next_: nil, schedule: nil, **) = super
67
+ # def initialize(id:, address_id:, card_id:, product_variant_id:, quantity:, next_: nil, schedule: nil, **) = super
75
68
 
76
69
  # def initialize: (Hash | TerminalShop::BaseModel) -> void
77
70
 
78
- # @abstract
79
- #
80
- # Frequency of the subscription.
81
- #
82
- # @example
83
- # ```ruby
84
- # case frequency
85
- # in :fixed
86
- # # ...
87
- # in :daily
88
- # # ...
89
- # in :weekly
90
- # # ...
91
- # in :monthly
92
- # # ...
93
- # in :yearly
94
- # # ...
95
- # end
96
- # ```
97
- class Frequency < TerminalShop::Enum
98
- FIXED = :fixed
99
- DAILY = :daily
100
- WEEKLY = :weekly
101
- MONTHLY = :monthly
102
- YEARLY = :yearly
103
-
104
- finalize!
105
-
106
- # @!parse
107
- # # @return [Array<Symbol>]
108
- # #
109
- # def self.values; end
110
- end
111
-
112
71
  # @abstract
113
72
  #
114
73
  # Schedule of the subscription.
@@ -15,11 +15,11 @@ module TerminalShop
15
15
  # @return [String]
16
16
  required :token, String
17
17
 
18
- # @!attribute time
19
- # Relevant timestamps for the token.
18
+ # @!attribute created
19
+ # The created time for the token.
20
20
  #
21
- # @return [TerminalShop::Models::TokenAPI::Time]
22
- required :time, -> { TerminalShop::Models::TokenAPI::Time }
21
+ # @return [String]
22
+ required :created, String
23
23
 
24
24
  # @!parse
25
25
  # # A personal access token used to access the Terminal API. If you leak this,
@@ -27,28 +27,11 @@ module TerminalShop
27
27
  # #
28
28
  # # @param id [String]
29
29
  # # @param token [String]
30
- # # @param time [TerminalShop::Models::TokenAPI::Time]
30
+ # # @param created [String]
31
31
  # #
32
- # def initialize(id:, token:, time:, **) = super
32
+ # def initialize(id:, token:, created:, **) = super
33
33
 
34
34
  # def initialize: (Hash | TerminalShop::BaseModel) -> void
35
-
36
- class Time < TerminalShop::BaseModel
37
- # @!attribute created
38
- # The created time for the token.
39
- #
40
- # @return [String]
41
- required :created, String
42
-
43
- # @!parse
44
- # # Relevant timestamps for the token.
45
- # #
46
- # # @param created [String]
47
- # #
48
- # def initialize(created:, **) = super
49
-
50
- # def initialize: (Hash | TerminalShop::BaseModel) -> void
51
- end
52
35
  end
53
36
  end
54
37
  end
@@ -33,50 +33,66 @@ module TerminalShop
33
33
  # @param conn [Net::HTTP]
34
34
  # @param deadline [Float]
35
35
  #
36
- private def calibrate_socket_timeout(conn, deadline)
36
+ def calibrate_socket_timeout(conn, deadline)
37
37
  timeout = deadline - TerminalShop::Util.monotonic_secs
38
+ (conn.open_timeout = timeout) unless conn.started?
38
39
  conn.read_timeout = conn.write_timeout = conn.continue_timeout = timeout
39
40
  end
40
41
 
41
42
  # @private
42
43
  #
43
- # @param conn [Net::HTTP]
44
- # @param req [Net::HTTPGenericRequest]
45
- # @param deadline [Float]
46
- # @param blk [Proc]
44
+ # @param request [Hash{Symbol=>Object}] .
45
+ #
46
+ # @option request [Symbol] :method
47
+ #
48
+ # @option request [URI::Generic] :url
49
+ #
50
+ # @option request [Hash{String=>String}] :headers
47
51
  #
48
- def transport(conn, req, deadline, &blk)
49
- unless conn.started?
50
- conn.open_timeout = deadline - TerminalShop::Util.monotonic_secs
51
- conn.start
52
+ # @return [Net::HTTPGenericRequest]
53
+ #
54
+ def build_request(request)
55
+ method, url, headers, body = request.fetch_values(:method, :url, :headers, :body)
56
+ req = Net::HTTPGenericRequest.new(
57
+ method.to_s.upcase,
58
+ !body.nil?,
59
+ method != :head,
60
+ url.to_s
61
+ )
62
+
63
+ headers.each { req[_1] = _2 }
64
+
65
+ case body
66
+ in nil
67
+ in String
68
+ req.body = body
69
+ in StringIO
70
+ req.body = body.string
71
+ in IO
72
+ body.rewind
73
+ req.body_stream = body
52
74
  end
53
75
 
54
- calibrate_socket_timeout(conn, deadline)
55
- conn.request(req) do |rsp|
56
- blk.call(rsp)
57
- rsp.read_body do |bytes|
58
- blk.call(bytes)
59
- calibrate_socket_timeout(conn, deadline)
60
- end
61
- end
76
+ req
62
77
  end
63
78
  end
64
79
 
65
80
  # @private
66
81
  #
67
82
  # @param url [URI::Generic]
68
- # @param streaming [Boolean]
69
83
  # @param blk [Proc]
70
84
  #
71
- private def with_pool(url, streaming:, &blk)
85
+ private def with_pool(url, &blk)
72
86
  origin = TerminalShop::Util.uri_origin(url)
73
87
  th = Thread.current
74
88
  key = :"#{object_id}-#{self.class.name}-connection_in_use_for_#{origin}"
75
89
 
76
- if th[key] || streaming
77
- return Enumerator.new do |y|
90
+ if th[key]
91
+ tap do
78
92
  conn = self.class.connect(url)
79
- blk.call(conn, y)
93
+ return blk.call(conn)
94
+ ensure
95
+ conn.finish if conn&.started?
80
96
  end
81
97
  end
82
98
 
@@ -87,20 +103,11 @@ module TerminalShop
87
103
  end
88
104
  end
89
105
 
90
- Enumerator.new do |y|
91
- pool.with do |conn|
92
- th[key] = true
93
-
94
- blk.call(conn, y)
95
- # rubocop:disable Lint/RescueException
96
- rescue Exception => e
97
- # rubocop:enable Lint/RescueException
98
- # should close connection on all errors to ensure no invalid state persists
99
- conn.finish if conn.started?
100
- raise e
101
- ensure
102
- th[key] = nil
103
- end
106
+ pool.with do |conn|
107
+ th[key] = true
108
+ blk.call(conn)
109
+ ensure
110
+ th[key] = nil
104
111
  end
105
112
  end
106
113
 
@@ -116,43 +123,43 @@ module TerminalShop
116
123
  #
117
124
  # @option request [Object] :body
118
125
  #
119
- # @option request [Boolean] :streaming
120
- #
121
- # @option request [Integer] :max_retries
122
- #
123
126
  # @option request [Float] :deadline
124
127
  #
125
128
  # @return [Array(Net::HTTPResponse, Enumerable)]
126
129
  #
127
130
  def execute(request)
128
- method, url, headers, body, deadline = request.fetch_values(:method, :url, :headers, :body, :deadline)
129
- streaming = request.fetch(:streaming)
130
-
131
- req = Net::HTTPGenericRequest.new(
132
- method.to_s.upcase,
133
- !body.nil?,
134
- method != :head,
135
- url.to_s
136
- )
137
-
138
- headers.each { req[_1] = _2 }
139
- case body
140
- in nil
141
- in String
142
- req.body = body
143
- in IO | StringIO
144
- body.rewind
145
- req.body_stream = body
131
+ url, deadline = request.fetch_values(:url, :deadline)
132
+ req = self.class.build_request(request)
133
+
134
+ eof = false
135
+ enum = Enumerator.new do |y|
136
+ with_pool(url) do |conn|
137
+ conn.start unless conn.started?
138
+ self.class.calibrate_socket_timeout(conn, deadline)
139
+
140
+ conn.request(req) do |rsp|
141
+ y << [conn, rsp]
142
+ rsp.read_body do |bytes|
143
+ y << bytes
144
+ self.class.calibrate_socket_timeout(conn, deadline)
145
+ end
146
+ eof = true
147
+ end
148
+ end
146
149
  end
147
150
 
148
- enum =
149
- with_pool(url, streaming: streaming) do |conn, y|
150
- self.class.transport(conn, req, deadline, &y)
151
- end
151
+ # need to protect the `Enumerator` against `#.rewind`
152
+ fused = false
153
+ conn, response = enum.next
154
+ body = Enumerator.new do |y|
155
+ next if fused
152
156
 
153
- enum = streaming ? enum.lazy : enum.to_a
154
- response = enum.take(1).first
155
- [response, (response.body = enum.drop(1))]
157
+ fused = true
158
+ loop { y << enum.next }
159
+ ensure
160
+ conn.finish if !eof && conn.started?
161
+ end
162
+ [response, (response.body = body)]
156
163
  end
157
164
 
158
165
  def initialize
@@ -3,6 +3,31 @@
3
3
  module TerminalShop
4
4
  module Resources
5
5
  class Order
6
+ # Create an order without a cart. The order will be placed immediately.
7
+ #
8
+ # @param params [TerminalShop::Models::OrderCreateParams, Hash{Symbol=>Object}] .
9
+ #
10
+ # @option params [String] :address_id Shipping address ID.
11
+ #
12
+ # @option params [String] :card_id Card ID.
13
+ #
14
+ # @option params [Hash{Symbol=>Integer}] :variants Product variants to include in the order, along with their quantities.
15
+ #
16
+ # @option params [TerminalShop::RequestOptions, Hash{Symbol=>Object}, nil] :request_options
17
+ #
18
+ # @return [TerminalShop::Models::OrderCreateResponse]
19
+ #
20
+ def create(params)
21
+ parsed, options = TerminalShop::Models::OrderCreateParams.dump_request(params)
22
+ @client.request(
23
+ method: :post,
24
+ path: "order",
25
+ body: parsed,
26
+ model: TerminalShop::Models::OrderCreateResponse,
27
+ options: options
28
+ )
29
+ end
30
+
6
31
  # List the orders associated with the current user.
7
32
  #
8
33
  # @param params [TerminalShop::Models::OrderListParams, Hash{Symbol=>Object}] .
@@ -13,8 +13,6 @@ module TerminalShop
13
13
  #
14
14
  # @option params [String] :card_id ID of the card used for the subscription.
15
15
  #
16
- # @option params [Symbol, TerminalShop::Models::SubscriptionAPI::Frequency] :frequency Frequency of the subscription.
17
- #
18
16
  # @option params [String] :product_variant_id ID of the product variant being subscribed to.
19
17
  #
20
18
  # @option params [Integer] :quantity Quantity of the subscription.
@@ -6,6 +6,48 @@ module TerminalShop
6
6
  # @private
7
7
  #
8
8
  module Util
9
+ # @private
10
+ #
11
+ # @return [String]
12
+ #
13
+ def self.arch
14
+ case (arch = RbConfig::CONFIG["arch"])&.downcase
15
+ in nil
16
+ "unknown"
17
+ in /aarch64|arm64/
18
+ "arm64"
19
+ in /x86_64/
20
+ "x64"
21
+ in /arm/
22
+ "arm"
23
+ else
24
+ "other:#{arch}"
25
+ end
26
+ end
27
+
28
+ # @private
29
+ #
30
+ # @return [String]
31
+ #
32
+ def self.os
33
+ case (host = RbConfig::CONFIG["host_os"])&.downcase
34
+ in nil
35
+ "Unknown"
36
+ in /linux/
37
+ "Linux"
38
+ in /darwin/
39
+ "MacOS"
40
+ in /freebsd/
41
+ "FreeBSD"
42
+ in /openbsd/
43
+ "OpenBSD"
44
+ in /mswin|mingw|cygwin|ucrt/
45
+ "Windows"
46
+ else
47
+ "Other:#{host}"
48
+ end
49
+ end
50
+
9
51
  # @private
10
52
  #
11
53
  # @param input [Object]
@@ -412,7 +454,7 @@ module TerminalShop
412
454
  def self.decode_content(headers, stream:, suppress_error: false)
413
455
  case headers["content-type"]
414
456
  in %r{^application/json}
415
- json = stream.to_a.join("")
457
+ json = stream.to_a.join
416
458
  begin
417
459
  JSON.parse(json, symbolize_names: true)
418
460
  rescue JSON::ParserError => e
@@ -420,10 +462,10 @@ module TerminalShop
420
462
  json
421
463
  end
422
464
  in %r{^text/}
423
- stream.to_a.join("")
465
+ stream.to_a.join
424
466
  else
425
467
  # TODO: parsing other response types
426
- StringIO.new(stream.to_a.join(""))
468
+ StringIO.new(stream.to_a.join)
427
469
  end
428
470
  end
429
471
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TerminalShop
4
- VERSION = "0.1.0-alpha.11"
4
+ VERSION = "0.1.0-alpha.13"
5
5
  end
data/lib/terminal-shop.rb CHANGED
@@ -7,6 +7,7 @@ require "erb"
7
7
  require "etc"
8
8
  require "json"
9
9
  require "net/http"
10
+ require "rbconfig"
10
11
  require "securerandom"
11
12
  require "set"
12
13
  require "stringio"
@@ -67,6 +68,8 @@ require_relative "terminal-shop/models/cart_set_item_response"
67
68
  require_relative "terminal-shop/models/email_create_params"
68
69
  require_relative "terminal-shop/models/email_create_response"
69
70
  require_relative "terminal-shop/models/order"
71
+ require_relative "terminal-shop/models/order_create_params"
72
+ require_relative "terminal-shop/models/order_create_response"
70
73
  require_relative "terminal-shop/models/order_get_params"
71
74
  require_relative "terminal-shop/models/order_get_response"
72
75
  require_relative "terminal-shop/models/order_list_params"
data/manifest.yaml CHANGED
@@ -5,6 +5,7 @@ dependencies:
5
5
  - etc
6
6
  - json
7
7
  - net/http
8
+ - rbconfig
8
9
  - securerandom
9
10
  - set
10
11
  - stringio
@@ -24,7 +24,6 @@ module TerminalShop
24
24
  url: URI::Generic,
25
25
  headers: T::Hash[String, String],
26
26
  body: T.anything,
27
- streaming: T::Boolean,
28
27
  max_retries: Integer,
29
28
  timeout: Float
30
29
  }
@@ -32,6 +31,8 @@ module TerminalShop
32
31
 
33
32
  MAX_REDIRECTS = 20
34
33
 
34
+ PLATFORM_HEADERS = T::Hash[String, String]
35
+
35
36
  sig { params(req: TerminalShop::BaseClient::RequestComponentsShape).void }
36
37
  def self.validate!(req)
37
38
  end
@@ -0,0 +1,60 @@
1
+ # typed: strong
2
+
3
+ module TerminalShop
4
+ module Models
5
+ class OrderCreateParams < TerminalShop::BaseModel
6
+ extend TerminalShop::RequestParameters::Converter
7
+ include TerminalShop::RequestParameters
8
+
9
+ sig { returns(String) }
10
+ def address_id
11
+ end
12
+
13
+ sig { params(_: String).returns(String) }
14
+ def address_id=(_)
15
+ end
16
+
17
+ sig { returns(String) }
18
+ def card_id
19
+ end
20
+
21
+ sig { params(_: String).returns(String) }
22
+ def card_id=(_)
23
+ end
24
+
25
+ sig { returns(T::Hash[Symbol, Integer]) }
26
+ def variants
27
+ end
28
+
29
+ sig { params(_: T::Hash[Symbol, Integer]).returns(T::Hash[Symbol, Integer]) }
30
+ def variants=(_)
31
+ end
32
+
33
+ sig do
34
+ params(
35
+ address_id: String,
36
+ card_id: String,
37
+ variants: T::Hash[Symbol, Integer],
38
+ request_options: T.any(TerminalShop::RequestOptions, T::Hash[Symbol, T.anything])
39
+ )
40
+ .void
41
+ end
42
+ def initialize(address_id:, card_id:, variants:, request_options: {})
43
+ end
44
+
45
+ sig do
46
+ override
47
+ .returns(
48
+ {
49
+ address_id: String,
50
+ card_id: String,
51
+ variants: T::Hash[Symbol, Integer],
52
+ request_options: TerminalShop::RequestOptions
53
+ }
54
+ )
55
+ end
56
+ def to_hash
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,23 @@
1
+ # typed: strong
2
+
3
+ module TerminalShop
4
+ module Models
5
+ class OrderCreateResponse < TerminalShop::BaseModel
6
+ sig { returns(String) }
7
+ def data
8
+ end
9
+
10
+ sig { params(_: String).returns(String) }
11
+ def data=(_)
12
+ end
13
+
14
+ sig { params(data: String).void }
15
+ def initialize(data:)
16
+ end
17
+
18
+ sig { override.returns({data: String}) }
19
+ def to_hash
20
+ end
21
+ end
22
+ end
23
+ end
@@ -27,14 +27,6 @@ module TerminalShop
27
27
  def card_id=(_)
28
28
  end
29
29
 
30
- sig { returns(Symbol) }
31
- def frequency
32
- end
33
-
34
- sig { params(_: Symbol).returns(Symbol) }
35
- def frequency=(_)
36
- end
37
-
38
30
  sig { returns(String) }
39
31
  def product_variant_id
40
32
  end
@@ -94,7 +86,6 @@ module TerminalShop
94
86
  id: String,
95
87
  address_id: String,
96
88
  card_id: String,
97
- frequency: Symbol,
98
89
  product_variant_id: String,
99
90
  quantity: Integer,
100
91
  next_: String,
@@ -105,16 +96,7 @@ module TerminalShop
105
96
  )
106
97
  .void
107
98
  end
108
- def initialize(
109
- id:,
110
- address_id:,
111
- card_id:,
112
- frequency:,
113
- product_variant_id:,
114
- quantity:,
115
- next_: nil,
116
- schedule: nil
117
- )
99
+ def initialize(id:, address_id:, card_id:, product_variant_id:, quantity:, next_: nil, schedule: nil)
118
100
  end
119
101
 
120
102
  sig do
@@ -124,7 +106,6 @@ module TerminalShop
124
106
  id: String,
125
107
  address_id: String,
126
108
  card_id: String,
127
- frequency: Symbol,
128
109
  product_variant_id: String,
129
110
  quantity: Integer,
130
111
  next_: String,
@@ -138,20 +119,6 @@ module TerminalShop
138
119
  def to_hash
139
120
  end
140
121
 
141
- class Frequency < TerminalShop::Enum
142
- abstract!
143
-
144
- FIXED = :fixed
145
- DAILY = :daily
146
- WEEKLY = :weekly
147
- MONTHLY = :monthly
148
- YEARLY = :yearly
149
-
150
- sig { override.returns(T::Array[Symbol]) }
151
- def self.values
152
- end
153
- end
154
-
155
122
  class Schedule < TerminalShop::Union
156
123
  abstract!
157
124
 
@@ -19,39 +19,21 @@ module TerminalShop
19
19
  def token=(_)
20
20
  end
21
21
 
22
- sig { returns(TerminalShop::Models::TokenAPI::Time) }
23
- def time
22
+ sig { returns(String) }
23
+ def created
24
24
  end
25
25
 
26
- sig { params(_: TerminalShop::Models::TokenAPI::Time).returns(TerminalShop::Models::TokenAPI::Time) }
27
- def time=(_)
26
+ sig { params(_: String).returns(String) }
27
+ def created=(_)
28
28
  end
29
29
 
30
- sig { params(id: String, token: String, time: TerminalShop::Models::TokenAPI::Time).void }
31
- def initialize(id:, token:, time:)
30
+ sig { params(id: String, token: String, created: String).void }
31
+ def initialize(id:, token:, created:)
32
32
  end
33
33
 
34
- sig { override.returns({id: String, token: String, time: TerminalShop::Models::TokenAPI::Time}) }
34
+ sig { override.returns({id: String, token: String, created: String}) }
35
35
  def to_hash
36
36
  end
37
-
38
- class Time < TerminalShop::BaseModel
39
- sig { returns(String) }
40
- def created
41
- end
42
-
43
- sig { params(_: String).returns(String) }
44
- def created=(_)
45
- end
46
-
47
- sig { params(created: String).void }
48
- def initialize(created:)
49
- end
50
-
51
- sig { override.returns({created: String}) }
52
- def to_hash
53
- end
54
- end
55
37
  end
56
38
  end
57
39
  end
@@ -3,15 +3,7 @@
3
3
  module TerminalShop
4
4
  class PooledNetRequester
5
5
  RequestShape = T.type_alias do
6
- {
7
- method: Symbol,
8
- url: URI::Generic,
9
- headers: T::Hash[String, String],
10
- body: T.anything,
11
- streaming: T::Boolean,
12
- max_retries: Integer,
13
- deadline: Float
14
- }
6
+ {method: Symbol, url: URI::Generic, headers: T::Hash[String, String], body: T.anything, deadline: Float}
15
7
  end
16
8
 
17
9
  sig { params(url: URI::Generic).returns(Net::HTTP) }
@@ -19,30 +11,17 @@ module TerminalShop
19
11
  end
20
12
 
21
13
  sig { params(conn: Net::HTTP, deadline: Float).void }
22
- private_class_method def self.calibrate_socket_timeout(conn, deadline)
14
+ def self.calibrate_socket_timeout(conn, deadline)
23
15
  end
24
16
 
25
- sig do
26
- params(
27
- conn: Net::HTTP,
28
- req: Net::HTTPGenericRequest,
29
- deadline: Float,
30
- blk: T.proc.params(arg0: T.any(Net::HTTPGenericRequest, String)).void
31
- )
32
- .void
33
- end
34
- def self.transport(conn, req, deadline, &blk)
17
+ sig { params(request: TerminalShop::PooledNetRequester::RequestShape).returns(Net::HTTPGenericRequest) }
18
+ def self.build_request(request)
35
19
  end
36
20
 
37
21
  sig do
38
- params(
39
- url: URI::Generic,
40
- streaming: T::Boolean,
41
- blk: T.proc.params(arg0: Net::HTTP, arg1: Enumerator::Yielder).void
42
- )
43
- .void
44
- end
45
- private def with_pool(url, streaming:, &blk)
22
+ params(url: URI::Generic, blk: T.proc.params(arg0: Net::HTTP, arg1: Enumerator::Yielder).void).void
23
+ end
24
+ private def with_pool(url, &blk)
46
25
  end
47
26
 
48
27
  sig do
@@ -3,6 +3,18 @@
3
3
  module TerminalShop
4
4
  module Resources
5
5
  class Order
6
+ sig do
7
+ params(
8
+ address_id: String,
9
+ card_id: String,
10
+ variants: T::Hash[Symbol, Integer],
11
+ request_options: T.nilable(T.any(TerminalShop::RequestOptions, T::Hash[Symbol, T.anything]))
12
+ )
13
+ .returns(TerminalShop::Models::OrderCreateResponse)
14
+ end
15
+ def create(address_id:, card_id:, variants:, request_options: {})
16
+ end
17
+
6
18
  sig do
7
19
  params(request_options: T.nilable(T.any(TerminalShop::RequestOptions, T::Hash[Symbol, T.anything])))
8
20
  .returns(TerminalShop::Models::OrderListResponse)
@@ -8,7 +8,6 @@ module TerminalShop
8
8
  id: String,
9
9
  address_id: String,
10
10
  card_id: String,
11
- frequency: Symbol,
12
11
  product_variant_id: String,
13
12
  quantity: Integer,
14
13
  next_: String,
@@ -24,7 +23,6 @@ module TerminalShop
24
23
  id:,
25
24
  address_id:,
26
25
  card_id:,
27
- frequency:,
28
26
  product_variant_id:,
29
27
  quantity:,
30
28
  next_: nil,
@@ -2,6 +2,14 @@
2
2
 
3
3
  module TerminalShop
4
4
  module Util
5
+ sig { returns(String) }
6
+ def self.arch
7
+ end
8
+
9
+ sig { returns(String) }
10
+ def self.os
11
+ end
12
+
5
13
  sig { params(input: T.anything).returns(T.any(T::Boolean, T.anything)) }
6
14
  def self.primitive?(input)
7
15
  end
@@ -1,5 +1,5 @@
1
1
  # typed: strong
2
2
 
3
3
  module TerminalShop
4
- VERSION = "0.1.0-alpha.11"
4
+ VERSION = "0.1.0-alpha.13"
5
5
  end
@@ -19,13 +19,14 @@ module TerminalShop
19
19
  url: URI::Generic,
20
20
  headers: ::Hash[String, String],
21
21
  body: top,
22
- streaming: bool,
23
22
  max_retries: Integer,
24
23
  timeout: Float
25
24
  }
26
25
 
27
26
  MAX_REDIRECTS: 20
28
27
 
28
+ PLATFORM_HEADERS: ::Hash[String, String]
29
+
29
30
  def self.validate!: (
30
31
  TerminalShop::BaseClient::request_components req
31
32
  ) -> void
@@ -0,0 +1,32 @@
1
+ module TerminalShop
2
+ module Models
3
+ type order_create_params =
4
+ { address_id: String, card_id: String, variants: ::Hash[Symbol, Integer] }
5
+ & TerminalShop::request_parameters
6
+
7
+ class OrderCreateParams < TerminalShop::BaseModel
8
+ extend TerminalShop::RequestParameters::Converter
9
+ include TerminalShop::RequestParameters
10
+
11
+ attr_accessor address_id: String
12
+
13
+ attr_accessor card_id: String
14
+
15
+ attr_accessor variants: ::Hash[Symbol, Integer]
16
+
17
+ def initialize:
18
+ (
19
+ address_id: String,
20
+ card_id: String,
21
+ variants: ::Hash[Symbol, Integer],
22
+ request_options: TerminalShop::request_opts
23
+ ) -> void
24
+ | (
25
+ ?TerminalShop::Models::order_create_params
26
+ | TerminalShop::BaseModel data
27
+ ) -> void
28
+
29
+ def to_hash: -> TerminalShop::Models::order_create_params
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module TerminalShop
2
+ module Models
3
+ type order_create_response = { data: String }
4
+
5
+ class OrderCreateResponse < TerminalShop::BaseModel
6
+ attr_accessor data: String
7
+
8
+ def initialize:
9
+ (data: String) -> void
10
+ | (
11
+ ?TerminalShop::Models::order_create_response
12
+ | TerminalShop::BaseModel data
13
+ ) -> void
14
+
15
+ def to_hash: -> TerminalShop::Models::order_create_response
16
+ end
17
+ end
18
+ end
@@ -5,7 +5,6 @@ module TerminalShop
5
5
  id: String,
6
6
  address_id: String,
7
7
  card_id: String,
8
- frequency: TerminalShop::Models::SubscriptionAPI::frequency,
9
8
  product_variant_id: String,
10
9
  quantity: Integer,
11
10
  next_: String,
@@ -19,8 +18,6 @@ module TerminalShop
19
18
 
20
19
  attr_accessor card_id: String
21
20
 
22
- attr_accessor frequency: TerminalShop::Models::SubscriptionAPI::frequency
23
-
24
21
  attr_accessor product_variant_id: String
25
22
 
26
23
  attr_accessor quantity: Integer
@@ -40,7 +37,6 @@ module TerminalShop
40
37
  id: String,
41
38
  address_id: String,
42
39
  card_id: String,
43
- frequency: TerminalShop::Models::SubscriptionAPI::frequency,
44
40
  product_variant_id: String,
45
41
  quantity: Integer,
46
42
  next_: String,
@@ -52,18 +48,6 @@ module TerminalShop
52
48
 
53
49
  def to_hash: -> TerminalShop::Models::subscription_api
54
50
 
55
- type frequency = :fixed | :daily | :weekly | :monthly | :yearly
56
-
57
- class Frequency < TerminalShop::Enum
58
- FIXED: :fixed
59
- DAILY: :daily
60
- WEEKLY: :weekly
61
- MONTHLY: :monthly
62
- YEARLY: :yearly
63
-
64
- def self.values: -> ::Array[TerminalShop::Models::SubscriptionAPI::frequency]
65
- end
66
-
67
51
  type schedule =
68
52
  TerminalShop::Models::SubscriptionAPI::Schedule::Fixed
69
53
  | TerminalShop::Models::SubscriptionAPI::Schedule::Weekly
@@ -1,40 +1,21 @@
1
1
  module TerminalShop
2
2
  module Models
3
- type token_api =
4
- { id: String, token: String, time: TerminalShop::Models::TokenAPI::Time }
3
+ type token_api = { id: String, token: String, created: String }
5
4
 
6
5
  class TokenAPI < TerminalShop::BaseModel
7
6
  attr_accessor id: String
8
7
 
9
8
  attr_accessor token: String
10
9
 
11
- attr_accessor time: TerminalShop::Models::TokenAPI::Time
10
+ attr_accessor created: String
12
11
 
13
12
  def initialize:
14
- (
15
- id: String,
16
- token: String,
17
- time: TerminalShop::Models::TokenAPI::Time
18
- ) -> void
13
+ (id: String, token: String, created: String) -> void
19
14
  | (
20
15
  ?TerminalShop::Models::token_api | TerminalShop::BaseModel data
21
16
  ) -> void
22
17
 
23
18
  def to_hash: -> TerminalShop::Models::token_api
24
-
25
- type time = { created: String }
26
-
27
- class Time < TerminalShop::BaseModel
28
- attr_accessor created: String
29
-
30
- def initialize:
31
- (created: String) -> void
32
- | (
33
- ?TerminalShop::Models::TokenAPI::time | TerminalShop::BaseModel data
34
- ) -> void
35
-
36
- def to_hash: -> TerminalShop::Models::TokenAPI::time
37
- end
38
19
  end
39
20
  end
40
21
  end
@@ -6,29 +6,19 @@ module TerminalShop
6
6
  url: URI::Generic,
7
7
  headers: ::Hash[String, String],
8
8
  body: top,
9
- streaming: bool,
10
- max_retries: Integer,
11
9
  deadline: Float
12
10
  }
13
11
 
14
12
  def self.connect: (URI::Generic url) -> top
15
13
 
16
- private def self.calibrate_socket_timeout: (
17
- top conn,
18
- Float deadline
19
- ) -> void
14
+ def self.calibrate_socket_timeout: (top conn, Float deadline) -> void
20
15
 
21
- def self.transport: (
22
- top conn,
23
- top req,
24
- Float deadline
25
- ) {
26
- (top | String arg0) -> void
27
- } -> void
16
+ def self.build_request: (
17
+ TerminalShop::PooledNetRequester::request request
18
+ ) -> top
28
19
 
29
20
  private def with_pool: (
30
- URI::Generic url,
31
- streaming: bool
21
+ URI::Generic url
32
22
  ) {
33
23
  (top arg0, Enumerator::Yielder arg1) -> void
34
24
  } -> void
@@ -1,6 +1,17 @@
1
1
  module TerminalShop
2
2
  module Resources
3
3
  class Order
4
+ def create:
5
+ (
6
+ TerminalShop::Models::OrderCreateParams | ::Hash[Symbol, top] params
7
+ ) -> TerminalShop::Models::OrderCreateResponse
8
+ | (
9
+ address_id: String,
10
+ card_id: String,
11
+ variants: ::Hash[Symbol, Integer],
12
+ request_options: TerminalShop::request_opts
13
+ ) -> TerminalShop::Models::OrderCreateResponse
14
+
4
15
  def list:
5
16
  (
6
17
  ?TerminalShop::Models::OrderListParams | ::Hash[Symbol, top] params
@@ -10,7 +10,6 @@ module TerminalShop
10
10
  id: String,
11
11
  address_id: String,
12
12
  card_id: String,
13
- frequency: TerminalShop::Models::SubscriptionAPI::frequency,
14
13
  product_variant_id: String,
15
14
  quantity: Integer,
16
15
  next_: String,
@@ -1,5 +1,9 @@
1
1
  module TerminalShop
2
2
  module Util
3
+ def self?.arch: -> String
4
+
5
+ def self?.os: -> String
6
+
3
7
  def self?.primitive?: (top input) -> (bool | top)
4
8
 
5
9
  def self?.coerce_boolean: (top input) -> (bool | top)
@@ -1,3 +1,3 @@
1
1
  module TerminalShop
2
- VERSION: "0.1.0-alpha.10"
2
+ VERSION: "0.1.0-alpha.12"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal-shop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.alpha.11
4
+ version: 0.1.0.pre.alpha.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terminal
@@ -78,6 +78,8 @@ files:
78
78
  - lib/terminal-shop/models/email_create_params.rb
79
79
  - lib/terminal-shop/models/email_create_response.rb
80
80
  - lib/terminal-shop/models/order.rb
81
+ - lib/terminal-shop/models/order_create_params.rb
82
+ - lib/terminal-shop/models/order_create_response.rb
81
83
  - lib/terminal-shop/models/order_get_params.rb
82
84
  - lib/terminal-shop/models/order_get_response.rb
83
85
  - lib/terminal-shop/models/order_list_params.rb
@@ -170,6 +172,8 @@ files:
170
172
  - rbi/lib/terminal-shop/models/email_create_params.rbi
171
173
  - rbi/lib/terminal-shop/models/email_create_response.rbi
172
174
  - rbi/lib/terminal-shop/models/order.rbi
175
+ - rbi/lib/terminal-shop/models/order_create_params.rbi
176
+ - rbi/lib/terminal-shop/models/order_create_response.rbi
173
177
  - rbi/lib/terminal-shop/models/order_get_params.rbi
174
178
  - rbi/lib/terminal-shop/models/order_get_response.rbi
175
179
  - rbi/lib/terminal-shop/models/order_list_params.rbi
@@ -261,6 +265,8 @@ files:
261
265
  - sig/terminal-shop/models/email_create_params.rbs
262
266
  - sig/terminal-shop/models/email_create_response.rbs
263
267
  - sig/terminal-shop/models/order.rbs
268
+ - sig/terminal-shop/models/order_create_params.rbs
269
+ - sig/terminal-shop/models/order_create_response.rbs
264
270
  - sig/terminal-shop/models/order_get_params.rbs
265
271
  - sig/terminal-shop/models/order_get_response.rbs
266
272
  - sig/terminal-shop/models/order_list_params.rbs