yelp-business 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f20c2c15c6eece9921105205aaf12e1aad62c131
4
+ data.tar.gz: 9044e250e8e97e14d52b5a02102f85fd00d7008f
5
+ SHA512:
6
+ metadata.gz: bd178f739422a474c08a8aee6dd4f909d84943adc9318738fbeb24bc5b88c8a64ecdb41cd9e50c85b3828d1b3184a717cd3a6b48be9879bf88c67c5b1d88d01f
7
+ data.tar.gz: 338aa4bff75d1844c6e7390f3202c7914d005f3e0aa7dc0127ad4732f45eb0cce9f469be1350d72d7eda19efb85aa8bf5a627e19ebd9915cb798c669f75a46e5
@@ -0,0 +1,16 @@
1
+ .idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /doc/
7
+ /pkg/
8
+ /coverage/
9
+ /spec/coverage/
10
+ /tmp/
11
+ /data
12
+ /spec/coverage/
13
+ /*.enc
14
+ **/.DS_Store
15
+ .ruby-version
16
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
@@ -0,0 +1,18 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=d17e91219ca5c271ca9bdd9eb447611f1be6b750abe8e4bc48146f5dec1154d4
4
+ sudo: false
5
+ language: ruby
6
+ cache: bundler
7
+ rvm:
8
+ - 2.4.1
9
+ - 2.3.5
10
+ before_install: gem install bundler -v 1.15.4
11
+ before_script:
12
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
+ - chmod +x ./cc-test-reporter
14
+ - ./cc-test-reporter before-build
15
+ script:
16
+ - bundle exec rspec
17
+ after_script:
18
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Konstantin Gredeskoul
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.
@@ -0,0 +1,90 @@
1
+ [![Build Status](https://travis-ci.org/pioneerworks/yelp-business.svg?branch=master)](https://travis-ci.org/pioneerworks/yelp-business)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0ddd76e19c265107cd3e/maintainability)](https://codeclimate.com/github/pioneerworks/yelp-business/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/0ddd76e19c265107cd3e/test_coverage)](https://codeclimate.com/github/pioneerworks/yelp-business/test_coverage)
4
+
5
+ # Yelp::Business
6
+
7
+ This is the simple data model encapsulating Yelp Business data received over [Fusion V3 API](https://www.yelp.com/developers/documentation/v3/business).
8
+
9
+ ## Usage
10
+
11
+ ### Configure
12
+
13
+ First we need to configure the [access token](https://www.yelp.com/developers/documentation/v3/authentication):
14
+
15
+ You can set the token either prorammatiacally (in ruby), or usin an external environment variable `YELP_ACCESS_TOKEN`.
16
+
17
+ ```bash
18
+ export YELP_ACCESS_TOKEN='5eUZNOPOlWw9oQHowSKaYcWQK......'
19
+ ```
20
+
21
+ Or, in ruby:
22
+
23
+ ```ruby
24
+ # Set the token either directly on +Yelp+ module:
25
+ Yelp.access_token = '5eUZNOPOlWw9oQHowSKaYcWQK....'
26
+
27
+ # or using #configure method on Yelp::Business
28
+ Yelp::Business.configure do |config|
29
+ config.access_token = token
30
+ end
31
+ ```
32
+
33
+ ### Fetch Business Info
34
+
35
+ Next, we'll fetch and cache business info. In your code, when you have business id or URL:
36
+
37
+ ```ruby
38
+ require 'yelp/business'
39
+
40
+ url = 'https://www.yelp.com/biz/gary-danko-san-francisco'
41
+ name = Yelp::Business.business_id_from(url) # => 'gary-danko-san-francisco'
42
+
43
+ # initialize the object, and also fetch the data from Yelp
44
+ business = Yelp::Business.new(name).get
45
+
46
+ # You can now call methods on the instance to access it's attributes:
47
+ business.name # => 'Gary Danko'
48
+ business.rating #=> 4.5
49
+ ```
50
+
51
+ You can also cache the results of the call:
52
+
53
+ ```ruby
54
+ cache_key = "yelp.business.#{business_id}"
55
+ business = Rails.cache.fetch(cache_key) do
56
+ Yelp::Business.new(business_id).get
57
+ end
58
+ business.name #=> 'Gary Danko'
59
+ ```
60
+
61
+ ## Installation
62
+
63
+ Add this line to your application's Gemfile:
64
+
65
+ ```ruby
66
+ gem 'yelp-business'
67
+ ```
68
+
69
+ And then execute:
70
+
71
+ $ bundle
72
+
73
+ Or install it yourself as:
74
+
75
+ $ gem install yelp-business
76
+
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ 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).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/pioneerworks/yelp-business](https://github.com/pioneerworks/yelp-business).
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yelp/business"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib_path = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH << lib_path if File.exist?(lib_path) && !$LOAD_PATH.include?(lib_path)
5
+
6
+ require_relative '../lib/yelp/business'
7
+ require 'colored2'
8
+
9
+ if ARGV.empty?
10
+ puts 'usage: yelp-business <business_id>'
11
+ puts ' eg: yelp-business gary-danko-san-francisco'
12
+ else
13
+ require 'awesome_print'
14
+ begin
15
+ ap Yelp::Business.new(ARGV.shift).get.data
16
+ rescue Yelp::MissingAccessTokenError
17
+ puts 'Error!'.bold.red + ' Set ' + 'YELP_ACCESS_TOKEN'.bold.blue + ' environment variable!'
18
+ end
19
+
20
+ end
@@ -0,0 +1,24 @@
1
+ require 'yelp/business/version'
2
+
3
+ module Yelp
4
+ class BaseError < StandardError; end
5
+ class BusinessNotFoundError < BaseError; end
6
+ class InvalidIdentifierError < BaseError; end
7
+ class MissingAccessTokenError < BaseError; end
8
+ class YelpError < BaseError; end
9
+ class AuthenticationError < YelpError; end
10
+
11
+ API_HOST = 'https://api.yelp.com'
12
+ BUSINESS_PATH = '/v3/businesses/'
13
+ REVIEWS_SUFFIX = '/reviews'
14
+ PUBLIC_URL_PREFIX = 'https://www.yelp.com/biz/'
15
+
16
+ class << self
17
+ attr_accessor :access_token
18
+ end
19
+
20
+ self.access_token ||= ENV['YELP_ACCESS_TOKEN']
21
+ end
22
+
23
+
24
+ require 'yelp/business'
@@ -0,0 +1,100 @@
1
+ require 'ostruct'
2
+ require 'json'
3
+ require 'http'
4
+ require 'colored2'
5
+
6
+ require_relative '../yelp'
7
+
8
+ module Yelp
9
+ class Business
10
+ class << self
11
+ def configure
12
+ Yelp.instance_eval do
13
+ yield(self)
14
+ end
15
+ end
16
+
17
+ def business_id_from(url)
18
+ url.gsub %r[.*/], ''
19
+ end
20
+ end
21
+
22
+ attr_accessor :data,
23
+ :business_url,
24
+ :business_id,
25
+ :api_url,
26
+ :reviews_api_url
27
+
28
+ def initialize(business_id = nil)
29
+ raise ::Yelp::InvalidIdentifierError, 'business ID can not be nil' if business_id.nil?
30
+ self.business_id = business_id
31
+ self.business_url = PUBLIC_URL_PREFIX + business_id
32
+ self.api_url = API_HOST + BUSINESS_PATH + self.business_id if self.business_id
33
+ self.reviews_api_url = API_HOST + BUSINESS_PATH + self.business_id + REVIEWS_SUFFIX
34
+ end
35
+
36
+
37
+ # usage: @business.fetch do |data|
38
+ # data.name #=> 'Gary Danko'
39
+ # end
40
+ def fetch
41
+ unless self.data
42
+ raise ::Yelp::MissingAccessTokenError, 'please specify authentication token as class variable' unless access_token
43
+ self.data = OpenStruct.new(exec_api_call)
44
+ raise ::Yelp::BusinessNotFoundError "Can't find business #{business_id}" unless data
45
+ end
46
+ yield(self.data) if block_given?
47
+ self
48
+ end
49
+
50
+ alias get fetch
51
+
52
+ def get_reviews
53
+ self.data = OpenStruct.new(reviews_api_fetch)
54
+ end
55
+
56
+ def exec_api_call
57
+ raise ::Yelp::MissingAccessTokenError unless access_token
58
+ response = api_fetch
59
+ if response['error']
60
+ raise ::Yelp::AuthenticationError, 'Invalid access token' if response['error']['code'] == 'TOKEN_INVALID'
61
+ raise ::Yelp::BusinessNotFoundError, "Business #{business_id} was not found" if response['error']['description'] =~ /could not be found/
62
+ raise ::Yelp::YelpError, "Yelp returned error: #{response['error']['description']}"
63
+ end
64
+ response
65
+ rescue Exception => e
66
+ error "ERROR: while fetching data for business #{business_id.bold.green} from #{api_url.yellow}"
67
+ error "DETAILS: #{e.message.bold.red}"
68
+ error(*e.backtrace) if ENV['DEBUG']
69
+ raise e
70
+ end
71
+
72
+ def error(*args)
73
+ STDERR.puts args.join("\n")
74
+ end
75
+
76
+ def api_fetch
77
+ HTTP.
78
+ auth(bearer_token).
79
+ get(api_url).parse
80
+ end
81
+
82
+ def reviews_api_fetch
83
+ HTTP.
84
+ auth(bearer_token).
85
+ get(reviews_api_url).parse
86
+ end
87
+
88
+ def bearer_token
89
+ "Bearer #{access_token}"
90
+ end
91
+
92
+ def access_token
93
+ Yelp.access_token
94
+ end
95
+
96
+ def method_missing(method, *args, &block)
97
+ data.send(method, *args, &block)
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,5 @@
1
+ module Yelp
2
+ class Business
3
+ VERSION = "0.2.1"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'yelp/business/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'yelp-business'
9
+ spec.version = Yelp::Business::VERSION
10
+ spec.authors = ['Konstantin Gredeskoul', 'Tim Cannady', 'Pioneerworks, Inc']
11
+ spec.email = %w(kigster@gmail.com tim@joinhomebase.com)
12
+
13
+ spec.summary = 'Provides and easy to use model wrapping Yelp Business'
14
+ spec.description = 'Provides and easy to use model wrapping Yelp Business'
15
+
16
+ spec.homepage = 'https://github.com/pioneerworks/yelp-business'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'http'
27
+ spec.add_dependency 'colored2'
28
+ spec.add_dependency 'awesome_print'
29
+
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'simplecov'
32
+ spec.add_development_dependency 'irbtools'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency 'rspec-its'
35
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yelp-business
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Gredeskoul
8
+ - Tim Cannady
9
+ - Pioneerworks, Inc
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2017-11-03 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: http
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: colored2
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: awesome_print
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: simplecov
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: irbtools
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: rspec
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: rspec-its
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ description: Provides and easy to use model wrapping Yelp Business
128
+ email:
129
+ - kigster@gmail.com
130
+ - tim@joinhomebase.com
131
+ executables: []
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - ".gitignore"
136
+ - ".rspec"
137
+ - ".travis.yml"
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - bin/yelp-business
145
+ - lib/yelp.rb
146
+ - lib/yelp/business.rb
147
+ - lib/yelp/business/version.rb
148
+ - yelp-business.gemspec
149
+ homepage: https://github.com/pioneerworks/yelp-business
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.6.11
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Provides and easy to use model wrapping Yelp Business
173
+ test_files: []