music_today_api_wrapper 16.12.15.03 → 18.12.15.01

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: 5257b1448b701bcfbbda6c1c88a866d1ffeed5f7
4
- data.tar.gz: 231cffef87679a3827e7c992298a3ff831caf0af
3
+ metadata.gz: ebbb03108cb1cb2343c6878cb03e5d24e7bca582
4
+ data.tar.gz: 14b42dbddf872170204c1e24084deb48cd15ecbf
5
5
  SHA512:
6
- metadata.gz: 63729e32b5b07c461e3f09adef9ea84c2b1268522d4a9c68842ad218a477bb4138dd677edcdc67c451f37abee0684dc7b7ef6d302bea683b47a0d32da68e3e08
7
- data.tar.gz: dd3e6a5d12b15b73b8ca428dff2b25e1d4b1dca44a90ee9499d10bd808b818020ea3e14e14bc2dfc6840d118a14698e2e91118a6a454337e54a37768d433afca
6
+ metadata.gz: 6614da695fa37cfaebf8cfac72cf0abb2e1733effbd5cadb031a348db16ab6d90cc24f57525f1153b3024c7152b0834fc64b44c15b96e2149b06fa39934542be
7
+ data.tar.gz: f77afdcae9d7cea20b84b8bb86a12346322c8bd4d2cf3474871bf52bf425076e1dc7b9415e48ff8035df7a00a52c44f5170bdf234b6683faf0f590527feb2b6b
@@ -1,5 +1,6 @@
1
1
  require 'support/configuration'
2
2
  require 'services/product_services'
3
+ require 'services/shipping_services'
3
4
 
4
5
  module MusicTodayApiWrapper
5
6
  class << self
@@ -16,8 +17,13 @@ module MusicTodayApiWrapper
16
17
  product_services.all_products
17
18
  end
18
19
 
19
- def self.find_product(id)
20
+ def self.find_product(parent_id, id)
20
21
  product_services = Services::ProductServices.new
21
- product_services.find_product(id)
22
+ product_services.find_product(parent_id, id)
23
+ end
24
+
25
+ def self.shipping_options(address, items)
26
+ shipping_services = Services::ShippingServices.new
27
+ shipping_services.options(address, items)
22
28
  end
23
29
  end
@@ -0,0 +1,39 @@
1
+ module MusicTodayApiWrapper
2
+ module Resources
3
+ class Address
4
+ attr_accessor :street,
5
+ :street_second_line,
6
+ :city,
7
+ :postal_code,
8
+ :country
9
+
10
+ # rubocop:disable ParameterLists
11
+ def initialize(street, state, city, postal_code, country = 'US',
12
+ street_second_line = nil)
13
+ @street = street
14
+ @state = state
15
+ @street_second_line = street_second_line
16
+ @city = city
17
+ @postal_code = postal_code
18
+ @country = country
19
+ end
20
+
21
+ def self.from_hash(address_hash)
22
+ Address.new(address_hash['street'],
23
+ address_hash['city'],
24
+ address_hash['postalCode'],
25
+ address_hash['country'],
26
+ address_hash['street2'])
27
+ end
28
+
29
+ def as_hash
30
+ { street: @street,
31
+ street2: @street_second_line,
32
+ state: @state,
33
+ city: @city,
34
+ postalCode: @postal_code,
35
+ country: @country }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -3,12 +3,19 @@ require 'resources/image'
3
3
  module MusicTodayApiWrapper
4
4
  module Resources
5
5
  class Product
6
- attr_accessor :uid, :name, :description, :category, :image, :price
6
+ attr_accessor :uid,
7
+ :parent_id,
8
+ :name,
9
+ :description,
10
+ :category,
11
+ :image,
12
+ :price
7
13
 
8
14
  # rubocop:disable ParameterLists
