whatser 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -28,13 +28,14 @@ Declare Whatser in your Gemfile:
28
28
 
29
29
  And instantiate a Whatser client as need:
30
30
 
31
- client = Whatser::Client.new(:api_key => 'key', :api_secret => 'secret', :oauth_token => '123abc')
31
+ client = Whatser::Client.new(:api_key => 'key', :api_secret => 'secret', :oauth_token => '123abc', :logger => Rails.logger)
32
32
 
33
33
  or...
34
34
 
35
35
  Whatser::Client.configure do |config|
36
36
  config.api_key = 'key'
37
37
  config.api_secret = 'secret'
38
+ config.logger = Rails.logger
38
39
  end
39
40
  client1 = Whatser::Client.new( :oauth_token => '123abc' )
40
41
  client2 = Whatser::Client.new( :oauth_token => '789xyz' )
@@ -150,6 +151,10 @@ As a convenience you can also iterate over a collection's spot data:
150
151
  collection = Whatser::Collection.new( [spot1, spot2, spot3] )
151
152
  collection.each { |c| puts c.name }
152
153
 
154
+ If a user somehow discovers a spot due to another user's recommendation or similar, you can explicitly thank them for the recommendation, slightly increasing the user's Whatser reputation and influence as an opinion leader.
155
+
156
+ user.thanks( spot_id, :message => 'thanks, this spot looks great', :facebook => true, :twitter => true)
157
+
153
158
  === Cities
154
159
 
155
160
  A user's cities represent the cities in which they have collected spots.
@@ -258,6 +263,17 @@ A data source represents a free or purchasable content stream of collected spots
258
263
  <Whatser::DataSource @review_count=405, @poi_count=0, @price=0.0, @collection_count=0, @name="Rough Guides - Barcelona", @media_count=0, @id=9, @users=[{"name": "premium user", "id": 33}]>
259
264
 
260
265
 
266
+ === Activity Feeds
267
+
268
+ User activity is recorded by the API, for example when a user adds or tags a POI, befriends another user, or uploads a photo. This activity is recorded in a user's activity feed, a collection of short messages detailing that user's activity. You can view feeds either by user, or globally.
269
+
270
+ Whatser.client.feed.user( user_id, :page => 1, :per_page => 10 )
271
+ Whatser.client.feed.mine( :page => 1, :per_page => 10 )
272
+ Whatser.client.feed.global( :page => 1, :per_page => 10 )
273
+
274
+ <Whatser::ActivityFeed @message="you uploaded a photo.", @poi_id=101, @object_avatar=nil, @user_avatar="http://example.com/avatars/imgs/1.jpg", @user_id=33, @object_name="Something", @user_name="This User", @object_id=1001, @poi_name="Some Spot", @city="Amsterdam">
275
+
276
+
261
277
  === Social Networks
262
278
 
263
279
  The API integrates with several social networks (Facebook, Foursquare, Gowalla, and Twitter), allowing users from those networks to login from or connect their accounts with Whatser. All four networks are supported for connecting a user account (via the OAuth 2 web authorization flow), and disconnecting a user account. In addition, the API provides access to other select parts of these networks to support social media and location-based user activity.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,15 +1,35 @@
1
+ require 'cgi'
2
+
1
3
  module Whatser
2
4
  class Service < Whatser::Resource
3
- def key
4
- self.class.name.split('::').last.downcase
5
+ class << self
6
+ def key
7
+ self.name.split('::').last.downcase
8
+ end
9
+
10
+ def connection_url(params={})
11
+ "#{client.api_uri}/oauth/services/#{key}/authorize?#{connection_querystring(params)}"
12
+ end
13
+
14
+ def disconnection_url(params={})
15
+ "#{client.api_uri}/oauth/services/#{key}/disconnect"
16
+ end
17
+
18
+ def connection_querystring(params={})
19
+ auth_params( params ).map{|k,v| "#{CGI.escape( (k || '').to_s )}=#{CGI.escape( (v || '').to_s )}"}.join('&')
20
+ end
21
+
22
+ def auth_params(params={})
23
+ {
24
+ :client_id => client.api_key,
25
+ :redirect_uri => client.redirect_uri
26
+ }.merge( params )
27
+ end
5
28
  end
6
29
 
7
- def connection_url
8
- "#{client.api_uri}/oauth/services/#{key}/authorize"
9
- end
30
+ def key; self.class.key; end
10
31
 
11
- def disconnection_url
12
- "#{client.api_uri}/oauth/services/#{key}/disconnect"
13
- end
32
+ def connection_url(params={}); self.class.connection_url(params) end
33
+ def disconnection_url(params={}); self.class.disconnection_url(params) end
14
34
  end
15
35
  end
@@ -13,6 +13,7 @@ module Whatser
13
13
  end
14
14
  end
15
15
 
16
+ attr_accessor :logger
16
17
  attr_accessor *Whatser::Configuration::VALID_OPTIONS_KEYS
17
18
  def initialize(options={})
18
19
  Whatser::Configuration::VALID_OPTIONS_KEYS.each do |key|
@@ -50,5 +51,10 @@ module Whatser
50
51
  def authorized?
51
52
  !oauth_token.blank?
52
53
  end
54
+
55
+ def log(msg,level=:info)
56
+ return if logger.blank?
57
+ logger.send(level, msg)
58
+ end
53
59
  end
54
60
  end
@@ -1,6 +1,6 @@
1
1
  module Whatser
2
2
  module Configuration
3
- VALID_OPTIONS_KEYS = [:api_key,:api_secret,:username,:password,:oauth_token,:api_uri,:redirect_uri]
3
+ VALID_OPTIONS_KEYS = [:api_key,:api_secret,:username,:password,:oauth_token,:api_uri,:redirect_uri,:logger]
4
4
 
5
5
  DEFAULT_API_URI = 'https://production-2.sogeoapi.com'.freeze
6
6
 
@@ -9,6 +9,7 @@ module Whatser
9
9
  base.api_secret = nil
10
10
  base.oauth_token = nil
11
11
  base.redirect_uri = nil
12
+ base.logger = nil
12
13
  base.api_uri = DEFAULT_API_URI
13
14
  end
14
15
  end
@@ -7,7 +7,10 @@ module Whatser
7
7
  end
8
8
 
9
9
  def request(verb, path, params={}, options={})
10
+ log("Whatser API Request: #{verb} @ #{path} with : #{params.to_s}")
10
11
  response = HTTParty.send( verb, compose_url(path), request_options( params ) )
12
+ log("Whatser API Response: #{response.code} with : #{response.body.to_s}")
13
+
11
14
  Whatser::Response.new( response.body, {:code => response.code}.merge(options) )
12
15
  end
13
16
 
@@ -17,10 +20,14 @@ module Whatser
17
20
  "#{api_uri}#{path}"
18
21
  end
19
22
 
20
- def request_options(params={})
23
+ def request_options(params={}, options={})
21
24
  params ||= {}
22
- return { :query => {:oauth_token => oauth_token}.merge(params[:query] || {}),
23
- :body => params[:body] }
25
+ return { :query => query_options(params[:query], options), :body => params[:body] }
26
+ end
27
+
28
+ def query_options(params, options={})
29
+ base = options[:auth] == :key ? {:client_id => api_key} : {:oauth_token => oauth_token}
30
+ base.merge( params || {} )
24
31
  end
25
32
  end
26
33
  end
@@ -1,7 +1,7 @@
1
1
  module Whatser
2
2
  class ActivityFeed < Whatser::Resource
3
3
  attr_accessor :user_id,:user_name,:user_avatar,:poi_id,:poi_name,:object_id,:object_name,:object_avatar
4
- attr_accessor :city,:message,:created_At
4
+ attr_accessor :city,:message,:created_at
5
5
 
6
6
  class << self
7
7
  def mine(params={})
@@ -32,7 +32,7 @@ module Whatser
32
32
  end
33
33
 
34
34
  def invite(emails)
35
- api_request :post, "/api/invites", {:query => emails}
35
+ api_request :post, "/api/invites", {:query => {'emails' => emails} }
36
36
  end
37
37
  end
38
38
 
@@ -45,9 +45,13 @@ module Whatser
45
45
  end
46
46
 
47
47
  def connection
48
- Whatser::Follow.connection(id)
48
+ Whatser::Follow.set(self.class.client).connection(id)
49
49
  end
50
50
 
51
+ def thanks(poi_id,params={})
52
+ api_request :post, "/api/users/#{id}/poi/#{poi_id}/thanks", {:query => params}
53
+ end
54
+
51
55
  def oauth_token
52
56
  if access_tokens.blank?
53
57
  nil
data/test/test_client.rb CHANGED
@@ -1,10 +1,20 @@
1
1
  require 'helper'
2
+ require 'logger'
2
3
 
3
4
  class TestClient < Test::Unit::TestCase
5
+ def setup
6
+ @client = Whatser::Client.new
7
+ end
8
+
9
+ def reset_config_defaults
10
+ Whatser::Client.configure do |config|
11
+ config.logger = nil
12
+ end
13
+ end
14
+
4
15
  def test_attr_accessor
5
- client = Whatser::Client.new
6
16
  Whatser::Configuration::VALID_OPTIONS_KEYS.each do |a|
7
- assert client.respond_to?(a)
17
+ assert @client.respond_to?(a)
8
18
  end
9
19
  end
10
20
 
@@ -39,6 +49,18 @@ class TestClient < Test::Unit::TestCase
39
49
  assert_equal 'key', client.api_key
40
50
  assert_equal 'secret', client.api_secret
41
51
  assert_equal '123abc', client.oauth_token
52
+ reset_config_defaults
53
+ end
54
+
55
+ def test_initialize_with_logger
56
+ logger = Logger.new('test-logger.txt')
57
+ Whatser::Client.configure do |config|
58
+ config.logger = logger
59
+ end
60
+ client = Whatser::Client.new
61
+ assert_equal logger, client.logger
62
+ reset_config_defaults
63
+ File.delete('test-logger.txt') if File::exists?( 'test-logger.txt' )
42
64
  end
43
65
 
44
66
  def test_resources
@@ -75,10 +97,28 @@ class TestClient < Test::Unit::TestCase
75
97
  end
76
98
 
77
99
  def test_authorized
78
- client = Whatser::Client.new
79
- assert_equal false, client.authorized?
100
+ assert_equal false, @client.authorized?
101
+
102
+ @client.oauth_token = 'test'
103
+ assert_equal true, @client.authorized?
104
+ end
105
+
106
+ def test_log
107
+ @client.logger = Logger.new('test-logger.txt')
108
+
109
+ assert_equal true, @client.log('test123')
110
+ assert_equal true, @client.log('test456', :error)
111
+
112
+ log = File.open('test-logger.txt') {|f| f.read}
113
+ assert log.include?('INFO -- : test123')
114
+ assert log.include?('ERROR -- : test456')
80
115
 
81
- client.oauth_token = 'test'
82
- assert_equal true, client.authorized?
116
+ File.delete('test-logger.txt') if File::exists?( 'test-logger.txt' )
117
+ @client.logger = nil
118
+ end
119
+
120
+ def test_log_without_logger
121
+ @client.logger = nil
122
+ assert_nil @client.log('test')
83
123
  end
84
124
  end
@@ -6,11 +6,12 @@ class TestConfiguration < Test::Unit::TestCase
6
6
  config.api_key = nil
7
7
  config.api_secret = nil
8
8
  config.oauth_token = nil
9
- end
9
+ config.logger = nil
10
+ end
10
11
  end
11
12
 
12
13
  def test_valid_options_keys
13
- expected = [:api_key,:api_secret,:username,:password,:oauth_token,:api_uri,:redirect_uri]
14
+ expected = [:api_key,:api_secret,:username,:password,:oauth_token,:api_uri,:redirect_uri,:logger]
14
15
  assert_equal expected, Whatser::Configuration::VALID_OPTIONS_KEYS
15
16
  end
16
17
 
@@ -21,6 +22,7 @@ class TestConfiguration < Test::Unit::TestCase
21
22
  assert_equal nil, client.api_secret
22
23
  assert_equal nil, client.oauth_token
23
24
  assert_equal nil, client.redirect_uri
25
+ assert_equal nil, client.logger
24
26
  assert_equal Whatser::Configuration::DEFAULT_API_URI, client.api_uri
25
27
  end
26
28
  end
@@ -1,7 +1,16 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestFacebook < Test::Unit::TestCase
4
+ def setup
5
+ @client = Whatser::Client.new(:oauth_token => '1234')
6
+ end
7
+
4
8
  def test_key
5
9
  assert_equal 'facebook', Whatser::Facebook.new.key
6
10
  end
11
+
12
+ def test_connection_url
13
+ expected = "#{@client.api_uri}/oauth/services/facebook/authorize"
14
+ assert_equal expected, @client.facebook.connection_url
15
+ end
7
16
  end
@@ -1,7 +1,16 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestFoursquare < Test::Unit::TestCase
4
+ def setup
5
+ @client = Whatser::Client.new(:oauth_token => '1234')
6
+ end
7
+
4
8
  def test_key
5
9
  assert_equal 'foursquare', Whatser::Foursquare.new.key
6
10
  end
11
+
12
+ def test_connection_url
13
+ expected = "#{@client.api_uri}/oauth/services/foursquare/authorize"
14
+ assert_equal expected, @client.foursquare.connection_url
15
+ end
7
16
  end
data/test/test_gowalla.rb CHANGED
@@ -1,7 +1,16 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestGowalla < Test::Unit::TestCase
4
+ def setup
5
+ @client = Whatser::Client.new(:oauth_token => '1234')
6
+ end
7
+
4
8
  def test_key
5
9
  assert_equal 'gowalla', Whatser::Gowalla.new.key
6
10
  end
11
+
12
+ def test_connection_url
13
+ expected = "#{@client.api_uri}/oauth/services/gowalla/authorize"
14
+ assert_equal expected, @client.gowalla.connection_url
15
+ end
7
16
  end
data/test/test_http.rb CHANGED
@@ -23,6 +23,12 @@ class TestHttp < Test::Unit::TestCase
23
23
  assert_equal expected, @client.send(:request_options, {:body => {:extra => 1} })
24
24
  end
25
25
 
26
+ def test_request_options_with_client_auth
27
+ @client.api_key = '123'
28
+ expected = {:query => {:client_id => '123'}, :body => nil}
29
+ assert_equal expected, @client.send(:request_options, {}, {:auth => :key})
30
+ end
31
+
26
32
  def test_base_uri_on_included
27
33
  assert_equal @client.api_uri, Whatser::Http.base_uri
28
34
  end
@@ -66,6 +66,11 @@ class TestResource < Test::Unit::TestCase
66
66
  assert mod.is_a?(Whatser::Poi)
67
67
  end
68
68
 
69
+ def test_from_hash_to_model_with_string
70
+ mod = Whatser::Resource.from_hash_to_model('freetextstringtest')
71
+ assert mod.is_a?(Whatser::Resource)
72
+ end
73
+
69
74
  def test_from_hash_to_model_empty
70
75
  mod = Whatser::Resource.from_hash_to_model(nil)
71
76
  assert mod.is_a?(Whatser::Resource)
