flavordb-ruby 0.0.1
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 +15 -0
- data/.gitignore +18 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +89 -0
- data/Rakefile +8 -0
- data/flavordb.gemspec +28 -0
- data/lib/flavordb.rb +32 -0
- data/lib/flavordb/base.rb +33 -0
- data/lib/flavordb/business.rb +27 -0
- data/lib/flavordb/client.rb +131 -0
- data/lib/flavordb/product.rb +32 -0
- data/lib/flavordb/product_category.rb +43 -0
- data/lib/flavordb/version.rb +3 -0
- data/spec/business_spec.rb +30 -0
- data/spec/client_spec.rb +62 -0
- data/spec/product_category_spec.rb +49 -0
- data/spec/product_spec.rb +34 -0
- data/spec/spec_helper.rb +12 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OGZlYjg2M2M2MWE4N2NjMDdjMGU0YjM3MmYzMDAwZjY1ODdlZDljNQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjgxNjZkY2I0YzA3NzhlNWMzNDE1NTZkNzc1YWViN2UxOWY5MGRhZA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDcwOWFhNzQzYTIwMzZlZmRiYzJhYzMwMzczYWViNGU3NDYyNmNjY2FiNGFl
|
10
|
+
ZjQ5NTQ1MjgxY2E5NzY5OGU3OTljZGY2MzFlMmMzNTJjY2UzZjY0NGFmMGYy
|
11
|
+
NWU2MmFlYmVlNTg1YzQ3Mzg4ZmNiMDk0NjViYjc3MTEzZjI1MTU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTFmMGQwNDRlMzFjYzc3ZDY1NTJkZWFhNjVmMmEyNmIyZjA5NDlhMDIxODMy
|
14
|
+
YTMzOGQzNGYwNWY3OWRhZjllYWRhMzY0ZDRiMTYzNTY3OTlmYWI2NGIyNzll
|
15
|
+
N2ZkYTVlYWQ0OTEwZDc1MzI0ZGRiMzk0YmQ2NzFlYTU0OGFhY2U=
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
flavordb-ruby
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0.0
|
4
|
+
- 1.9.3
|
5
|
+
- 1.8.7
|
6
|
+
env:
|
7
|
+
global:
|
8
|
+
- secure: tklYdNk9OVh9usXyyPlpsw7srJ+u9Py1Hyqv7YsB9iu5YfImXDE7xwiTTx42eI/MEyVzNFb5CLYYECrDB9e89u8yTnrsaPfvINf2feMrkk/q4wVIa/zt/fD/v5oucAvZ41E04YJBw+Kr/gQiEiZiwV8iQTRDnRBBFzXFR7i/Ye8=
|
9
|
+
- secure: KqKfLVlg+yos3AVEeuvnk5UKY3SuF02nFHpll7KL1UXVoshLgTsTSdhNqaguJ7v8NyHUOqgoHI3WwM2QQQ2UMOsBhb933J9Y4nqWXM25ig2sjfJtLaACc5edmY09VU80GMMq29X5j9ikw4VCoE1tl5Yts0JZH0ks8z1Q1pXNDnc=
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Michael North
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
[](https://travis-ci.org/TrueNorth/flavordb-ruby)
|
2
|
+
# Flavordb
|
3
|
+
|
4
|
+
TODO: Write a gem description
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'flavordb'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install flavordb
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### API Credentials
|
23
|
+
It's best to store your API credentials as environment variables
|
24
|
+
|
25
|
+
./config/initializers/flavordb.rb
|
26
|
+
```ruby
|
27
|
+
ENV['FLAVORDB_API_KEY'] = '93a5da2ed....'
|
28
|
+
ENV['FLAVORDB_API_SECRET'] = '7ff85916b58f...'
|
29
|
+
```
|
30
|
+
|
31
|
+
### Working with the data
|
32
|
+
```ruby
|
33
|
+
# Create an instance of the API client
|
34
|
+
client = Flavordb::Client.new
|
35
|
+
|
36
|
+
# get a product category by id
|
37
|
+
product_category = client.get_product_category(1)
|
38
|
+
# subcategories
|
39
|
+
product_category.subcategories.each do |subcategory|
|
40
|
+
puts subcategory.name
|
41
|
+
end
|
42
|
+
# parent category
|
43
|
+
parent_category = product_category.parent_category
|
44
|
+
# products
|
45
|
+
product_array = product_category.products
|
46
|
+
|
47
|
+
|
48
|
+
# get a product by id
|
49
|
+
product = client.get_product(1234)
|
50
|
+
|
51
|
+
# get a business by id
|
52
|
+
business = client.get_business(150)
|
53
|
+
# products
|
54
|
+
business_products = business.products
|
55
|
+
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
(The MIT License.)
|
70
|
+
|
71
|
+
© 2009–2013 Michael North
|
72
|
+
|
73
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
74
|
+
of this software and associated documentation files (the "Software"), to deal
|
75
|
+
in the Software without restriction, including without limitation the rights
|
76
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
77
|
+
copies of the Software, and to permit persons to whom the Software is
|
78
|
+
furnished to do so, subject to the following conditions:
|
79
|
+
|
80
|
+
The above copyright notice and this permission notice shall be included in all
|
81
|
+
copies or substantial portions of the Software.
|
82
|
+
|
83
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
84
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
85
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
86
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
87
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
88
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
89
|
+
SOFTWARE.
|
data/Rakefile
ADDED
data/flavordb.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flavordb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flavordb-ruby"
|
8
|
+
spec.version = Flavordb::VERSION
|
9
|
+
spec.authors = ["Michael North"]
|
10
|
+
spec.email = ["michael.north@truenorthapps.com"]
|
11
|
+
spec.description = %q{A ruby client for the FlavorDB API}
|
12
|
+
spec.summary = %q{An awesome object-oriented way of working with beer, wine and whiskey data}
|
13
|
+
spec.homepage = "http://developers.flavordb.com/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.rubyforge_project = "flavordb-ruby"
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "json"
|
24
|
+
spec.add_dependency "typhoeus"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
end
|
data/lib/flavordb.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'flavordb/version'
|
2
|
+
require 'flavordb/client'
|
3
|
+
|
4
|
+
module Flavordb
|
5
|
+
|
6
|
+
class << self
|
7
|
+
attr_accessor :configuration
|
8
|
+
|
9
|
+
def configuration
|
10
|
+
if @configuration.nil?
|
11
|
+
@configuration = Configuration.new
|
12
|
+
end
|
13
|
+
@configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure
|
17
|
+
@configuration ||= Configuration.new
|
18
|
+
yield(@configuration)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
class Configuration
|
26
|
+
attr_accessor :verbose
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@verbose = true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Flavordb
|
2
|
+
class Base
|
3
|
+
attr_reader :id, :name, :resource
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def get_or_create (data)
|
7
|
+
#puts "GET OR CREATE #{data}"
|
8
|
+
object_id = data['id']
|
9
|
+
#puts "OBJECT ID #{object_id}"
|
10
|
+
if object_id.nil?
|
11
|
+
nil
|
12
|
+
else
|
13
|
+
object = object_cache[object_id]
|
14
|
+
if object.nil?
|
15
|
+
object = self.new data
|
16
|
+
object_cache[object_id] = object
|
17
|
+
end
|
18
|
+
object
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
protected
|
25
|
+
def initialize (data)
|
26
|
+
@id = data['id']
|
27
|
+
@name = data['name']
|
28
|
+
@resource = data['resource']
|
29
|
+
@description = data['description']
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'flavordb/base'
|
2
|
+
|
3
|
+
module Flavordb
|
4
|
+
class Business < Base
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def object_cache
|
8
|
+
@object_cache = {} if @object_cache.nil?
|
9
|
+
@object_cache
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def products(opts = {})
|
14
|
+
client = opts[:client] || Flavordb::Client.default_client
|
15
|
+
product_data = client.get_object_data_by_path "#{self.resource}/products"
|
16
|
+
product_data['data'].map {|p| Flavordb::Product.get_or_create p}
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def initialize data
|
21
|
+
super
|
22
|
+
@products_resource = data['productsResource']
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'flavordb/version'
|
2
|
+
require 'typhoeus'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'flavordb/business'
|
6
|
+
require 'flavordb/product_category'
|
7
|
+
require 'flavordb/product'
|
8
|
+
|
9
|
+
module Flavordb
|
10
|
+
|
11
|
+
class Client
|
12
|
+
|
13
|
+
attr_reader :api_endpoint, :api_key, :api_secret
|
14
|
+
|
15
|
+
class << self
|
16
|
+
attr_accessor :default_client
|
17
|
+
def initialize
|
18
|
+
@default_client = nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def initialize(key = ENV['FLAVORDB_API_KEY'], secret = ENV['FLAVORDB_API_SECRET'], opts = {})
|
24
|
+
@api_endpoint = opts[:api_endpoint] || default_api_endpoint
|
25
|
+
@api_key = key
|
26
|
+
@api_secret = secret
|
27
|
+
self.class.default_client = self #if self.class.default_client.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid?
|
31
|
+
if api_key.nil? || api_secret.nil?
|
32
|
+
return false
|
33
|
+
else
|
34
|
+
!get_oauth_token.nil?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_businesses(params)
|
39
|
+
raw_businesses = get_object_data_by_path "/businesses", params
|
40
|
+
if !raw_businesses['data'].nil?
|
41
|
+
raw_businesses['data'].map {|b| Business.get_or_create(b)}
|
42
|
+
else
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def find_products(params)
|
48
|
+
raw_products= get_object_data_by_path "/products", params
|
49
|
+
if !raw_products['data'].nil?
|
50
|
+
raw_products['data'].map {|b| Product.get_or_create(b)}
|
51
|
+
else
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_business(id)
|
57
|
+
raw_business = get_object_data_by_path("/businesses/#{id}")
|
58
|
+
!raw_business['data'].nil? ? Business.get_or_create(raw_business['data']) : nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def get_product_category(id)
|
62
|
+
raw_category = get_object_data_by_path("/product_categories/#{id}")
|
63
|
+
!raw_category['data'].nil? ? ProductCategory.get_or_create(raw_category['data']) : nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_product(id)
|
67
|
+
raw_product = get_object_data_by_path("/products/#{id}")
|
68
|
+
!raw_product['data'].nil? ? Product.get_or_create(raw_product['data']) : nil
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_object_data_by_path (path, api_params={})
|
72
|
+
url = (path =~ /http:\/{2}/).nil? ? "#{api_endpoint}#{path}" : path
|
73
|
+
authenticated_json_request :get, url, api_params
|
74
|
+
end
|
75
|
+
|
76
|
+
def api_token
|
77
|
+
if @api_token.nil?
|
78
|
+
@api_token = get_oauth_token
|
79
|
+
end
|
80
|
+
@api_token
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
def authenticated_json_request (method, url, api_params)
|
85
|
+
request = Typhoeus::Request.new url,
|
86
|
+
:method => method,
|
87
|
+
:headers => { 'Authorization' => "Bearer #{api_token}" },
|
88
|
+
:params => api_params
|
89
|
+
|
90
|
+
if Flavordb.configuration.verbose
|
91
|
+
request_url = request.url
|
92
|
+
request_url = "#{request_url}#{(request_url =~ /\?/).nil? ? '?' : '&'}access_token=#{api_token}"
|
93
|
+
puts "[Flavordb API]: #{request_url}"
|
94
|
+
end
|
95
|
+
|
96
|
+
response = request.run
|
97
|
+
if response.return_code == :ok
|
98
|
+
response_data = JSON.parse response.body
|
99
|
+
response_data
|
100
|
+
else
|
101
|
+
raise "Bad request: #{url}\n\tSTATUS: #{response.return_code}\n\tMESSAGE: #{response.return_message}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def get_oauth_token
|
106
|
+
server_response = Typhoeus.post 'http://www.flavordb.com/oauth/token', :params => {
|
107
|
+
:grant_type => 'client_credentials',
|
108
|
+
:client_id => api_key,
|
109
|
+
:client_secret => api_secret
|
110
|
+
}
|
111
|
+
begin
|
112
|
+
response_data = JSON.parse(server_response.body)
|
113
|
+
tok = response_data['access_token']
|
114
|
+
if !tok.nil?
|
115
|
+
@api_token = tok
|
116
|
+
@api_token
|
117
|
+
else
|
118
|
+
nil
|
119
|
+
end
|
120
|
+
rescue Exception => e
|
121
|
+
puts "ERROR: #{e.message}"
|
122
|
+
nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def default_api_endpoint
|
128
|
+
'http://api.flavordb.com/api/v1'
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'flavordb/base'
|
2
|
+
|
3
|
+
module Flavordb
|
4
|
+
class Product < Base
|
5
|
+
attr_reader :product_category_id, :business_id
|
6
|
+
class << self
|
7
|
+
def object_cache
|
8
|
+
@object_cache = {} if @object_cache.nil?
|
9
|
+
@object_cache
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def business(opts={})
|
14
|
+
client = opts[:client] || Flavordb::Client.default_client
|
15
|
+
client.get_business(business_id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def product_category(opts={})
|
19
|
+
client = opts[:client] || Flavordb::Client.default_client
|
20
|
+
client.get_product_category(product_category_id)
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
def initialize data
|
25
|
+
super
|
26
|
+
@product_category_id = data['productCategoryId']
|
27
|
+
@business_id = data['businessId']
|
28
|
+
@business_resource = data['business'] ? data['business']['resource'] : nil
|
29
|
+
@product_category_resource = data['productCategory'] ? data['productCategory']['resource'] : nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'flavordb/base'
|
2
|
+
|
3
|
+
module Flavordb
|
4
|
+
class ProductCategory < Base
|
5
|
+
attr_reader :parent_category_id
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def object_cache
|
9
|
+
@object_cache = {} if @object_cache.nil?
|
10
|
+
@object_cache
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize data
|
15
|
+
super
|
16
|
+
@is_root = data['rootCategory'] || false
|
17
|
+
@parent_category_id = data['parentCategoryId']
|
18
|
+
@products_resource = data['productsResource']
|
19
|
+
@subcategories_resource = data['subcategoriesResource']
|
20
|
+
end
|
21
|
+
|
22
|
+
def root?
|
23
|
+
@is_root
|
24
|
+
end
|
25
|
+
|
26
|
+
def parent_category(opts = {})
|
27
|
+
client = opts[:client] || Flavordb::Client.default_client
|
28
|
+
client.get_product_category(parent_category_id)
|
29
|
+
end
|
30
|
+
|
31
|
+
def products(opts = {})
|
32
|
+
client = opts[:client] || Flavordb::Client.default_client
|
33
|
+
product_data = client.get_object_data_by_path "#{self.resource}/products"
|
34
|
+
product_data['data'].map {|p| Flavordb::Product.get_or_create p}
|
35
|
+
end
|
36
|
+
|
37
|
+
def subcategories(opts = {})
|
38
|
+
client = opts[:client] || Flavordb::Client.default_client
|
39
|
+
subcategory_data = client.get_object_data_by_path "#{self.resource}/subcategories"
|
40
|
+
subcategory_data['data'].map {|sc| Flavordb::ProductCategory.get_or_create sc}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Flavordb::Business do
|
4
|
+
|
5
|
+
before (:all) do
|
6
|
+
@client = Flavordb::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
before (:each) do
|
10
|
+
@business = @client.get_business(142)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should know its resource" do
|
14
|
+
@business.resource.should_not == nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should know its name" do
|
18
|
+
@business.name.should_not == nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should know its id" do
|
22
|
+
@business.id.should == 142
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should retrieve its products on demand" do
|
26
|
+
prods = @business.products
|
27
|
+
prods.should_not == nil
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Flavordb::Client do
|
5
|
+
|
6
|
+
it "should use the API endpoint passed to it" do
|
7
|
+
client = Flavordb::Client.new(nil, nil, :api_endpoint => 'http://localhost:12345')
|
8
|
+
client.api_endpoint.should == 'http://localhost:12345'
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should use API credentials from environment variables" do
|
12
|
+
ENV['FLAVORDB_API_KEY'].should_not == nil?
|
13
|
+
ENV['FLAVORDB_API_SECRET'].should_not == nil?
|
14
|
+
client = Flavordb::Client.new
|
15
|
+
client.api_key.should == ENV['FLAVORDB_API_KEY']
|
16
|
+
client.api_secret.should == ENV['FLAVORDB_API_SECRET']
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should know it's invalid if initialized with invalid API credentials" do
|
20
|
+
client = Flavordb::Client.new(nil, nil)
|
21
|
+
client.valid?.should == false
|
22
|
+
end
|
23
|
+
it "should know it's valid if initialized with valid API credentials" do
|
24
|
+
client = Flavordb::Client.new
|
25
|
+
client.valid?.should == true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be able to request an object by path" do
|
29
|
+
client = Flavordb::Client.new
|
30
|
+
business = client.get_object_data_by_path('/businesses/16')
|
31
|
+
business.should_not == nil
|
32
|
+
business['data']['id'].should == 16
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be able to get a business by id" do
|
36
|
+
client = Flavordb::Client.new
|
37
|
+
business = client.get_business(16)
|
38
|
+
business.should_not == nil
|
39
|
+
business.id.should == 16
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be able to find businesses by name" do
|
43
|
+
client = Flavordb::Client.new
|
44
|
+
businesses = client.find_businesses(:q => 'Lagunitas')
|
45
|
+
businesses.should_not == nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be able to get a product category by id" do
|
49
|
+
client = Flavordb::Client.new
|
50
|
+
product_category = client.get_product_category(16)
|
51
|
+
product_category.should_not == nil
|
52
|
+
product_category.id.should == 16
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should be able to get a product by id" do
|
56
|
+
client = Flavordb::Client.new
|
57
|
+
product = client.get_product(16)
|
58
|
+
product.should_not == nil
|
59
|
+
product.id.should == 16
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Flavordb::ProductCategory do
|
4
|
+
|
5
|
+
before (:all) do
|
6
|
+
@client = Flavordb::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
before (:each) do
|
10
|
+
@product_category = @client.get_product_category(16)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "@product_category know its resource" do
|
14
|
+
@product_category.resource.should_not == nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should know its name" do
|
18
|
+
@product_category.name.should_not == nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should know its id" do
|
22
|
+
@product_category.id.should == 16
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should know whether it's a root category" do
|
26
|
+
@product_category.root?.should == false
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have a parent_category_id property" do
|
30
|
+
@product_category.parent_category_id.should_not == nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should load parent category upon request" do
|
34
|
+
parent_category = @product_category.parent_category
|
35
|
+
parent_category.should_not == nil
|
36
|
+
parent_category.id.should == @product_category.parent_category_id
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should load subcategories upon request" do
|
40
|
+
subcategories = @product_category.subcategories
|
41
|
+
subcategories.should_not == nil
|
42
|
+
subcategories[0].parent_category_id.should == @product_category.id
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should load products upon request" do
|
46
|
+
products = @product_category.products
|
47
|
+
products.should_not == nil
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Flavordb::Product do
|
4
|
+
|
5
|
+
before (:all) do
|
6
|
+
@client = Flavordb::Client.new
|
7
|
+
end
|
8
|
+
|
9
|
+
before (:each) do
|
10
|
+
@product = @client.get_product(1234)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "@product_category know its resource" do
|
14
|
+
@product.resource.should_not == nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should know its name" do
|
18
|
+
@product.name.should_not == nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should know its id" do
|
22
|
+
@product.id.should == 1234
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should retrieve its business on demand" do
|
26
|
+
business = @product.business
|
27
|
+
business.should_not == nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should retrieve its product category on demand" do
|
31
|
+
product_category = @product.product_category
|
32
|
+
product_category.should_not == nil
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'flavordb'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flavordb-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael North
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: typhoeus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
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: A ruby client for the FlavorDB API
|
84
|
+
email:
|
85
|
+
- michael.north@truenorthapps.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .ruby-gemset
|
92
|
+
- .ruby-version
|
93
|
+
- .travis.yml
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- flavordb.gemspec
|
99
|
+
- lib/flavordb.rb
|
100
|
+
- lib/flavordb/base.rb
|
101
|
+
- lib/flavordb/business.rb
|
102
|
+
- lib/flavordb/client.rb
|
103
|
+
- lib/flavordb/product.rb
|
104
|
+
- lib/flavordb/product_category.rb
|
105
|
+
- lib/flavordb/version.rb
|
106
|
+
- spec/business_spec.rb
|
107
|
+
- spec/client_spec.rb
|
108
|
+
- spec/product_category_spec.rb
|
109
|
+
- spec/product_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
homepage: http://developers.flavordb.com/
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project: flavordb-ruby
|
131
|
+
rubygems_version: 2.0.7
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: An awesome object-oriented way of working with beer, wine and whiskey data
|
135
|
+
test_files:
|
136
|
+
- spec/business_spec.rb
|
137
|
+
- spec/client_spec.rb
|
138
|
+
- spec/product_category_spec.rb
|
139
|
+
- spec/product_spec.rb
|
140
|
+
- spec/spec_helper.rb
|