9
- def initialize(uid, name, description, category, price,
15
+ def initialize(uid, parent_id, name, description, category, price,
10
16
  image = MusicTodayApiWrapper::Resources::Image.new)
11
17
  @uid = uid
18
+ @parent_id = parent_id
12
19
  @name = name
13
20
  @description = description
14
21
  @category = category
@@ -16,16 +23,24 @@ module MusicTodayApiWrapper
16
23
  @image = image
17
24
  end
18
25
 
19
- def self.from_hash(product_hash)
20
- image = Image.new(product_hash['imageUrlSmall'],
21
- product_hash['imageUrlMedium'],
22
- product_hash['imageUrlLarge'])
26
+ def self.from_hash(product)
27
+ image = Image.new(product['imageUrlSmall'],
28
+ product['imageUrlMedium'],
29
+ product['imageUrlLarge'])
30
+ variants = []
31
+ product['variants'].each do |variant|
32
+ variants << variant_from_hash(product, variant, image)
33
+ end
34
+ variants
35
+ end
23
36
 
24
- Product.new(product_hash['id'],
37
+ def self.variant_from_hash(product_hash, variant_hash, image)
38
+ Product.new(variant_hash['sku'],
39
+ product_hash['id'],
25
40
  product_hash['name'],
26
41
  product_hash['lang']['en']['textDesc'],
27
42
  product_hash['categoryName'],
28
- product_hash['listPrice'],
43
+ variant_hash['listPrice'],
29
44
  image)
30
45
  end
31
46
  end
@@ -0,0 +1,21 @@
1
+ require 'resources/product'
2
+ module MusicTodayApiWrapper
3
+ module Resources
4
+ module Purchase
5
+ class Item
6
+ attr_accessor :product, :quantity
7
+
8
+ def initialize(product = Product.new, quantity = 1)
9
+ @product = product
10
+ @quantity = quantity
11
+ end
12
+
13
+ def as_hash
14
+ { sku: @product.uid,
15
+ qty: @quantity,
16
+ price: @product.price }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module MusicTodayApiWrapper
2
+ module Resources
3
+ module Purchase
4
+ class ShippingOption
5
+ attr_accessor :type, :delivery_date, :rate, :description
6
+
7
+ def initialize(type, delivery_date, rate, description)
8
+ @type = type
9
+ @delivery_date = delivery_date ? Date.parse(delivery_date) : nil
10
+ @rate = rate
11
+ @description = description
12
+ end
13
+
14
+ def self.from_hash(option)
15
+ ShippingOption.new(option['shippingOptionType'],
16
+ option['deliveryDate'],
17
+ option['totalRate'],
18
+ option['shippingOptionName'])
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,33 +5,63 @@ require 'support/configuration'
5
5
 
6
6
  module MusicTodayApiWrapper
7
7
  class RestClient
8
+ attr_accessor :catalog_number
9
+
8
10
  def initialize
9
11
  config = MusicTodayApiWrapper::Configuration.new
10
12
  @url = config.url
11
13
  @user = config.user
12
14
  @api_key = config.api_key
13
- @catalog_number = config.catalog
15
+ @catalog_number = config.catalog.to_i
14
16
  @common_response = MusicTodayApiWrapper::RestClients::CommonResponse.new
15
17
  end
16
18
 
17
19
  def all_products
18
20
  @common_response.work do
19
- uri =
20
- URI("#{@url}/catalog/content/#{@catalog_number}/?apiuser=#{@user}&"\
21
- "apikey=#{@api_key}")
22
- res = Net::HTTP.get_response(uri)
23
- @common_response.data[:products] = JSON.parse(res.body)['products']
21
+ url = "#{@url}/catalog/content/#{@catalog_number}/"
22
+ @common_response.data[:products] = get(url)['products']
24
23
  end
25
24
  end
26
25
 
27
26
  def find_product(product_id)
28
27
  @common_response.work do
29
- uri =
30
- URI("#{@url}/catalog/product/#{@catalog_number}/#{product_id}?"\
31
- "apiuser=#{@user}&apikey=#{@api_key}")
32
- res = Net::HTTP.get_response(uri)
33
- @common_response.data[:product] = JSON.parse(res.body)['product']
28
+ url = "#{@url}/catalog/product/#{@catalog_number}/#{product_id}"
29
+ @common_response.data[:product] = get(url)['product']
30
+ end
31
+ end
32
+
33
+ def shipping_options(params)
34
+ @common_response.work do
35
+ url = "#{@url}/order/shippingOptionsGet"
36
+ @common_response.data[:shipping_options] =
37
+ post(url, {}, params)['shippingOptions']
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def get(url, options = {})
44
+ uri = URI(url)
45
+ hit(uri, options) do
46
+ Net::HTTP.get_response(uri)
34
47
  end
35
48
  end
49
+
50
+ def post(url, options = {}, body = {})
51
+ uri = URI(url)
52
+ hit(uri, options) do
53
+ request = Net::HTTP::Post.new(uri)
54
+ request.body = body.to_json
55
+ request.content_type = 'application/json'
56
+ Net::HTTP.start(uri.hostname) { |http| http.request(request) }
57
+ end
58
+ end
59
+
60
+ def hit(uri, options = {})
61
+ options.merge!(apiuser: @user, apikey: @api_key)
62
+ uri.query = URI.encode_www_form(options)
63
+ response = yield
64
+ JSON.parse(response.body)
65
+ end
36
66
  end
37
67
  end
@@ -20,23 +20,26 @@ module MusicTodayApiWrapper
20
20
  end
21
21
  end
22
22
 
23
- def find_product(id)
24
- results = @rest_client.find_product(id)
23
+ def find_product(parent_id, id)
24
+ results = @rest_client.find_product(parent_id)
25
25
  return results unless results.success?
26
26
 
27
27
  @common_response.work do
28
- @common_response.data[:product] =
29
- MusicTodayApiWrapper::Resources::Product
30
- .from_hash(results.data[:product])
28
+ find_variant(id, results.data[:product])
31
29
  end
32
30
  end
33
31
 
34
32
  private
35
33
 
36
34
  def product_mapper(product)
37
- product_obj =
38
- MusicTodayApiWrapper::Resources::Product.from_hash(product)
39
- @common_response.data[:products] << product_obj
35
+ @common_response.data[:products] +=
36
+ Resources::Product.from_hash(product)
37
+ end
38
+
39
+ def find_variant(id, product_and_variants)
40
+ products = Resources::Product.from_hash(product_and_variants)
41
+ @common_response.data[:product] =
42
+ products.find { |product| product.uid == id }
40
43
  end
41
44
  end
42
45
  end
@@ -0,0 +1,39 @@
1
+ require 'rest_clients/music_today_rest_client'
2
+ require 'resources/purchase/shipping_option'
3
+
4
+ module MusicTodayApiWrapper
5
+ module Services
6
+ class ShippingServices
7
+ def initialize
8
+ @common_response =
9
+ MusicTodayApiWrapper::RestClients::CommonResponse.new
10
+ @common_response.data[:shipping_options] = []
11
+ @rest_client = MusicTodayApiWrapper::RestClient.new
12
+ end
13
+
14
+ def options(address, items)
15
+ hash_items = []
16
+ items.each { |item| hash_items << item.as_hash }
17
+
18
+ params = { storeId: @rest_client.catalog_number,
19
+ address: address.as_hash,
20
+ lineItems: hash_items }
21
+ @common_response.work do
22
+ response = @rest_client.shipping_options(params)
23
+ return response unless response.success?
24
+
25
+ response
26
+ .data[:shipping_options]
27
+ .each { |option| shipping_option_mapper(option) }
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def shipping_option_mapper(option)
34
+ @common_response.data[:shipping_options] <<
35
+ Resources::Purchase::ShippingOption.from_hash(option)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -5,7 +5,6 @@ module MusicTodayApiWrapper
5
5
  attr_accessor :user, :api_key, :url, :catalog
6
6
 
7
7
  def initialize
8
- Dotenv.load
9
8
  @user = ENV['MUSIC_TODAY_USER']
10
9
  @api_key = ENV['MUSIC_TODAY_API_KEY']
11
10
  @url = ENV['MUSIC_TODAY_BASE_URL']
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: 16.12.15.03
4
+ version: 18.12.15.01
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Gonzaga
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-12-16 00:00:00.000000000 Z
12
+ date: 2015-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -121,6 +121,20 @@ dependencies:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
123
  version: '3.0'
124
+ - !ruby/object:Gem::Dependency
125
+ name: webmock
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '1.22'
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '1.22'
124
138
  - !ruby/object:Gem::Dependency
125
139
  name: bundler
126
140
  requirement: !ruby/object:Gem::Requirement
@@ -176,11 +190,15 @@ extensions: []
176
190
  extra_rdoc_files: []
177
191
  files:
178
192
  - lib/music_today_api_wrapper.rb
193
+ - lib/resources/address.rb
179
194
  - lib/resources/image.rb
180
195
  - lib/resources/product.rb
196
+ - lib/resources/purchase/item.rb
197
+ - lib/resources/purchase/shipping_option.rb
181
198
  - lib/rest_clients/common_response.rb
182
199
  - lib/rest_clients/music_today_rest_client.rb
183
200
  - lib/services/product_services.rb
201
+ - lib/services/shipping_services.rb
184
202
  - lib/support/configuration.rb
185
203
  homepage: https://github.com/neonroots/music_today_api_wrapper
186
204
  licenses: