ugalic_yelp 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.
data/README.rdoc ADDED
@@ -0,0 +1,6 @@
1
+ = yelp
2
+
3
+ Describe your project here
4
+
5
+ :include:yelp.rdoc
6
+
data/bin/yelp ADDED
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env ruby
2
+ # 1.9 adds realpath to resolve symlinks; 1.8 doesn't
3
+ # have this method, so we add it so we get resolved symlinks
4
+ # and compatibility
5
+ unless File.respond_to? :realpath
6
+ class File #:nodoc:
7
+ def self.realpath path
8
+ return realpath(File.readlink(path)) if symlink?(path)
9
+ path
10
+ end
11
+ end
12
+ end
13
+ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
14
+ require 'rubygems'
15
+ require 'gli'
16
+ require 'yelp_version'
17
+ require 'yelp/api'
18
+ require 'yelp/web'
19
+ require 'yaml'
20
+
21
+ include GLI
22
+
23
+ program_desc 'Ruby script that supports Yelp API v2.0 '
24
+
25
+ version Yelp::VERSION
26
+ CONFIG_FILE = File.join(ENV['HOME'],'.yelp.rc.yaml')
27
+ API_OPTIONS = {
28
+ consumer_key: '',
29
+ consumer_secret: '',
30
+ token: '',
31
+ token_secret: '',
32
+ api_host: ''
33
+ }
34
+
35
+ desc 'Login into Yelp with username and password'
36
+ arg_name '<Username> <Password>'
37
+ command :login do |c|
38
+
39
+ c.action do |global_options,options,args|
40
+
41
+ username, password = args.shift, args.shift
42
+ if username.nil? || password.nil?
43
+ puts "You need to provide <Username> and <Password> !\n Run: yelp help login"
44
+ exit 1
45
+ end
46
+
47
+ msg = Yelp::Web.login( username: username, password: password )
48
+ p msg
49
+
50
+ end
51
+ end
52
+
53
+ desc 'Seach for popular pub in given city'
54
+ arg_name '<City>'
55
+ command :pub do |c|
56
+ c.action do |global_options,options,args|
57
+
58
+ if args.empty?
59
+ p "For details run: yelp help pub"
60
+ exit 1
61
+ end
62
+
63
+ city = args.shift
64
+ if File.exists? CONFIG_FILE
65
+ options_config = YAML.load_file( CONFIG_FILE )
66
+ options_config.reject!{ |key, value| value == '' }
67
+
68
+ if options_config.empty?
69
+ STDERR.puts "Please add API access details in #{CONFIG_FILE}"
70
+ else
71
+ api = Yelp::Api.new( options_config )
72
+ results = api.popular_pub_in( city )
73
+ STDERR.puts results
74
+ end
75
+ else
76
+ File.open(CONFIG_FILE,'w') { |file| YAML::dump( API_OPTIONS, file ) }
77
+ STDERR.puts "Initialized configuration file in #{CONFIG_FILE}"
78
+ STDERR.puts "Before using Yelp API, please add API access details in #{CONFIG_FILE}"
79
+ end
80
+
81
+ end
82
+ end
83
+
84
+ pre do |global,command,options,args|
85
+ # Pre logic here
86
+ # Return true to proceed; false to abort and not call the
87
+ # chosen command
88
+ # Use skips_pre before a command to skip this block
89
+ # on that command only
90
+ true
91
+ end
92
+
93
+ post do |global,command,options,args|
94
+ # Post logic here
95
+ # Use skips_post before a command to skip this
96
+ # block on that command only
97
+ end
98
+
99
+ on_error do |exception|
100
+ # Error logic here
101
+ # return false to skip default error handling
102
+ true
103
+ end
104
+
105
+ exit GLI.run(ARGV)
data/lib/yelp/api.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'oauth'
2
+ require 'json'
3
+
4
+ module Yelp
5
+ class Api
6
+
7
+ attr_accessor :consumer, :access_token, :body
8
+
9
+ PATH = '/v2/search?term=pubs&sort=2&cc=GB&location=__CITY__'
10
+ MSG = {
11
+ connection_error: 'Could not connect to the internet',
12
+ no_results: 'No matching results' }
13
+
14
+ def initialize( options )
15
+ @body = "", ""
16
+ @consumer = OAuth::Consumer.new( options[:consumer_key], options[:consumer_secret], {:site => "http://#{options[:api_host]}"} )
17
+ @access_token = OAuth::AccessToken.new( @consumer, options[:token], options[:token_secret] )
18
+ end
19
+
20
+ def popular_pub_in( city = 'London')
21
+ path = PATH.gsub( '__CITY__', city )
22
+ @body = @access_token.get( path ).body
23
+ @body.include?('error') ? JSON.parse(@body)['error']['text'] : results
24
+ rescue SocketError => e
25
+ MSG[:connection_error]
26
+ rescue Exception => e
27
+ "[#{e.class}] - #{e.message}"
28
+ end
29
+
30
+ def results
31
+ business = {}
32
+ @body = JSON.parse( @body )
33
+ unless @body['businesses'].empty?
34
+ business = @body['businesses'].first #we are interested just in first result
35
+ business['location']['address'] << business['location']['postal_code']
36
+ end
37
+
38
+ business.empty? ? MSG[:no_results] : prepare_output( business )
39
+ end
40
+
41
+ def prepare_output( business )
42
+ neighborhoods = business['location']['neighborhoods'] unless business['location']['neighborhoods'].nil?
43
+ output =
44
+ """
45
+ The highest rated pub in #{business['location']['city']} is:
46
+
47
+ ================================================================================
48
+ #{business['name']} - #{ !neighborhoods.nil? ? neighborhoods.join(', ') : 'N/A'}
49
+ ================================================================================
50
+
51
+ #{business['location']['address'].join(', ')}
52
+ #{business['phone']}
53
+
54
+ #{business['rating']} stars
55
+
56
+ ================================================================================
57
+ """
58
+ output
59
+ end
60
+
61
+ end
62
+
63
+ end
data/lib/yelp/web.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'mechanize'
2
+
3
+ module Yelp
4
+ class Web
5
+ attr_accessor :consumer, :access_token, :body, :city
6
+
7
+ LOGIN_PAGE = 'http://yelp.co.uk/login'
8
+
9
+ MSG = {
10
+ success: 'Your account details are valid',
11
+ failed: 'Failed to log in to Yelp',
12
+ connection_error: 'Could not connect to the internet' }
13
+
14
+ def self.login( params )
15
+ login_valid = false
16
+ email, password = params[:username], params[:password]
17
+ #using mechanize in order to post csrftoken param
18
+ agent = Mechanize.new { |a| a.user_agent_alias = 'Mac Safari' }
19
+ agent.get( LOGIN_PAGE )
20
+
21
+ #extract login-form
22
+ login_form = agent.page.form_with( id: 'login-form' )
23
+ if login_form
24
+ login_form.email = email
25
+ login_form.password = password
26
+ login_form.submit
27
+ login_valid = agent.page.link_with( id: 'logout-link') != nil
28
+ end
29
+
30
+ login_valid ? MSG[:success] : MSG[:failed]
31
+ rescue Net::HTTP::Persistent::Error => e
32
+ MSG[:connection_error]
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Yelp
2
+ VERSION = '0.0.1'
3
+ end
data/yelp.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = yelp
2
+
3
+ Generate this with
4
+ yelp rdoc
5
+ After you have described your command line interface
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ugalic_yelp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Uros Galic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gli
16
+ requirement: &70254286750040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70254286750040
25
+ - !ruby/object:Gem::Dependency
26
+ name: mechanize
27
+ requirement: &70254286765780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70254286765780
36
+ - !ruby/object:Gem::Dependency
37
+ name: cucumber
38
+ requirement: &70254286765340 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70254286765340
47
+ - !ruby/object:Gem::Dependency
48
+ name: oauth
49
+ requirement: &70254286764780 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70254286764780
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70254286764320 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70254286764320
69
+ - !ruby/object:Gem::Dependency
70
+ name: aruba
71
+ requirement: &70254286763660 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.4.6
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70254286763660
80
+ - !ruby/object:Gem::Dependency
81
+ name: rake
82
+ requirement: &70254286763160 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70254286763160
91
+ - !ruby/object:Gem::Dependency
92
+ name: rdoc
93
+ requirement: &70254286762620 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70254286762620
102
+ description:
103
+ email: uros.galic@gmail.com
104
+ executables:
105
+ - yelp
106
+ extensions: []
107
+ extra_rdoc_files:
108
+ - README.rdoc
109
+ - yelp.rdoc
110
+ files:
111
+ - bin/yelp
112
+ - lib/yelp_version.rb
113
+ - lib/yelp/api.rb
114
+ - lib/yelp/web.rb
115
+ - README.rdoc
116
+ - yelp.rdoc
117
+ homepage: http://your.website.com
118
+ licenses: []
119
+ post_install_message:
120
+ rdoc_options:
121
+ - --title
122
+ - yelp
123
+ - --main
124
+ - README.rdoc
125
+ - -ri
126
+ require_paths:
127
+ - lib
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project: ugalic_yelp
143
+ rubygems_version: 1.8.15
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: Get popular pub in a given city
147
+ test_files: []