user_reviews 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.rbc
2
+ .bundle
3
+ .config
4
+ .yardoc
5
+ InstalledFiles
6
+ _yardoc
7
+ coverage
8
+ doc/
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+ .pairs
17
+ user_reviews-*.gem
18
+ scripts
19
+ scripts/**/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in user_reviews.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'pry'
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ user_reviews (0.0.7)
5
+ activesupport (~> 3.2.11)
6
+ httparty
7
+ json
8
+ oauth
9
+ rake
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (3.2.11)
15
+ i18n (~> 0.6)
16
+ multi_json (~> 1.0)
17
+ coderay (1.0.8)
18
+ diff-lcs (1.1.3)
19
+ httparty (0.10.2)
20
+ multi_json (~> 1.0)
21
+ multi_xml (>= 0.5.2)
22
+ i18n (0.6.1)
23
+ json (1.7.6)
24
+ method_source (0.8.1)
25
+ multi_json (1.5.0)
26
+ multi_xml (0.5.2)
27
+ oauth (0.4.7)
28
+ pry (0.9.11.4)
29
+ coderay (~> 1.0.5)
30
+ method_source (~> 0.8)
31
+ slop (~> 3.4)
32
+ rake (10.0.3)
33
+ rspec (2.12.0)
34
+ rspec-core (~> 2.12.0)
35
+ rspec-expectations (~> 2.12.0)
36
+ rspec-mocks (~> 2.12.0)
37
+ rspec-core (2.12.2)
38
+ rspec-expectations (2.12.1)
39
+ diff-lcs (~> 1.1.3)
40
+ rspec-mocks (2.12.2)
41
+ slop (3.4.3)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ pry
48
+ rspec
49
+ user_reviews!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Unpakt LLC
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,100 @@
1
+ # UserReviews
2
+
3
+ [![Build Status](https://secure.travis-ci.org/Unpakt/user_reviews.png?branch=master)](http://travis-ci.org/unpakt/user_reviews)
4
+
5
+ Is a simple api wrapper for:
6
+
7
+ * Yelp
8
+ * Citysearch
9
+ * Google Ratings
10
+
11
+ ## Getting started
12
+
13
+ ### Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'user_reviews'
17
+ ```
18
+
19
+ ### And then execute:
20
+
21
+ ```console
22
+ $ bundle
23
+ ```
24
+
25
+ ### Or install it yourself as:
26
+
27
+ ```console
28
+ $ gem install user_reviews
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ### Create an api_provider
34
+
35
+ ### For Citysearch
36
+
37
+ ```ruby
38
+ auth_info = {publisher_key: 'your_city_search_api_key'}
39
+ city_search = UserReviews.provider_factory(:citysearch, auth_info)
40
+ ```
41
+
42
+ ### For Google Reviews
43
+
44
+ ```ruby
45
+ auth_info = {key: 'your_google_api_key'}
46
+ google = UserReviews.provider_factory(:google, auth_info)
47
+ ```
48
+
49
+ ### For Yelp
50
+
51
+ ```ruby
52
+ auth_info = {consumer_key: 'yelp_consumer_key', consumer_secret:'yelp_consumer_secret' ,token:'yelp_token' , token_secret:'yelp_token_secret'}
53
+ yelp = UserReviews.provider_factory(:yelp, auth_info)
54
+ ```
55
+
56
+ ### Get reviews for a business
57
+
58
+ ```ruby
59
+ city_search.find_reviews_for_business city_search_business_id
60
+ ```
61
+
62
+ ### Get information about a business
63
+
64
+ ```ruby
65
+ city_search.find_business_by_id(business_id)
66
+ ```
67
+
68
+ Each Review object has 4 attributes:
69
+ * author_name
70
+ * rating
71
+ * reviewed_at
72
+ * comment
73
+
74
+ Each Business object has 4 attributes:
75
+ * name
76
+ * address
77
+ * reference
78
+ * average_rating
79
+ * url
80
+
81
+
82
+ More information about each API:
83
+
84
+ * Yelp: http://www.yelp.com/developers/documentation/v2/overview
85
+ * CitySearch: http://www.citygridmedia.com/developer/
86
+ * GoogleReviews: https://developers.google.com/places/documentation/
87
+
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Add your own provider folder which inherits from the Providers::Base class and tests
94
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 5. Push to the branch (`git push origin my-new-feature`)
96
+ 6. Create new Pull Request
97
+
98
+ ## License
99
+
100
+ MIT License. Copyright 2013. http://www.unpakt.com
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'bundler'
3
+ require 'rspec/core/rake_task'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ require 'httparty'
2
+ require 'oauth'
3
+ require 'json'
4
+ require 'open-uri'
5
+ require 'date'
6
+
7
+ module Providers
8
+ class Base
9
+
10
+ def find_business_by_name(name, business_type, city_state = "")
11
+ end
12
+
13
+ def find_reviews_for_business(ref)
14
+ end
15
+
16
+ def find_business_by_id(id)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,72 @@
1
+ module Providers
2
+ module Citysearch
3
+ class Api < Providers::Base
4
+
5
+ LOCALHOST_IP = "127.0.0.1"
6
+
7
+ def initialize(options)
8
+ @key = options[:publisher_key]
9
+ @ip = options[:client_ip] || LOCALHOST_IP
10
+ end
11
+
12
+ def find_business_by_name(name, business_type, city_state)
13
+ request_url = business_search_url(name, business_type, city_state)
14
+ json_results = HTTParty.get(request_url)
15
+ parse_business_results(json_results)
16
+ end
17
+
18
+ def find_business_by_id(citysearch_id)
19
+ request_url = business_url(citysearch_id)
20
+ json_results = HTTParty.get(request_url)
21
+ json = json_results["locations"] || []
22
+
23
+ return json if json.empty?
24
+ Providers::Citysearch::Business.build_business(json.first)
25
+ end
26
+
27
+ def find_reviews_for_business(citysearch_id)
28
+ request_url = reviews_url(citysearch_id)
29
+ json_results = HTTParty.get(request_url)
30
+ parse_reviews_results(json_results)
31
+ end
32
+
33
+ private
34
+
35
+ def business_url(citysearch_id)
36
+ "http://api.citygridmedia.com/content/places/v2/detail?publisher=#{@key}&id=#{citysearch_id}&id_type=cs&client_ip=127.0.0.1&format=json"
37
+ end
38
+
39
+ def business_search_url(name, business_type = "", city_state)
40
+ "http://api.citygridmedia.com/content/places/v2/search/where?where=#{URI::encode(city_state)}&what=#{URI::encode(name)}&publisher=#{@key}&format=json"
41
+ end
42
+
43
+ def reviews_url(citysearch_id)
44
+ "http://api.citygridmedia.com/content/reviews/v2/search/where?listing_id=#{citysearch_id}&publisher=#{@key}&format=json"
45
+ end
46
+
47
+ def parse_business_results(json)
48
+ result = []
49
+ return result if json.nil? || json["results"].nil?
50
+
51
+ json = json["results"] || []
52
+ json["locations"].each do |e|
53
+ result.push(Providers::Citysearch::Business.build_business(e))
54
+ end
55
+
56
+ result
57
+ end
58
+
59
+ def parse_reviews_results(json)
60
+ result = []
61
+ return result if json.nil? || json["results"].nil?
62
+
63
+ json = json["results"] || []
64
+ json["reviews"].each do |e|
65
+ result.push(Providers::Citysearch::Review.new(e))
66
+ end
67
+
68
+ result
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,23 @@
1
+ module Providers
2
+ module Citysearch
3
+ class Business
4
+ attr_accessor :name, :address, :reference, :average_rating, :total_reviews
5
+
6
+ def self.build_business(node)
7
+ return nil if [node["name"], node["address"], node["review_info"], node["id"]].include?(nil)
8
+ new(node)
9
+ end
10
+
11
+ private
12
+
13
+ def initialize(node)
14
+ @name = node["name"]
15
+ full_address = node["address"]
16
+ @address = [full_address["street"], full_address["city"], full_address["state"], full_address["postal_code"]].join(" ")
17
+ @reference = node["id"]
18
+ @total_reviews = node['review_info']['total_user_reviews']
19
+ @average_rating = node['review_info']['overall_review_rating']
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+
2
+ module Providers
3
+ module Citysearch
4
+ class Review
5
+ attr_accessor :rating, :author_name, :reviewed_at, :comment
6
+
7
+ def initialize(node)
8
+ @rating = node["review_rating"]
9
+ @author_name = node["review_author"]
10
+ @reviewed_at = DateTime.parse(node["review_date"]).to_time
11
+ @comment = node["review_text"]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,73 @@
1
+ module Providers
2
+ module Google
3
+ class ExceededQueryLimitError < StandardError; end
4
+
5
+ class Api < Providers::Base
6
+
7
+ def initialize(options)
8
+ @key = options[:key]
9
+ end
10
+
11
+ def find_business_by_name(name, business_type, city_state = "")
12
+ request_url = business_url(name, business_type)
13
+ @json_results = HTTParty.get(request_url)
14
+ parse_business_results(@json_results)
15
+ end
16
+
17
+ def find_reviews_for_business(ref)
18
+ request_url = reviews_url(ref)
19
+ @json_results = HTTParty.get(request_url)
20
+ parse_reviews_results(@json_results)
21
+ end
22
+
23
+ def find_business_by_id(google_id)
24
+ request_url = reviews_url(google_id)
25
+ @json_results = HTTParty.get(request_url)
26
+ return @json_results if @json_results.nil?
27
+
28
+ json = @json_results["result"]
29
+ return json if json.nil?
30
+
31
+ Providers::Google::Business.build_business(json)
32
+ end
33
+
34
+ private
35
+
36
+ def query_limit_has_been_reached?
37
+ @json_results["status"] == 'OVER_QUERY_LIMIT'
38
+ end
39
+
40
+ def business_url(business_name, business_type)
41
+ "https://maps.googleapis.com/maps/api/place/textsearch/json?key=#{@key}&query=#{URI::encode(business_name)}=&sensor=false&type=#{URI::encode(business_type)}"
42
+ end
43
+
44
+ def reviews_url(reference)
45
+ "https://maps.googleapis.com/maps/api/place/details/json?key=#{@key}&reference=#{reference}&sensor=false"
46
+ end
47
+
48
+ def parse_business_results(json)
49
+ raise Providers::Google::ExceededQueryLimitError if query_limit_has_been_reached?
50
+
51
+ result = []
52
+ return result if json.nil? || json["results"].nil?
53
+
54
+ json["results"].each do |e|
55
+ result.push(Providers::Google::Business.build_business(e))
56
+ end
57
+
58
+ result
59
+ end
60
+
61
+ def parse_reviews_results(json)
62
+ result = []
63
+ return result if json.nil? || json["result"].nil? || json["result"]["reviews"].nil?
64
+
65
+ json["result"]["reviews"].each do |e|
66
+ result.push(Providers::GoogleReviews::Review.new(e))
67
+ end
68
+
69
+ result
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,21 @@
1
+ module Providers
2
+ module Google
3
+ class Business
4
+ attr_accessor :name, :address, :reference, :average_rating, :url
5
+
6
+ def self.build_business(node)
7
+ return nil if [node["name"], node["formatted_address"], node["reference"]].include?(nil)
8
+ new(node)
9
+ end
10
+
11
+ private
12
+ def initialize(node)
13
+ @name = node["name"]
14
+ @address = node["formatted_address"]
15
+ @reference = node["reference"]
16
+ @average_rating = node["rating"]
17
+ @url = node["url"]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Providers
2
+ module Google
3
+ class Review
4
+ attr_accessor :rating, :author_name, :reviewed_at, :comment
5
+
6
+ def initialize(node)
7
+ @rating = average_rating(node["aspects"])
8
+ @author_name = node["author_name"]
9
+ @reviewed_at = Time.at(node["time"].to_i)
10
+ @comment = node["text"]
11
+ end
12
+
13
+ private
14
+
15
+ def average_rating(arr)
16
+ return 0 if arr.nil? || arr.size < 1
17
+
18
+ sum = 0
19
+ arr.each { |el| sum += el["rating"].to_f }
20
+ sum.to_f / arr.size
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ module Providers
2
+ module Yelp
3
+ class Api < Providers::Base
4
+
5
+ def initialize(options)
6
+ @consumer_key = options[:consumer_key]
7
+ @consumer_secret = options[:consumer_secret]
8
+ @token = options[:token]
9
+ @token_secret = options[:token_secret]
10
+ end
11
+
12
+ def find_business_by_name(name, business_type, city_state)
13
+ access_token = create_oauth_token
14
+ response = JSON.parse(access_token.get(business_path(name, business_type, city_state)).body)
15
+ parse_businesses(response["businesses"])
16
+ end
17
+
18
+ def find_reviews_for_business(yelp_id)
19
+ access_token = create_oauth_token
20
+ response = JSON.parse(access_token.get(review_path(yelp_id)).body)
21
+ parse_reviews(response["reviews"])
22
+ end
23
+
24
+ def find_business_by_id(yelp_id)
25
+ access_token = create_oauth_token
26
+ response = JSON.parse(access_token.get(review_path(yelp_id)).body)
27
+ Providers::YelpReviews::Business.build_business(response) if response &&
28
+ response["location"] &&
29
+ response["location"]["display_address"]
30
+
31
+ end
32
+
33
+ private
34
+
35
+ def create_oauth_token
36
+ consumer_key = @consumer_key
37
+ consumer_secret = @consumer_secret
38
+ token = @token
39
+ token_secret = @token_secret
40
+
41
+ consumer = OAuth::Consumer.new(consumer_key, consumer_secret, site: "http://api.yelp.com")
42
+ OAuth::AccessToken.new(consumer, token, token_secret)
43
+ end
44
+
45
+ def business_path(name, business_type, city_state)
46
+ "/v2/search?term=#{URI::encode(name)}&category_filter=#{URI::encode(business_type)}&location=#{URI::encode(city_state)}"
47
+ end
48
+
49
+ def review_path(yelp_id)
50
+ "/v2/business/#{yelp_id}"
51
+ end
52
+
53
+ def parse_businesses(business_list)
54
+ result = []
55
+ return result if business_list.nil? ||
56
+ business_list.empty? ||
57
+ business_list["location"].nil? ||
58
+ business_list["location"]["display_address"].nil?
59
+
60
+ business_list.each do |business|
61
+ result.push(Providers::YelpReviews::Business.build_business(business))
62
+ end
63
+
64
+ result
65
+ end
66
+
67
+ def parse_reviews(review_list)
68
+ result = []
69
+ return result if review_list.nil?
70
+
71
+ review_list.each do |review|
72
+ result.push(Providers::YelpReviews::Review.new(review))
73
+ end
74
+
75
+ result
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,29 @@
1
+ module Providers
2
+ module Yelp
3
+ class Business
4
+ attr_accessor :name, :address, :yelp_id, :average_rating, :total_reviews
5
+
6
+ def self.build_business(node)
7
+ return nil if [node["location"], node["rating"], node["id"]].include?(nil)
8
+ new(node)
9
+ end
10
+
11
+ private
12
+
13
+ def initialize(node)
14
+ return if [node,
15
+ node["location"],
16
+ node["location"]["display_address"],
17
+ node["rating"],
18
+ node["review_count"],
19
+ node["id"]].include?(nil)
20
+
21
+ @address = node["location"]["display_address"].join(" ")
22
+ @name = node["name"]
23
+ @average_rating = node["rating"]
24
+ @total_reviews = node["review_count"]
25
+ @yelp_id = node["id"]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module Providers
2
+ module Yelp
3
+ class Review
4
+ attr_accessor :rating, :author_name, :reviewed_at, :comment
5
+
6
+ def initialize(node)
7
+ @rating = node["rating"]
8
+ @author_name = node["name"]
9
+ @reviewed_at = Time.at(node["time_created"].to_i)
10
+ @comment = node["excerpt"]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module UserReviews
2
+ VERSION = "0.0.7".freeze
3
+ end
@@ -0,0 +1,26 @@
1
+ require "user_reviews/version"
2
+ require 'active_support/inflector'
3
+
4
+ Dir[File.dirname(__FILE__) + "/providers/*.rb"].each do |file|
5
+ require "#{file}"
6
+ end
7
+
8
+ Dir[File.dirname(__FILE__) + "/providers/**/*.rb"].each do |file|
9
+ require "#{file}"
10
+ end
11
+
12
+ module UserReviews
13
+ class ProviderNotFoundError < StandardError; end
14
+
15
+ include ActiveSupport::Inflector
16
+
17
+ def self.provider_factory(service, init_options)
18
+ service = service.to_s.classify
19
+
20
+ begin
21
+ "Providers::#{service}::Api".constantize.new(init_options)
22
+ rescue => e
23
+ raise ProviderNotFoundError.new(e)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Providers::Citysearch::Api do
4
+
5
+ describe "#initialize" do
6
+ it "initializes a new CitySearch provider" do
7
+ Providers::Citysearch::Api.new(publisher_key: "").should be
8
+ end
9
+ end
10
+
11
+ describe "#find_business_by_name" do
12
+ let(:json_result) { { "address" => {} } }
13
+ let(:business_url) { "http://www.example.com" }
14
+ let(:citysearch_wrapper) { Providers::Citysearch::Api.new(key: "") }
15
+ let(:query_result) { [Providers::Citysearch::Business.build_business(json_result)] }
16
+
17
+ it "returns an array of possible businesses" do
18
+ citysearch_wrapper.should_receive(:business_search_url).with("Unpakt", "moving company", "NY,NY") { business_url }
19
+ HTTParty.should_receive(:get).with(business_url) { json_result }
20
+ citysearch_wrapper.should_receive(:parse_business_results).with(json_result) { query_result }
21
+
22
+ citysearch_wrapper.find_business_by_name("Unpakt", "moving company", "NY,NY").should eq query_result
23
+ end
24
+ end
25
+
26
+
27
+ describe "#find_reviews_for_business" do
28
+ let(:citysearch_id) { stub }
29
+ let(:json_result) { {"review_date" => "2007-03-07T21:53:41.000Z"} }
30
+ let(:reviews_url) { "http://www.example.com" }
31
+ let(:citysearch_wrapper) { Providers::Citysearch::Api.new(key: "") }
32
+ let(:query_result) { [Providers::Citysearch::Review.new(json_result)] }
33
+
34
+ it "returns the rating for a given entity" do
35
+ citysearch_wrapper.should_receive(:reviews_url).with(citysearch_id) { reviews_url }
36
+ HTTParty.should_receive(:get).with(reviews_url) { json_result }
37
+ citysearch_wrapper.should_receive(:parse_reviews_results).with(json_result) { query_result }
38
+
39
+ citysearch_wrapper.find_reviews_for_business(citysearch_id).should eq query_result
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Providers::Citysearch::Business do
4
+ describe "#initialize" do
5
+ let(:address) { {"street" => '', "city" => '', "state" => '', "postal_code" => '' } }
6
+ let(:node) do
7
+ {
8
+ "name" => '',
9
+ "address" => address,
10
+ "id" => stub,
11
+ "rating" => stub,
12
+ "review_info" => {}
13
+ }
14
+ end
15
+
16
+ context "when required fields are not present" do
17
+ let(:node) { Hash.new(nil) }
18
+ it "returns nil" do
19
+ biz = Providers::Citysearch::Business.build_business(node)
20
+ biz.should be_nil
21
+ end
22
+ end
23
+
24
+ context "when required fields are present" do
25
+ it "returns an Bussiness object" do
26
+ Providers::Citysearch::Business.build_business(node).should be
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Providers::Google::Api do
4
+
5
+ describe "#initialize" do
6
+ it "initializes a new GoogleReviews provider" do
7
+ Providers::Google::Api.new(key: "").should be
8
+ end
9
+ end
10
+
11
+ describe "#find_business_by_name" do
12
+ let(:json_result) { "{}" }
13
+ let(:business_url) { "http://www.example.com" }
14
+ let(:google_wrapper) { Providers::Google::Api.new(key: "") }
15
+ let(:query_result) { [Providers::Google::Business.build_business(json_result)] }
16
+
17
+ context "when the api is valid" do
18
+ it "returns an array of possible businesses" do
19
+ google_wrapper.should_receive(:business_url).with("Unpakt", "moving_company") { business_url }
20
+ HTTParty.should_receive(:get).with(business_url) { json_result }
21
+ google_wrapper.should_receive(:parse_business_results).with(json_result) { query_result }
22
+
23
+ google_wrapper.find_business_by_name("Unpakt", "moving_company").should eq query_result
24
+ end
25
+ end
26
+
27
+ context "when the api query limit is reached" do
28
+ let(:json_result) { {'status' => 'OVER_QUERY_LIMIT'} }
29
+
30
+ it "raises an error" do
31
+ google_wrapper.should_receive(:business_url).with("Unpakt", "moving_company") { business_url }
32
+ HTTParty.should_receive(:get).with(business_url) { json_result }
33
+ lambda { google_wrapper.find_business_by_name("Unpakt", "moving_company") }.should raise_error Providers::Google::ExceededQueryLimitError
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#find_reviews_for_business" do
39
+ let(:reference) { stub }
40
+ let(:json_result) { "{}" }
41
+ let(:reviews_url) { "http://www.example.com" }
42
+ let(:google_wrapper) { Providers::Google::Api.new(key: "") }
43
+ let(:query_result) { [Providers::Google::Review.new(json_result)] }
44
+
45
+ it "returns the rating for a given entity" do
46
+ google_wrapper.should_receive(:reviews_url).with(reference) { reviews_url }
47
+ HTTParty.should_receive(:get).with(reviews_url) { json_result }
48
+ google_wrapper.should_receive(:parse_reviews_results).with(json_result) { query_result }
49
+
50
+ google_wrapper.find_reviews_for_business(reference).should eq query_result
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Providers::Google::Business do
4
+ describe "#initialize" do
5
+ let(:node) do
6
+ {
7
+ "name" => '',
8
+ "formatted_address" => '',
9
+ "reference" => stub,
10
+ "url" => stub,
11
+ "rating" => stub
12
+ }
13
+ end
14
+
15
+ context "when required fields are not present" do
16
+ let(:node) { Hash.new(nil) }
17
+ it "returns nil" do
18
+ biz = Providers::Google::Business.build_business(node)
19
+ biz.should be_nil
20
+ end
21
+ end
22
+
23
+ context "when required fields are present" do
24
+ it "returns an Bussiness object" do
25
+ Providers::Google::Business.build_business(node).should be
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Providers::Yelp::Api do
4
+
5
+ describe "#initialize" do
6
+ it "initializes a new GoogleReviews provider" do
7
+ Providers::Yelp::Api.new(consumer_key: "",
8
+ consumer_secret: "",
9
+ token: "",
10
+ token_secret: "").should be
11
+ end
12
+ end
13
+
14
+ describe "#find_business_by_name" do
15
+ let(:token) { stub }
16
+ let(:api_result) { stub }
17
+ let(:response_body) { stub }
18
+ let(:business_path) { "super_path" }
19
+ let(:business_place) do
20
+ {"location" => {"display_address" => ["1 Place", "NY"]}}
21
+ end
22
+ let(:json_result) {{"businesses" => business_places }}
23
+ let(:business_places) { [business_place] }
24
+ let(:yelp_wrapper) { Providers::Yelp::Api.new({}) }
25
+ let(:query_result) { [Providers::Yelp::Business.build_business(business_place)] }
26
+
27
+ it "returns an array of possible businesses" do
28
+ yelp_wrapper.should_receive(:create_oauth_token) { token }
29
+ yelp_wrapper.should_receive(:business_path).with("Unpakt", "movers", "NY,NY") { business_path }
30
+ token.should_receive(:get).with(business_path) { api_result }
31
+ api_result.should_receive(:body) { response_body }
32
+
33
+
34
+ JSON.should_receive(:parse).with(response_body) { json_result }
35
+ yelp_wrapper.should_receive(:parse_businesses).with(business_places) { query_result }
36
+
37
+ yelp_wrapper.find_business_by_name("Unpakt", "movers", "NY,NY").should eq query_result
38
+ end
39
+ end
40
+
41
+
42
+ describe "#find_reviews_for_business" do
43
+ let(:token) { stub }
44
+ let(:api_result) { stub }
45
+ let(:response_body) { stub }
46
+ let(:review_path) { "super_path" }
47
+ let(:review) {{"rating" => 1}}
48
+ let(:reviews) { [review] }
49
+ let(:json_result) {{"reviews" => reviews }}
50
+ let(:yelp_wrapper) { Providers::Yelp::Api.new({}) }
51
+ let(:query_result) { [Providers::Yelp::Review.new(review)] }
52
+
53
+ it "returns an array of possible businesses" do
54
+ yelp_wrapper.should_receive(:create_oauth_token) { token }
55
+ yelp_wrapper.should_receive(:review_path).with("yelp-Unpakt") { review_path }
56
+ token.should_receive(:get).with(review_path) { api_result }
57
+ api_result.should_receive(:body) { response_body }
58
+
59
+
60
+ JSON.should_receive(:parse).with(response_body) { json_result }
61
+ yelp_wrapper.should_receive(:parse_reviews).with(reviews) { query_result }
62
+
63
+ yelp_wrapper.find_reviews_for_business("yelp-Unpakt").should eq query_result
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Providers::Yelp::Business do
4
+ describe "#initialize" do
5
+ let(:node) do
6
+ {
7
+ "location" => '',
8
+ "rating" => '',
9
+ "id" => stub,
10
+ }
11
+ end
12
+
13
+ context "when required fields are not present" do
14
+ let(:node) { Hash.new(nil) }
15
+ it "returns nil" do
16
+ biz = Providers::Yelp::Business.build_business(node)
17
+ biz.should be_nil
18
+ end
19
+ end
20
+
21
+ context "when required fields are present" do
22
+ it "returns an Bussiness object" do
23
+ Providers::Yelp::Business.build_business(node).should be
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require_relative File.dirname(__FILE__) + "/../lib/user_reviews"
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
21
+
22
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe UserReviews do
4
+ describe "::provider_factory" do
5
+ context "when the provider exists" do
6
+ it "returns a new Yelp api provider object" do
7
+ UserReviews.provider_factory(:yelp, {}).should be_an_instance_of Providers::Yelp::Api
8
+ end
9
+
10
+ it "returns a new google api provider object" do
11
+ UserReviews.provider_factory(:google, {}).should be_an_instance_of Providers::Google::Api
12
+ end
13
+
14
+ it "returns a new citysearch api provider object" do
15
+ UserReviews.provider_factory(:citysearch, {}).should be_an_instance_of Providers::Citysearch::Api
16
+ end
17
+ end
18
+
19
+ context "when the :not_found provider doesn't exist" do
20
+ it "raises a provider not found error" do
21
+ lambda {UserReviews.provider_factory(:foobar, {}) }.should raise_error UserReviews::ProviderNotFoundError
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'user_reviews/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "user_reviews"
8
+ gem.version = UserReviews::VERSION.dup
9
+ gem.authors = ["Unpakt LLC"]
10
+ gem.email = ["info@unpakt.com"]
11
+ gem.description = %q{Wrapper for mulitple user review APIs}
12
+ gem.summary = %q{Wrapper for mulitple user review APIs}
13
+ gem.homepage = "http://www.unpakt.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "activesupport", "~> 3.2.11"
21
+ gem.add_dependency "rake"
22
+ gem.add_dependency "httparty"
23
+ gem.add_dependency "oauth"
24
+ gem.add_dependency "json"
25
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: user_reviews
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Unpakt LLC
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.11
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httparty
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: oauth
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Wrapper for mulitple user review APIs
95
+ email:
96
+ - info@unpakt.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - lib/providers/base.rb
109
+ - lib/providers/citysearch/api.rb
110
+ - lib/providers/citysearch/business.rb
111
+ - lib/providers/citysearch/review.rb
112
+ - lib/providers/google/api.rb
113
+ - lib/providers/google/business.rb
114
+ - lib/providers/google/review.rb
115
+ - lib/providers/yelp/api.rb
116
+ - lib/providers/yelp/business.rb
117
+ - lib/providers/yelp/review.rb
118
+ - lib/user_reviews.rb
119
+ - lib/user_reviews/version.rb
120
+ - spec/providers/citysearch/api_spec.rb
121
+ - spec/providers/citysearch/business_spec.rb
122
+ - spec/providers/google/api_spec.rb
123
+ - spec/providers/google/business_spec.rb
124
+ - spec/providers/yelp/api_spec.rb
125
+ - spec/providers/yelp/business_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/user_reviews_spec.rb
128
+ - user_reviews.gemspec
129
+ homepage: http://www.unpakt.com
130
+ licenses: []
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 1.8.24
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Wrapper for mulitple user review APIs
153
+ test_files:
154
+ - spec/providers/citysearch/api_spec.rb
155
+ - spec/providers/citysearch/business_spec.rb
156
+ - spec/providers/google/api_spec.rb
157
+ - spec/providers/google/business_spec.rb
158
+ - spec/providers/yelp/api_spec.rb
159
+ - spec/providers/yelp/business_spec.rb
160
+ - spec/spec_helper.rb
161
+ - spec/user_reviews_spec.rb
162
+ has_rdoc: