music_today_api_wrapper 13.01.16.02 → 15.12.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 453f6084ada895525939a6ca3a4c7ccb2f627735
4
- data.tar.gz: 070878d2c5dae862c3e9bec68f403732361ae3e3
3
+ metadata.gz: 9e541eb609ffa5fbe4499ed9791a9e39b532aa08
4
+ data.tar.gz: e182d0e1933deab6a89d244b608e28e7ebce92f0
5
5
  SHA512:
6
- metadata.gz: 2b474571057baee48ff111cee2425a5af90e34b78edd22b6eb7de7ced976bb35f6fba8b164584d546716a8b13c69dc213029133533bc281fd5a0de0d7cd7c290
7
- data.tar.gz: 76fcf9926ffc645403accd580585cdf23eeaa757254fb7f81714e49b51b7ab74c77660a117001d6fa58419bd2fed898de536e3601365278788fbec20135a1662
6
+ metadata.gz: 4d946cd9e6c00737600e39902eb0dedade86a1393c9aa64757abf12e876f15ca0228bb6b3f1244278bbe6f2eb4ca14a43e3677d42971ddf4cb1ccca636780870
7
+ data.tar.gz: 280a82a38b10f4be8763d85586cee9b5b593c310cb33a6e3c59d3fea7b974edea49285461edae85cc61305ba98d52886e2ddd062c950e21ab045cc4acfedc463
@@ -0,0 +1,28 @@
1
+ require './lib/rest_clients/music_today_rest_client'
2
+ require './lib/resources/product'
3
+
4
+ module MusicTodayApiWrapper
5
+ class ProductServices
6
+ def initialize
7
+ @common_response = CommonResponse.new
8
+ @common_response.data[:products] = []
9
+ @rest_client = MusicTodayApiWrapper::RestClient.new
10
+ end
11
+
12
+ def all_products
13
+ results = @rest_client.all_products
14
+ return results unless results.success?
15
+
16
+ @common_response.work do
17
+ results.data[:products].each { |product| product_mapper(product) }
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def product_mapper(product)
24
+ product_obj = Product.from_hash(product)
25
+ @common_response.data[:products] << product_obj
26
+ end
27
+ end
28
+ end
@@ -1,7 +1,5 @@
1
1
  require 'support/configuration'
2
- require 'services/product_services'
3
- require 'services/shipping_services'
4
- require 'services/checkout_services'
2
+ require 'domain/product_services'
5
3
 
6
4
  module MusicTodayApiWrapper
7
5
  class << self
@@ -13,23 +11,8 @@ module MusicTodayApiWrapper
13
11
  yield(configuration)
14
12
  end
15
13
 
16
- def self.products(per_page = nil, page_number = nil)
17
- product_services = Services::ProductServices.new
18
- product_services.all_products(per_page, page_number)
19
- end
20
-
21
- def self.find_product(id)
22
- product_services = Services::ProductServices.new
23
- product_services.find_product(id)
24
- end
25
-
26
- def self.shipping_options(address, items)
27
- shipping_services = Services::ShippingServices.new
28
- shipping_services.options(address, items)
29
- end
30
-
31
- def self.checkout(address, items)
32
- checkout_services = Services::CheckoutServices.new
33
- checkout_services.checkout(address, items)
14
+ def self.products
15
+ product_services = ProductServices.new
16
+ product_services.all_products
34
17
  end
35
18
  end
@@ -1,13 +1,9 @@
1
- module MusicTodayApiWrapper
2
- module Resources
3
- class Image
4
- attr_accessor :short, :medium, :large
1
+ class Image
2
+ attr_accessor :short, :medium, :large
5
3
 
6
- def initialize(short = nil, medium = nil, large = nil)
7
- @short = short
8
- @medium = medium
9
- @large = large
10
- end
11
- end
4
+ def initialize(short = nil, medium = nil, large = nil)
5
+ @short = short
6
+ @medium = medium
7
+ @large = large
12
8
  end
13
9
  end
@@ -1,47 +1,21 @@
1
- require 'resources/image'
2
- require 'resources/variant'
1
+ require './lib/resources/image'
3
2
 
4
- module MusicTodayApiWrapper
5
- module Resources
6
- class Product
7
- attr_accessor :uid,
8
- :name,
9
- :description,
10
- :category,
11
- :image,
12
- :price,
13
- :variants
3
+ class Product
4
+ attr_accessor :name, :description, :category, :image
14
5
 
15
- # rubocop:disable ParameterLists
16
- def initialize(uid, name, description, category, price,
17
- image = MusicTodayApiWrapper::Resources::Image.new, variants = [])
18
- @uid = uid
19
- @name = name
20
- @description = description
21
- @category = category
22
- @price = price
23
- @image = image
24
- @variants = variants
25
- end
26
-
27
- # rubocop:disable AbcSize
28
- def self.from_hash(hash)
29
- image = Image.new(hash['imageUrlSmall'],
30
- hash['imageUrlMedium'],
31
- hash['imageUrlLarge'])
6
+ def initialize(name, description, category, image = Image.new)
7
+ @name = name
8
+ @description = description
9
+ @category = category
10
+ @image = image
11
+ end
32
12
 
33
- product = Product.new(hash['id'],
34
- hash['name'],
35
- hash['lang']['en']['textDesc'],
36
- hash['categoryName'],
37
- hash['listPrice'],
38
- image)
13
+ def self.from_hash(product_hash)
14
+ image = Image.new(product_hash['imageUrlSmall'],
15
+ product_hash['imageUrlMedium'],
16
+ product_hash['imageUrlLarge'])
39
17
 
40
- hash['variants'].each do |variant|
41
- product.variants << Variant.from_hash(variant)
42
- end
43
- product
44
- end
45
- end
18
+ Product.new(product_hash['name'], product_hash['lang']['en']['textDesc'],
19
+ product_hash['categoryName'], image)
46
20
  end
47
21
  end
@@ -1,24 +1,20 @@
1
- module MusicTodayApiWrapper
2
- module RestClients
3
- class CommonResponse
4
- attr_accessor :data, :errors
1
+ class CommonResponse
2
+ attr_accessor :data, :errors
5
3
 
6
- def initialize(data = {}, errors = [])
7
- @data = data
8
- @errors = errors
9
- end
4
+ def initialize(data = {}, errors = [])
5
+ @data = data
6
+ @errors = errors
7
+ end
10
8
 
11
- def work
12
- yield
13
- self
14
- rescue StandardError => error
15
- @errors.push(error.message)
16
- self
17
- end
9
+ def work
10
+ yield
11
+ self
12
+ rescue StandardError => error
13
+ @errors.push(error.message)
14
+ self
15
+ end
18
16
 
19
- def success?
20
- @errors.empty?
21
- end
22
- end
17
+ def success?
18
+ @errors.empty?
23
19
  end
24
20
  end
@@ -1,81 +1,25 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
- require 'rest_clients/common_response'
4
- require 'support/configuration'
3
+ require './lib/rest_clients/common_response'
4
+ require './lib/support/configuration'
5
5
 
6
6
  module MusicTodayApiWrapper
7
7
  class RestClient
8
- attr_accessor :catalog_number
9
-
10
8
  def initialize
11
9
  config = MusicTodayApiWrapper::Configuration.new
12
10
  @url = config.url
13
11
  @user = config.user
14
12
  @api_key = config.api_key
15
- @catalog_number = config.catalog.to_i
16
- @department = config.department
17
- @common_response = MusicTodayApiWrapper::RestClients::CommonResponse.new
18
- end
19
-
20
- def all_products(per_page = nil, page_number = nil)
21
- @common_response.work do
22
- options = {}
23
- options[:size] = per_page if per_page
24
- options[:page] = page_number if page_number
25
- department = @department ? "/#{@department}" : ''
26
-
27
- url = "#{@url}/catalog/content/#{@catalog_number}#{department}/"
28
- @common_response.data[:products] = get(url, options)['products']
29
- end
13
+ @common_response = CommonResponse.new
30
14
  end
31
15
 
32
- def find_product(product_id)
16
+ def all_products
33
17
  @common_response.work do
34
- url = "#{@url}/catalog/product/#{@catalog_number}/#{product_id}"
35
- @common_response.data[:product] = get(url)['product']
18
+ uri =
19
+ URI("#{@url}/catalog/content/1/?apiuser=#{@user}&apikey=#{@api_key}")
20
+ res = Net::HTTP.get_response(uri)
21
+ @common_response.data[:products] = JSON.parse(res.body)['products']
36
22
  end
37
23
  end
38
-
39
- def shipping_options(params)
40
- @common_response.work do
41
- url = "#{@url}/order/shippingOptionsGet"
42
- @common_response.data[:shipping_options] =
43
- post(url, {}, params)['shippingOptions']
44
- end
45
- end
46
-
47
- def checkout(params)
48
- @common_response.work do
49
- url = "#{@url}/cart/pricing"
50
- @common_response.data[:session] =
51
- post(url, {}, params)['addresses'].first
52
- end
53
- end
54
-
55
- private
56
-
57
- def get(url, options = {})
58
- uri = URI(url)
59
- hit(uri, options) do
60
- Net::HTTP.get_response(uri)
61
- end
62
- end
63
-
64
- def post(url, options = {}, body = {})
65
- uri = URI(url)
66
- hit(uri, options) do
67
- request = Net::HTTP::Post.new(uri)
68
- request.body = body.to_json
69
- request.content_type = 'application/json'
70
- Net::HTTP.start(uri.hostname) { |http| http.request(request) }
71
- end
72
- end
73
-
74
- def hit(uri, options = {})
75
- options.merge!(apiuser: @user, apikey: @api_key)
76
- uri.query = URI.encode_www_form(options)
77
- response = yield
78
- JSON.parse(response.body)
79
- end
80
24
  end
81
25
  end
@@ -1,13 +1,14 @@
1
+ require 'dotenv'
2
+
1
3
  module MusicTodayApiWrapper
2
4
  class Configuration
3
- attr_accessor :user, :api_key, :url, :catalog, :department
5
+ attr_accessor :user, :api_key, :url
4
6
 
5
7
  def initialize
8
+ Dotenv.load
6
9
  @user = ENV['MUSIC_TODAY_USER']
7
10
  @api_key = ENV['MUSIC_TODAY_API_KEY']
8
11
  @url = ENV['MUSIC_TODAY_BASE_URL']
9
- @catalog = ENV['MUSIC_TODAY_CATALOG_NAME']
10
- @department = ENV['MUSIC_TODAY_DEPARTMENT_NAME'] || nil
11
12
  end
12
13
  end
13
14
  end
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: 13.01.16.02
4
+ version: 15.12.15
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: 2016-01-13 00:00:00.000000000 Z
12
+ date: 2015-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -121,20 +121,6 @@ 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'
138
124
  - !ruby/object:Gem::Dependency
139
125
  name: bundler
140
126
  requirement: !ruby/object:Gem::Requirement
@@ -189,21 +175,12 @@ executables: []
189
175
  extensions: []
190
176
  extra_rdoc_files: []
191
177
  files:
178
+ - lib/domain/product_services.rb
192
179
  - lib/music_today_api_wrapper.rb
193
- - lib/resources/address.rb
194
- - lib/resources/checkout/session.rb
195
- - lib/resources/hash.rb
196
180
  - lib/resources/image.rb
197
181
  - lib/resources/product.rb
198
- - lib/resources/purchase/item.rb
199
- - lib/resources/purchase/shipping_option.rb
200
- - lib/resources/string.rb
201
- - lib/resources/variant.rb
202
182
  - lib/rest_clients/common_response.rb
203
183
  - lib/rest_clients/music_today_rest_client.rb
204
- - lib/services/checkout_services.rb
205
- - lib/services/product_services.rb
206
- - lib/services/shipping_services.rb
207
184
  - lib/support/configuration.rb
208
185
  homepage: https://github.com/neonroots/music_today_api_wrapper
209
186
  licenses:
@@ -1,48 +0,0 @@
1
- require 'resources/purchase/shipping_option'
2
- require 'resources/hash'
3
-
4
- module MusicTodayApiWrapper
5
- module Resources
6
- class Address
7
- attr_accessor :street,
8
- :street_second_line,
9
- :city,
10
- :postal_code,
11
- :country,
12
- :shipping_options,
13
- :selected_shipping
14
-
15
- # rubocop:disable ParameterLists
16
- def initialize(street, state, city, postal_code, country = 'US',
17
- street_second_line = '', shipping_options = [],
18
- selected_shipping = nil)
19
- @street = street
20
- @state = state
21
- @street_second_line = street_second_line
22
- @city = city
23
- @postal_code = postal_code
24
- @country = country
25
- @shipping_options = shipping_options
26
- @selected_shipping = selected_shipping
27
- end
28
-
29
- def self.from_hash(address_hash)
30
- Address.new(address_hash['street'],
31
- address_hash['city'],
32
- address_hash['postalCode'],
33
- address_hash['country'],
34
- address_hash['street2'])
35
- end
36
-
37
- def as_hash
38
- { street: @street,
39
- street2: @street_second_line,
40
- state: @state,
41
- city: @city,
42
- postalCode: @postal_code,
43
- country: @country,
44
- shippingOptionType: @selected_shipping }.compact
45
- end
46
- end
47
- end
48
- end
@@ -1,27 +0,0 @@
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
@@ -1,8 +0,0 @@
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,35 +0,0 @@
1
- require 'resources/variant'
2
- require 'resources/hash'
3
-
4
- module MusicTodayApiWrapper
5
- module Resources
6
- module Purchase
7
- class Item
8
- attr_accessor :variant, :quantity
9
-
10
- def initialize(variant = Variant.new, quantity = 1, tax = nil,
11
- total = nil)
12
- @variant = variant
13
- @quantity = quantity
14
- @tax = tax
15
- @total = total
16
- end
17
-
18
- def as_hash
19
- { sku: @variant.sku,
20
- qty: @quantity,
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'])
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,31 +0,0 @@
1
- require 'resources/hash'
2
-
3
- module MusicTodayApiWrapper
4
- module Resources
5
- module Purchase
6
- class ShippingOption
7
- attr_accessor :type, :delivery_date, :rate, :description
8
-
9
- def initialize(type, delivery_date = nil, rate = nil,
10
- description = nil)
11
- @type = type
12
- @delivery_date = delivery_date ? Date.parse(delivery_date) : nil
13
- @rate = rate
14
- @description = description
15
- end
16
-
17
- def self.from_hash(option)
18
- ShippingOption.new(option['shippingOptionType'],
19
- option['deliveryDate'],
20
- option['totalRate'],
21
- option['shippingOptionName'])
22
- end
23
-
24
- def as_hash
25
- { shippingOptionType: @type, deliveryDate: @delivery_date,
26
- totalRate: @rate, shippingOptionName: @description }.compact
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,13 +0,0 @@
1
- module MusicTodayApiWrapper
2
- # rubocop:disable ClassAndModuleChildren
3
- class ::String
4
- def to_underscore!
5
- gsub!(/(.)([A-Z])/, '\1_\2')
6
- downcase!
7
- end
8
-
9
- def to_underscore
10
- dup.tap(&:to_underscore!)
11
- end
12
- end
13
- end
@@ -1,39 +0,0 @@
1
- require 'resources/string'
2
- module MusicTodayApiWrapper
3
- module Resources
4
- class Variant
5
- attr_accessor :sku,
6
- :price,
7
- :quantity_available,
8
- :variant_names
9
-
10
- def initialize(sku, price, quantity_available, variant_names = [])
11
- @sku = sku
12
- @price = price
13
- @quantity_available = quantity_available
14
- @variant_names = variant_names
15
- end
16
-
17
- def self.from_hash(variant_hash)
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 = []
27
-
28
- names.each do |variant|
29
- variant_name = {}
30
- variant_name[variant.keys[0]
31
- .to_underscore
32
- .to_sym] = variant.values[0]
33
- symbolized_names << variant_name
34
- end
35
- symbolized_names
36
- end
37
- end
38
- end
39
- end
@@ -1,39 +0,0 @@
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
@@ -1,43 +0,0 @@
1
- require 'rest_clients/music_today_rest_client'
2
- require 'resources/product'
3
-
4
- module MusicTodayApiWrapper
5
- module Services
6
- class ProductServices
7
- def initialize
8
- @common_response =
9
- MusicTodayApiWrapper::RestClients::CommonResponse.new
10
- @rest_client = MusicTodayApiWrapper::RestClient.new
11
- end
12
-
13
- def all_products(per_page = nil, page_number = nil)
14
- results = @rest_client.all_products(per_page, page_number)
15
- return results unless results.success?
16
-
17
- @common_response.data[:products] = []
18
- @common_response.work do
19
- results.data[:products].each { |product| product_mapper(product) }
20
- end
21
- end
22
-
23
- def find_product(id)
24
- results = @rest_client.find_product(id)
25
- return results unless results.success?
26
-
27
- @common_response.work do
28
- @common_response.data[:product] =
29
- MusicTodayApiWrapper::Resources::Product
30
- .from_hash(results.data[:product])
31
- end
32
- end
33
-
34
- private
35
-
36
- def product_mapper(product)
37
- product_obj =
38
- MusicTodayApiWrapper::Resources::Product.from_hash(product)
39
- @common_response.data[:products] << product_obj
40
- end
41
- end
42
- end
43
- end
@@ -1,38 +0,0 @@
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 = RestClients::CommonResponse.new
9
- @common_response.data[:shipping_options] = []
10
- @rest_client = RestClient.new
11
- end
12
-
13
- def options(address, items)
14
- hash_items = []
15
- items.each { |item| hash_items << item.as_hash }
16
-
17
- params = { storeId: @rest_client.catalog_number,
18
- address: address.as_hash,
19
- lineItems: hash_items }
20
- @common_response.work do
21
- response = @rest_client.shipping_options(params)
22
- return response unless response.success?
23
-
24
- response
25
- .data[:shipping_options]
26
- .each { |option| shipping_option_mapper(option) }
27
- end
28
- end
29
-
30
- private
31
-
32
- def shipping_option_mapper(option)
33
- @common_response.data[:shipping_options] <<
34
- Resources::Purchase::ShippingOption.from_hash(option)
35
- end
36
- end
37
- end
38
- end