music_today_api_wrapper 22.12.15.02 → 22.12.15.03

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe7ef535ebb2583ffd33da42ed9622ecddbdd289
4
- data.tar.gz: d30f851887f6fa99ff9d102d82827259df8a3e3f
3
+ metadata.gz: b3f1708963b1fd2219c16e832691bcc7f5752acf
4
+ data.tar.gz: 36d4ba12c95ca99ea151af4d091fceb3b4b31c04
5
5
  SHA512:
6
- metadata.gz: 7af11049cf359e22f6e2742c3f777b593c0e7ca8594610575cedf67bf9e74360370c67d14c5b1689659590f4b13aefabe86927f0a8a7d5e87e4d419df35a24a9
7
- data.tar.gz: 316da87c1172e1be150323c5e781ff3df661aa60a2d8f1a2b9458b1ffbade7d5a523dd3b8cc12d63b842713926202f505c939e6c2aa7fef669dcf3b67eb38307
6
+ metadata.gz: f7a788e3fcfa0a0717f8fab2a913dfd16059db6cafdef1b783ddbc05f34883c80b5fbca7df8ee6edffd77162b7c353dad4a757ba71c22c8b5f5952097f77f112
7
+ data.tar.gz: c7799a3b7a4a9d4d3fef8e1c17a834c584eb6ab9c9023400e74b6782554b9dc24a31da4755265941fbb352aa6b11be3df16cfc6e1ca28c134f7d29fddcb67adc
@@ -1,6 +1,7 @@
1
1
  require 'support/configuration'
2
2
  require 'services/product_services'
3
3
  require 'services/shipping_services'
4
+ require 'services/checkout_services'
4
5
 
5
6
  module MusicTodayApiWrapper
6
7
  class << self
@@ -26,4 +27,9 @@ module MusicTodayApiWrapper
26
27
  shipping_services = Services::ShippingServices.new
27
28
  shipping_services.options(address, items)
28
29
  end
30
+
31
+ def self.checkout(address, items)
32
+ checkout_services = Services::CheckoutServices.new
33
+ checkout_services.checkout(address, items)
34
+ end
29
35
  end
@@ -1,3 +1,6 @@
1
+ require 'resources/purchase/shipping_option'
2
+ require 'resources/hash'
3
+
1
4
  module MusicTodayApiWrapper
2
5
  module Resources
3
6
  class Address
@@ -5,17 +8,22 @@ module MusicTodayApiWrapper
5
8
  :street_second_line,
6
9
  :city,
7
10
  :postal_code,
8
- :country
11
+ :country,
12
+ :shipping_options,
13
+ :selected_shipping
9
14
 
10
15
  # rubocop:disable ParameterLists
11
16
  def initialize(street, state, city, postal_code, country = 'US',
12
- street_second_line = nil)
17
+ street_second_line = '', shipping_options = [],
18
+ selected_shipping = nil)
13
19
  @street = street
14
20
  @state = state
15
21
  @street_second_line = street_second_line
16
22
  @city = city
17
23
  @postal_code = postal_code
18
24
  @country = country
25
+ @shipping_options = shipping_options
26
+ @selected_shipping = selected_shipping
19
27
  end
20
28
 
21
29
  def self.from_hash(address_hash)
@@ -32,7 +40,8 @@ module MusicTodayApiWrapper
32
40
  state: @state,
33
41
  city: @city,
34
42
  postalCode: @postal_code,
35
- country: @country }
43
+ country: @country,
44
+ shippingOptionType: @selected_shipping }.compact
36
45
  end
37
46
  end
38
47
  end
@@ -0,0 +1,27 @@
1
+ require 'resources/address'
2
+ require 'resources/purchase/item'
3
+
4
+ module MusicTodayApiWrapper
5
+ module Resources
6
+ module Checkout
7
+ class Session
8
+ attr_accessor :id, :address, :items
9
+
10
+ def initialize(id, address, items = [])
11
+ @id = id
12
+ @address = address
13
+ @items = items
14
+ end
15
+
16
+ def self.from_hash(session_hash)
17
+ address = Address.from_hash(session_hash)
18
+ session = Session.new(session_hash['sessionId'], address)
19
+ session_hash['items'].each do |item|
20
+ session.items << Resources::Purchase::Item.from_hash(item)
21
+ end
22
+ session
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ module MusicTodayApiWrapper
2
+ # rubocop:disable ClassAndModuleChildren
3
+ class ::Hash
4
+ def compact
5
+ select { |_, value| !value.nil? }
6
+ end
7
+ end
8
+ end
@@ -1,19 +1,33 @@
1
1
  require 'resources/variant'
2
+ require 'resources/hash'
3
+
2
4
  module MusicTodayApiWrapper
3
5
  module Resources
4
6
  module Purchase
5
7
  class Item
6
8
  attr_accessor :variant, :quantity
7
9
 
8
- def initialize(variant = Variant.new, quantity = 1)
10
+ def initialize(variant = Variant.new, quantity = 1, tax = nil,
11
+ total = nil)
9
12
  @variant = variant
10
13
  @quantity = quantity
14
+ @tax = tax
15
+ @total = total
11
16
  end
12
17
 
13
18
  def as_hash
14
19
  { sku: @variant.sku,
15
20
  qty: @quantity,
16
- price: @variant.price }
21
+ quantity: @quantity,
22
+ price: @variant.price,
23
+ tax: @tax,
24
+ total: @total }.compact
25
+ end
26
+
27
+ def self.from_hash(item_hash)
28
+ variant = Variant.from_hash(item_hash)
29
+ Item.new(variant, item_hash['quantity'], item_hash['tax'],
30
+ item_hash['total'])
17
31
  end
18
32
  end
19
33
  end
@@ -1,10 +1,13 @@
1
+ require 'resources/hash'
2
+
1
3
  module MusicTodayApiWrapper
2
4
  module Resources
3
5
  module Purchase
4
6
  class ShippingOption
5
7
  attr_accessor :type, :delivery_date, :rate, :description
6
8
 
7
- def initialize(type, delivery_date, rate, description)
9
+ def initialize(type, delivery_date = nil, rate = nil,
10
+ description = nil)
8
11
  @type = type
9
12
  @delivery_date = delivery_date ? Date.parse(delivery_date) : nil
10
13
  @rate = rate
@@ -17,6 +20,11 @@ module MusicTodayApiWrapper
17
20
  option['totalRate'],
18
21
  option['shippingOptionName'])
19
22
  end
23
+
24
+ def as_hash
25
+ { shippingOptionType: @type, deliveryDate: @delivery_date,
26
+ totalRate: @rate, shippingOptionName: @description }.compact
27
+ end
20
28
  end
21
29
  end
22
30
  end
@@ -1,10 +1,13 @@
1
- class String
2
- def to_underscore!
3
- gsub!(/(.)([A-Z])/, '\1_\2')
4
- downcase!
5
- end
1
+ module MusicTodayApiWrapper
2
+ # rubocop:disable ClassAndModuleChildren
3
+ class ::String
4
+ def to_underscore!
5
+ gsub!(/(.)([A-Z])/, '\1_\2')
6
+ downcase!
7
+ end
6
8
 
7
- def to_underscore
8
- dup.tap(&:to_underscore!)
9
+ def to_underscore
10
+ dup.tap(&:to_underscore!)
11
+ end
9
12
  end
10
13
  end
@@ -15,20 +15,24 @@ module MusicTodayApiWrapper
15
15
  end
16
16
 
17
17
  def self.from_hash(variant_hash)
18
- variant_names = []
18
+ Variant.new(variant_hash['sku'],
19
+ variant_hash['listPrice'],
20
+ variant_hash['qtyAvailable'],
21
+ variant_names(variant_hash['variantNames']))
22
+ end
23
+
24
+ def self.variant_names(names)
25
+ return [] unless names
26
+ symbolized_names = []
19
27
 
