bestbuy_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d5f754a396c74d1b261ee6a128194d3ac2edc688621ba22b31963dd9611d7597
4
+ data.tar.gz: 962ade249e82c4cfa4592da1d928ead4c4a4b5efcd156f080b51078cc353a091
5
+ SHA512:
6
+ metadata.gz: ab324760e5daefe668c269db7a3ca8f80e3c81bd4927832c3b0231f1230e94514ba955298237fe68d1f115474fae7a31a79cbf253ca3f87759fbed8dc8e40dcb
7
+ data.tar.gz: 7cdfb22fc14a3ec62788955b467a21cfeab9f983a35c93626b37e408f8bd0aa9e13bcfcb4184d943d76095bb47ef5464737786156c766d576d5ad5d68695fd77
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Metrics/BlockLength:
4
+ Max: 150
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bestbuy_api.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Best Buy API
2
+
3
+ A Ruby wrapper for the [Best Buy developer API](https://developer.bestbuy.com/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bestbuy_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bestbuy_api
20
+
21
+ ## Getting Started
22
+ Sign-up for a developer API Key at https://developer.bestbuy.com/
23
+
24
+ ## API Documentation
25
+
26
+ Documentation for the Best Buy API is located at [bestbuyapis.github.io](https://bestbuyapis.github.io/api-documentation/).
27
+
28
+ ## Usage
29
+
30
+ ``` ruby
31
+ require 'bestbuy_api'
32
+
33
+ BestbuyApi.config.api_key = 'YourBestbuyApiKey'
34
+
35
+ product = BestbuyApi::Product.select(:sku, :name, :sale_price, :url, :image)
36
+ .where(keyword: 'iPhone')
37
+ .limit(10).page(1)
38
+ product.items.each do |item|
39
+ item['sku']
40
+ end
41
+ ```
42
+
43
+ ## Find products
44
+
45
+ ``` ruby
46
+ product = BestbuyApi::Product.select(:sku, :name, :sale_price, :url, :image)
47
+ product.items
48
+ ```
49
+
50
+ ## Find categories
51
+
52
+ ``` ruby
53
+ category = BestbuyApi::Category.select(:id, :name, :subcategory_id, :subcategory_name)
54
+ category.items
55
+ ```
56
+
57
+ ## Find stores
58
+
59
+ ``` ruby
60
+ store = BestbuyApi::Store.select(:id, :city, :state, :postal_code)
61
+ store.items
62
+ ```
63
+
64
+ ## Selecting Specific Fields
65
+
66
+ ``` ruby
67
+ BestbuyApi::Product.select(category_id: 'abcat500')
68
+ ```
69
+
70
+ ## Conditions
71
+
72
+ ``` ruby
73
+ BestbuyApi::Product.where(category_id: 'abcat500')
74
+ ```
75
+
76
+ ## Limit and Offset
77
+
78
+ ``` ruby
79
+ BestbuyApi::Product.limit(20).page(2)
80
+ ```
81
+
82
+ ## Pagination
83
+
84
+ ``` ruby
85
+ product = BestbuyApi::Product.where(category_id: 'abcat500')
86
+ product.pagination or product.url
87
+ ```
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bestbuy_api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bestbuy_api"
8
+ spec.version = BestbuyApi::VERSION
9
+ spec.authors = ['Michael John Bayucot']
10
+ spec.email = ['mbayucot@gmail.com']
11
+
12
+ spec.summary = 'A Ruby wrapper for the Bestbuy API'
13
+ spec.homepage = 'https://github.com/mbayucot/bestbuy-api'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'httparty', '~> 0.13'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'simplecov', '~> 0.16.1'
29
+ spec.add_development_dependency 'vcr', '~> 4.0', '>= 4.0.0'
30
+ spec.add_development_dependency 'webmock', '~> 3.4', '>= 3.4.2'
31
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'model'
2
+
3
+ module BestbuyApi
4
+ # Defines the attributes for the categories api. Documentation is
5
+ # located at https://bestbuyapis.github.io/api-documentation/#categories-api.
6
+ class Category < Model
7
+ PATH = 'categories'.freeze
8
+
9
+ ATTRIBUTES = {
10
+ id: { search: true },
11
+ name: { search: true },
12
+ subcategory_id: { kw: 'subCategories.id' },
13
+ subcategory_name: { kw: 'subCategories.name' }
14
+ }.freeze
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ require 'forwardable'
2
+
3
+ module BestbuyApi
4
+ # Collect the parameters and validates the attributes
5
+ class Criteria
6
+ extend Forwardable
7
+ def_delegators :request, :items, :pagination, :url
8
+
9
+ def initialize(klass)
10
+ @klass = klass
11
+ end
12
+
13
+ def criteria
14
+ @criteria ||= { attributes: [], conditions: {} }
15
+ end
16
+
17
+ def select(args)
18
+ # Check on attributes that are readable
19
+ attributes = @klass::ATTRIBUTES.select { |_k, v| v.key?(:read) ? v[:read] : true }
20
+ args.each { |k, _v| assert_keys(attributes.keys, k) }
21
+
22
+ criteria[:attributes] = args
23
+ self
24
+ end
25
+
26
+ def where(args)
27
+ # Check on attributes that are searchable
28
+ attributes = @klass::ATTRIBUTES.select { |_k, v| v.key?(:search) && v[:search] }
29
+ args.each { |k, _v| assert_keys(attributes.keys, k) }
30
+
31
+ criteria[:conditions].merge!(args)
32
+ self
33
+ end
34
+
35
+ def limit(limit)
36
+ criteria[:limit] = limit.to_i
37
+ self
38
+ end
39
+
40
+ def page(page)
41
+ criteria[:page] = page.to_i
42
+ self
43
+ end
44
+
45
+ private
46
+
47
+ def request
48
+ @request ||= BestbuyApi::Request.new(@klass::PATH, @klass::ATTRIBUTES, criteria)
49
+ end
50
+
51
+ def assert_keys(valid_keys, key)
52
+ return true if valid_keys.include?(key)
53
+
54
+ raise MissingAttributeError, "Unknown attribute: #{key.inspect}. Valid attributes are: " \
55
+ "#{valid_keys.map(&:inspect).join(', ')}"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module BestbuyApi
2
+ class MissingApiKeyError < ArgumentError; end
3
+ class MissingAttributeError < ArgumentError; end
4
+ class RequestError < StandardError; end
5
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'criteria'
2
+
3
+ module BestbuyApi
4
+ # Chains method calls to build up queries
5
+ class Model
6
+ def self.select(*args)
7
+ Criteria.new(self).select(args.uniq)
8
+ end
9
+
10
+ def self.where(args)
11
+ Criteria.new(self).where(args)
12
+ end
13
+
14
+ def self.limit(args)
15
+ Criteria.new(self).limit(args)
16
+ end
17
+
18
+ def self.page(args)
19
+ Criteria.new(self).page(args)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'model'
2
+
3
+ module BestbuyApi
4
+ # Defines the attributes for the products api. Documentation is
5
+ # located at https://bestbuyapis.github.io/api-documentation/#products-api.
6
+ class Product < Model
7
+ PATH = 'products'.freeze
8
+
9
+ ATTRIBUTES = {
10
+ keyword: { kw: 'search', search: true, read: false },
11
+ sku: { search: true },
12
+ category_id: { kw: 'categoryPath.id', search: true },
13
+ category_name: { kw: 'categoryPath.name', search: true },
14
+ condition: { search: true },
15
+ customer_rating: { kw: 'customerReviewAverage', search: true },
16
+ manufacturer: { search: true },
17
+ regular_price: { kw: 'regularPrice' },
18
+ sale_price: { kw: 'salePrice' },
19
+ shipping_cost: { kw: 'shippingCost' },
20
+ reviews_count: { kw: 'customerReviewCount' },
21
+ discount: { kw: 'percentSavings' },
22
+ free_shipping: { kw: 'freeShipping' },
23
+ url: {}, name: {}, description: {}, image: {}
24
+ }.freeze
25
+ end
26
+ end
@@ -0,0 +1,81 @@
1
+ require 'httparty'
2
+
3
+ module BestbuyApi
4
+ # Wrap HTTParty gem to query the api
5
+ class Request
6
+ include HTTParty
7
+ base_uri 'https://api.bestbuy.com/v1'
8
+ PAGE_VARS = %w[totalPages total currentPage].freeze
9
+
10
+ def initialize(path, attributes, criteria)
11
+ # Get your API Key at https://developer.bestbuy.com/.
12
+ api_key = BestbuyApi.config.api_key
13
+ raise MissingApiKeyError, 'API Key is not defined' if api_key.nil?
14
+
15
+ @path = path
16
+ @attributes = attributes
17
+ @criteria = criteria
18
+ @query = { apiKey: api_key, format: 'json' }
19
+ end
20
+
21
+ def items
22
+ response[@path] if response.key?(@path)
23
+ end
24
+
25
+ def pagination
26
+ response.select { |k| PAGE_VARS.include?(k) }
27
+ end
28
+
29
+ def url
30
+ response.request.last_uri.to_s
31
+ end
32
+
33
+ private
34
+
35
+ def response
36
+ @response ||= find
37
+ end
38
+
39
+ def find
40
+ response = self.class.get("/#{@path}#{slug}", query: params)
41
+ raise RequestError, "#{response.code} Request Error" if response.code != 200
42
+
43
+ response
44
+ end
45
+
46
+ # Collect attributes that are readable and substitute using kw or key
47
+ def slug
48
+ attributes = @attributes.select { |_k, v| v.key?(:search) && v[:search] }
49
+ opts = @criteria[:conditions].map do |k, v|
50
+ attributes[k].key?(:kw) ? { attributes[k][:kw] => v } : { k => v }
51
+ end
52
+ opts = opts.reduce({}, :merge)
53
+
54
+ encode_slug(opts)
55
+ end
56
+
57
+ # Collect attributes that are searchable and substitute using kw or key
58
+ def params
59
+ attributes = @attributes.select { |_k, v| v.key?(:read) ? v[:read] : true }
60
+ opts = Array[@criteria[:attributes].map do |k|
61
+ attributes[k].key?(:kw) ? attributes[k][:kw] : k
62
+ end].join(',')
63
+
64
+ join_fields(opts)
65
+ end
66
+
67
+ def encode_slug(opts)
68
+ search = opts.delete('search') if opts.key?('search')
69
+ slug = URI.encode_www_form(opts)
70
+ slug = format("(search=#{search})%<and>s", and: ("&#{slug}" unless slug.empty?)) if search
71
+ return "(#{slug})" unless slug.empty?
72
+ end
73
+
74
+ def join_fields(opts)
75
+ params = @criteria.slice(:limit, :page)
76
+ params[:show] = opts unless opts.empty?
77
+ @query.merge!(params) unless params.empty?
78
+ @query
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'model'
2
+ require_relative 'request'
3
+
4
+ module BestbuyApi
5
+ # Defines the attributes for the stores api. Documentation is
6
+ # located at https://bestbuyapis.github.io/api-documentation/#stores-api.
7
+ class Store < Model
8
+ PATH = 'stores'.freeze
9
+
10
+ ATTRIBUTES = {
11
+ id: { kw: 'storeId', search: true },
12
+ city: { search: true },
13
+ state: { kw: 'region', search: true },
14
+ postal_code: { kw: 'fullPostalCode', search: true },
15
+ store_type: { kw: 'storeType' },
16
+ name: {}, address: {}, address2: {}, country: {},
17
+ hours: {}, phone: {}
18
+ }.freeze
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module BestbuyApi
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'bestbuy_api/exceptions'
2
+ require 'bestbuy_api/product'
3
+ require 'bestbuy_api/store'
4
+ require 'bestbuy_api/category'
5
+
6
+ # Handles BestBuy api queries. Documentation is located at
7
+ # https://bestbuyapis.github.io/api-documentation.
8
+ module BestbuyApi
9
+ class << self
10
+ attr_accessor :api_key
11
+
12
+ def config
13
+ self
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bestbuy_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael John Bayucot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.16.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.16.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 4.0.0
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '4.0'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 4.0.0
103
+ - !ruby/object:Gem::Dependency
104
+ name: webmock
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.4'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 3.4.2
113
+ type: :development
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '3.4'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 3.4.2
123
+ description:
124
+ email:
125
+ - mbayucot@gmail.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - ".gitignore"
131
+ - ".rspec"
132
+ - ".rubocop.yml"
133
+ - ".travis.yml"
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - bestbuy_api.gemspec
139
+ - lib/bestbuy_api.rb
140
+ - lib/bestbuy_api/category.rb
141
+ - lib/bestbuy_api/criteria.rb
142
+ - lib/bestbuy_api/exceptions.rb
143
+ - lib/bestbuy_api/model.rb
144
+ - lib/bestbuy_api/product.rb
145
+ - lib/bestbuy_api/request.rb
146
+ - lib/bestbuy_api/store.rb
147
+ - lib/bestbuy_api/version.rb
148
+ homepage: https://github.com/mbayucot/bestbuy-api
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.7.7
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: A Ruby wrapper for the Bestbuy API
172
+ test_files: []