@@ -0,0 +1,45 @@
1
+ require 'helper'
2
+
3
+ class TestService < Test::Unit::TestCase
4
+ def setup
5
+ @client = Whatser::Client.new(:api_key => '123', :redirect_uri => 'http://example.com')
6
+ @service = Whatser::Service.set(@client).new
7
+ end
8
+
9
+ def test_key
10
+ assert_equal 'service', Whatser::Service.key
11
+ assert_equal 'service', @service.key
12
+ end
13
+
14
+ def test_connection_url
15
+ expected = "#{@client.api_uri}/oauth/services/service/authorize?"
16
+ expected = expected + "client_id=#{@client.api_key}&redirect_uri=#{ CGI.escape(@client.redirect_uri) }"
17
+ assert_equal expected, @service.connection_url
18
+ end
19
+
20
+ def test_disconnection_url
21
+ expected = "#{@client.api_uri}/oauth/services/service/disconnect"
22
+ assert_equal expected, @service.disconnection_url
23
+ end
24
+
25
+ def test_auth_params
26
+ expected = {:client_id => @client.api_key, :redirect_uri => @client.redirect_uri}
27
+ assert_equal expected, Whatser::Service.auth_params
28
+ end
29
+
30
+ def test_auth_params_with_params
31
+ override_url = 'http://sub.example.com'
32
+ expected = {:client_id => @client.api_key, :redirect_uri => override_url}
33
+ assert_equal expected, Whatser::Service.auth_params(:redirect_uri => override_url)
34
+ end
35
+
36
+ def test_connection_querystring
37
+ expected = "client_id=#{@client.api_key}&redirect_uri=#{ CGI.escape(@client.redirect_uri) }"
38
+ assert_equal expected, Whatser::Service.connection_querystring
39
+ end
40
+
41
+ def test_connection_querystring_with_params
42
+ expected = "display=wap&client_id=#{@client.api_key}&redirect_uri=#{ CGI.escape(@client.redirect_uri) }"
43
+ assert_equal expected, Whatser::Service.connection_querystring(:display => 'wap')
44
+ end
45
+ end
data/test/test_twitter.rb CHANGED
@@ -1,7 +1,16 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestTwitter < Test::Unit::TestCase
4
+ def setup
5
+ @client = Whatser::Client.new(:oauth_token => '1234')
6
+ end
7
+
4
8
  def test_key
5
9
  assert_equal 'twitter', Whatser::Twitter.new.key
6
10
  end
11
+
12
+ def test_connection_url
13
+ expected = "#{@client.api_uri}/oauth/services/twitter/authorize"
14
+ assert_equal expected, @client.twitter.connection_url
15
+ end
7
16
  end
data/test/test_user.rb CHANGED
@@ -42,6 +42,14 @@ class TestUser < Test::Unit::TestCase
42
42
  assert @client.users.create(:name => 'test', :email => 'test@example.com', :password => '123pass').is_a?(Whatser::Response)
43
43
  end
44
44
 
45
+ def test_connection
46
+ assert @user.connection.is_a?(Whatser::Response)
47
+ end
48
+
49
+ def test_thanks
50
+ assert @user.thanks(1, :opt => 'test').is_a?(Whatser::Response)
51
+ end
52
+
45
53
  def test_save
46
54
  assert @user.save.is_a?(Whatser::Response)
47
55
  end
data/whatser.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{whatser}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Travis Dunn"]
12
- s.date = %q{2011-01-12}
12
+ s.date = %q{2011-01-20}
13
13
  s.description = %q{The 'Whatser API' Gem is a simple Ruby / Rails wrapper for interacting with Whatser's location-based web services (see http://docs.sogeoapi.com for more details).}
14
14
  s.email = %q{cmd@travisdunn.com}
15
15
  s.extra_rdoc_files = [
@@ -71,6 +71,7 @@ Gem::Specification.new do |s|
71
71
  "test/test_resource.rb",
72
72
  "test/test_response.rb",
73
73
  "test/test_review.rb",
74
+ "test/test_service.rb",
74
75
  "test/test_subscription.rb",
75
76
  "test/test_tag.rb",
76
77
  "test/test_twitter.rb",
@@ -103,6 +104,7 @@ Gem::Specification.new do |s|
103
104
  "test/test_resource.rb",
104
105
  "test/test_response.rb",
105
106
  "test/test_review.rb",
107
+ "test/test_service.rb",
106
108
  "test/test_subscription.rb",
107
109
  "test/test_tag.rb",
108
110
  "test/test_twitter.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatser
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Travis Dunn
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-12 00:00:00 +01:00
18
+ date: 2011-01-20 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -170,6 +170,7 @@ files:
170
170
  - test/test_resource.rb
171
171
  - test/test_response.rb
172
172
  - test/test_review.rb
173
+ - test/test_service.rb
173
174
  - test/test_subscription.rb
174
175
  - test/test_tag.rb
175
176
  - test/test_twitter.rb
@@ -230,6 +231,7 @@ test_files:
230
231
  - test/test_resource.rb
231
232
  - test/test_response.rb
232
233
  - test/test_review.rb
234
+ - test/test_service.rb
233
235
  - test/test_subscription.rb
234
236
  - test/test_tag.rb
235
237
  - test/test_twitter.rb