20
- variant_hash['variantNames'].each do |variant|
28
+ names.each do |variant|
21
29
  variant_name = {}
22
30
  variant_name[variant.keys[0]
23
31
  .to_underscore
24
32
  .to_sym] = variant.values[0]
25
- variant_names << variant_name
33
+ symbolized_names << variant_name
26
34
  end
27
-
28
- Variant.new(variant_hash['sku'],
29
- variant_hash['listPrice'],
30
- variant_hash['qtyAvailable'],
31
- variant_names)
35
+ symbolized_names
32
36
  end
33
37
  end
34
38
  end
@@ -42,6 +42,14 @@ module MusicTodayApiWrapper
42
42
  end
43
43
  end
44
44
 
45
+ def checkout(params)
46
+ @common_response.work do
47
+ url = "#{@url}/cart/pricing"
48
+ @common_response.data[:session] =
49
+ post(url, {}, params)['addresses'].first
50
+ end
51
+ end
52
+
45
53
  private
46
54
 
47
55
  def get(url, options = {})
@@ -0,0 +1,39 @@
1
+ require 'rest_clients/music_today_rest_client'
2
+ require 'rest_clients/common_response'
3
+ require 'resources/purchase/item'
4
+ require 'resources/checkout/session'
5
+
6
+ module MusicTodayApiWrapper
7
+ module Services
8
+ class CheckoutServices
9
+ def initialize
10
+ @common_response = RestClients::CommonResponse.new
11
+ @rest_client = RestClient.new
12
+ end
13
+
14
+ # rubocop:disable MethodLength
15
+ def checkout(address, items)
16
+ items_hash = []
17
+ items.each { |item| items_hash << item.as_hash }
18
+ address_hash =
19
+ address
20
+ .as_hash
21
+ .merge(id: 1, items: items_hash)
22
+
23
+ @common_response.work do
24
+ params = { storeId: @rest_client.catalog_number,
25
+ currency: 'USD',
26
+ priceLevel: '',
27
+ addresses: [address_hash],
28
+ promotions: [] }
29
+
30
+ response = @rest_client.checkout(params)
31
+
32
+ return response unless response.success?
33
+ @common_response.data[:session] =
34
+ Resources::Checkout::Session.from_hash(response.data[:session])
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -5,10 +5,9 @@ module MusicTodayApiWrapper
5
5
  module Services
6
6
  class ShippingServices
7
7
  def initialize
8
- @common_response =
9
- MusicTodayApiWrapper::RestClients::CommonResponse.new
8
+ @common_response = RestClients::CommonResponse.new
10
9
  @common_response.data[:shipping_options] = []
11
- @rest_client = MusicTodayApiWrapper::RestClient.new
10
+ @rest_client = RestClient.new
12
11
  end
13
12
 
14
13
  def options(address, items)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: music_today_api_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 22.12.15.02
4
+ version: 22.12.15.03
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Gonzaga
@@ -191,6 +191,8 @@ extra_rdoc_files: []
191
191
  files:
192
192
  - lib/music_today_api_wrapper.rb
193
193
  - lib/resources/address.rb
194
+ - lib/resources/checkout/session.rb
195
+ - lib/resources/hash.rb
194
196
  - lib/resources/image.rb
195
197
  - lib/resources/product.rb
196
198
  - lib/resources/purchase/item.rb
@@ -199,6 +201,7 @@ files:
199
201
  - lib/resources/variant.rb
200
202
  - lib/rest_clients/common_response.rb
201
203
  - lib/rest_clients/music_today_rest_client.rb
204
+ - lib/services/checkout_services.rb
202
205
  - lib/services/product_services.rb
203
206
  - lib/services/shipping_services.rb
204
207
  - lib/support/configuration.rb