best_buy 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +6 -0
- data/bestbuy.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/bestbuy.rb +4 -0
- data/lib/bestbuy/api_error.rb +5 -0
- data/lib/bestbuy/client.rb +23 -0
- data/lib/bestbuy/request.rb +100 -0
- data/lib/bestbuy/version.rb +3 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e295eefcc01f8b347c6db1f57c6fd6beecf3e148
|
4
|
+
data.tar.gz: 01e25099bb14c2c4a1061862f1d1fd201958ffd9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f0a5a82b5efa7f2f41b0c567508e05440153125d4c9ad34415704d1caf32212bec97d14242e71385d40ef551aa002b5eed3ec0d30f8d19fe7864e27c844ba45
|
7
|
+
data.tar.gz: e3b396e54d3b0582f80fd042e4c0a4a10b9b652c818f0256e200a22b353cc26ad49bfcfb99dafa9754352b91e5edabf27752b65e613617fae9ef675e44a706bc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 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,59 @@
|
|
1
|
+
# BestBuy
|
2
|
+
|
3
|
+
The BestBuy gem lets you request information from the BestBuy API in your Ruby applications. The API requires you to have an API key before you can make any requests, so make sure you obtain one [here](https://remix.mashery.com/member/register) first.
|
4
|
+
|
5
|
+
**Disclaimer**: I am not affiliated with Best Buy in any way. I built this because it was useful to me and I'm releasing it in the hopes that it will be useful to others.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'bestbuy'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install bestbuy
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
First, require the BestBuy gem where you want to make requests to the API using:
|
26
|
+
|
27
|
+
```
|
28
|
+
require 'best_buy'
|
29
|
+
```
|
30
|
+
|
31
|
+
Next, you will need to instantiate a `BestBuy::Client` with your API key:
|
32
|
+
|
33
|
+
```
|
34
|
+
bby = BestBuy::Client(api_key: '1234567890')
|
35
|
+
```
|
36
|
+
|
37
|
+
Once you've created a client, you can access the various endpoints of the BestBuy API as methods off of the BestBuy client. Most options are passed as hash parameters to these methods.
|
38
|
+
|
39
|
+
```
|
40
|
+
bby.products(upc: '004815162342')
|
41
|
+
|
42
|
+
#=> [#<BestBuy::Product:0xDEADBEEF>, ...]
|
43
|
+
```
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
|
+
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/timraymond/bestbuy.
|
54
|
+
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
59
|
+
|
data/Rakefile
ADDED
data/bestbuy.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bestbuy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "best_buy"
|
8
|
+
spec.version = BestBuy::VERSION
|
9
|
+
spec.authors = ["Timothy Raymond"]
|
10
|
+
spec.email = ["traymond@reviewed.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A gem for requesting information from the BestBuy API }
|
13
|
+
spec.homepage = "http://github.com/reviewed/bestbuy"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'multi_json', "~> 1.11"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
26
|
+
spec.add_development_dependency "yard", "~> 0"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bestbuy"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/bestbuy.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module BestBuy
|
2
|
+
# A BestBuy::Client allows queries to be constructed to the BestBuy API
|
3
|
+
class Client
|
4
|
+
# @param api_key[String] The API Key for making requests to the BestBuy API. Get one at https://remix.mashery.com/member/register
|
5
|
+
def initialize(api_key: nil, affiliate_tracking_id: nil)
|
6
|
+
raise ArgumentError, "API Key not set" unless api_key
|
7
|
+
@api_key = api_key
|
8
|
+
@affiliate_tracking_id = affiliate_tracking_id
|
9
|
+
end
|
10
|
+
|
11
|
+
# Issues a request for products held in the BestBuy API
|
12
|
+
#
|
13
|
+
# @param params[Hash] Parameters passed to the products API call which filter the result set. Parameters are combined by logical OR
|
14
|
+
# @return Array<Hash> Products that were found in the BestBuy API
|
15
|
+
def products(**params)
|
16
|
+
filters = params.map {|key, value| "#{key}=#{value}"}
|
17
|
+
BestBuy::Request.new(api_key: @api_key,
|
18
|
+
affiliate_tracking_id: @affiliate_tracking_id,
|
19
|
+
endpoint: 'products',
|
20
|
+
filters: filters)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module BestBuy
|
5
|
+
# Represents an unfulfilled request to the BestBuy API. Fullfillment of the
|
6
|
+
# request can be triggered by invoking Request#call. Request also provides
|
7
|
+
# methods to inspect the request that will be made to the BestBuy API
|
8
|
+
class Request
|
9
|
+
# Endpoints that are supported by this API client
|
10
|
+
VALID_ENDPOINTS = [
|
11
|
+
'products',
|
12
|
+
]
|
13
|
+
# @param api_key[String] The API key required by the BestBuy API
|
14
|
+
# @param endpoint[String] The endpoint of the API that this request will be made against. Must be one of VALID_ENDPOINTS
|
15
|
+
# @param filters[Array<String>] Filters that will be applied to the particular resource being requested.
|
16
|
+
def initialize(api_key:, endpoint:, affiliate_tracking_id: nil, filters: [])
|
17
|
+
unless VALID_ENDPOINTS.include? endpoint
|
18
|
+
fail APIError, "The endpoint \"#{endpoint}\" is currently unsupported. Supported endpoints are: #{VALID_ENDPOINTS.join(", ")}"
|
19
|
+
end
|
20
|
+
@endpoint = endpoint
|
21
|
+
@filters = filters
|
22
|
+
@affiliate_tracking_id = affiliate_tracking_id
|
23
|
+
@api_key = api_key
|
24
|
+
@show_params = []
|
25
|
+
end
|
26
|
+
|
27
|
+
# @returns [String] The URL that will be used for this Request
|
28
|
+
def to_s
|
29
|
+
"https://api.bestbuy.com/v1/#{@endpoint}(#{@filters.join('|')})?#{query_string}"
|
30
|
+
end
|
31
|
+
|
32
|
+
# Converts the request into a cURL request for debugging purposes
|
33
|
+
#
|
34
|
+
# @returns [String] An eval-able string that will make a request using cURL to the BestBuy API
|
35
|
+
def to_curl
|
36
|
+
"curl #{to_s}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# Sets the request to only return the specified fields from the API
|
40
|
+
#
|
41
|
+
# @example Pluck attributes
|
42
|
+
# bby = BestBuy::Client.new(api_key: '12345')
|
43
|
+
# bby.products(upc: '004815162342').pluck(:name).call
|
44
|
+
# #=> [{:name => "MegaProduct 5000"}]
|
45
|
+
# @param requested_fields [Array<Symbol>] The requested fields that should be included in product results
|
46
|
+
# @return BestBuy::Request The augmented request
|
47
|
+
def pluck(*requested_fields)
|
48
|
+
@show_params = requested_fields.map(&:to_s)
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
# Synchronously executes a request to the BestBuy API. This will block until results are ready and have been parsed
|
53
|
+
#
|
54
|
+
# @return Array<Hash> The results returned from the API.
|
55
|
+
def call
|
56
|
+
resp = ::Net::HTTP.get URI(to_s)
|
57
|
+
::MultiJson.decode(resp)['products']
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns the query string that will be used for this request. Query string parameters are returned in alphabetical order.
|
61
|
+
#
|
62
|
+
# @return String The query string for this request
|
63
|
+
def query_string
|
64
|
+
[
|
65
|
+
api_key_param,
|
66
|
+
format_param,
|
67
|
+
show_param,
|
68
|
+
affiliate_param,
|
69
|
+
].compact.sort.join("&")
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# Inserts the query string parameter responsible for crediting affiliate links
|
75
|
+
def affiliate_param
|
76
|
+
if @affiliate_tracking_id
|
77
|
+
"LID=#{@affiliate_tracking_id}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Controls the apiKey query string parameter
|
82
|
+
def api_key_param
|
83
|
+
"apiKey=#{@api_key}"
|
84
|
+
end
|
85
|
+
|
86
|
+
# Changes the response format. The API accepts XML, but this gem requres JSON
|
87
|
+
def format_param
|
88
|
+
"format=json"
|
89
|
+
end
|
90
|
+
|
91
|
+
# Controls the fields returned for API response items. Corresponsds to the "show" query parameter
|
92
|
+
def show_param
|
93
|
+
if @show_params && @show_params.length > 0
|
94
|
+
"show=#{@show_params.join(",")}"
|
95
|
+
else
|
96
|
+
nil
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: best_buy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Timothy Raymond
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multi_json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
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.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- traymond@reviewed.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bestbuy.gemspec
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/bestbuy.rb
|
101
|
+
- lib/bestbuy/api_error.rb
|
102
|
+
- lib/bestbuy/client.rb
|
103
|
+
- lib/bestbuy/request.rb
|
104
|
+
- lib/bestbuy/version.rb
|
105
|
+
homepage: http://github.com/reviewed/bestbuy
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.4.8
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: A gem for requesting information from the BestBuy API
|
129
|
+
test_files: []
|
130
|
+
has_rdoc:
|