dopplr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Dopplr
2
+
3
+ This library provides objects for talking to some of the data available in Dopplr, The Social Atlas. It is essentially a multi-user wrapping of the Dopplr API, with each object being bound to a client.
4
+
5
+ ## Usage
6
+
7
+ ### First things first
8
+
9
+ Require the Dopplr library and create a `Dopplr::Client` instance. Return a login URL for the user to obtain a token, store the token for that user.
10
+
11
+ require 'dopplr'
12
+
13
+ client = Dopplr::Client.new #=> #<Dopplr::Client:0x578208 ...>
14
+ client.login_url "http://www.you.com/" #=> "https://www.dopplr.com/api/..."
15
+
16
+ Now that you have a single-use token, assign it to the `Dopplr::Client` object and generate a session token.
17
+
18
+ client.token = '1a2b3c4d5e6f' #=> '1a2b3c4d5e6f'
19
+ client.create_session #=> '3c4d5e6f1a2b'
20
+
21
+ ### Instantiating and working with objects
22
+
23
+ All Dopplr objects are created using the client as a base, you can then branch of from them to dig deeper.
24
+
25
+ mike = client.traveller #=> Dopplr::Traveller for the token holder
26
+ chicago = client.city 4887398 #=> Dopplr::City for ID 4887398
27
+ montreal = mike.home_city #=> Dopplr::City for mike's home city
28
+ trip = mike.trips.first #=> Dopplr::Trip for mike's first trip
29
+
30
+ Return some basic information about each object.
31
+
32
+ montreal.country #=> "Canada"
33
+ montreal.timezone #=> "America/Montreal"
34
+ montreal.localtime #=> Wed Jul 22 09:30:15 2009
35
+
36
+ chicago.url #=> "http://www.dopplr.com/place/us/il/chicago"
37
+ chicago.latitude #=> 41.85
38
+ chicago.longitude #=> -87.6501
39
+
40
+ mike.name #=> "Mike Richards"
41
+ mike.status #=> "is at home in Montreal"
42
+ mike.travel_today #=> false
43
+
44
+ trip.city.name #=> "Calgary"
45
+ trip.start #=> Sat Feb 16 00:00:00 2008
46
+ trip.return_transport #=> "plane"
47
+
48
+ Get trips and fellows for a particular traveller.
49
+
50
+ mike.trips #=> [#<Dopplr::Trip:0x56f4a0 ...>, ...]
51
+
52
+ mike.fellows #=> {:can_see_trips_of => [#<Dopplr::Traveller:0x570954 ...>, ...],
53
+ :shows_trips_to => [#<Dopplr::Traveller:0x59e8b1 ...> ...]}
54
+
55
+ Return a new `Dopplr::City` object without knowing the geoname_id (I'm feeling lucky).
56
+
57
+ portland = client.find_city "Portland"
58
+
59
+ portland.country #=> "United States"
60
+ portland.geoname_id #=> 5746545
@@ -0,0 +1,27 @@
1
+ module Dopplr
2
+ class City
3
+ attr_reader :name, :country, :timezone, :localtime, :latitude, :longitude
4
+ attr_reader :geoname_id, :country_code, :woeid, :rgb, :utc_offset, :url
5
+
6
+ def initialize(client, id)
7
+ @client = client
8
+ @geoname_id = id
9
+ populate
10
+ end
11
+
12
+ def populate
13
+ info = @client.get('city_info', :geoname_id => @geoname_id)['city']
14
+ @name = info['name']
15
+ @country = info['country']
16
+ @timezone = info['timezone']
17
+ @latitude = info['latitude']
18
+ @longitude = info['longitude']
19
+ @country_code = info['country_code']
20
+ @woeid = info['woeid']
21
+ @rgb = info['rgb']
22
+ @utc_offset = info['utcoffset']
23
+ @url = info['url']
24
+ @localtime = Time.parse info['localtime'].slice(0..-7)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,68 @@
1
+ module Dopplr
2
+ class Client
3
+ attr_accessor :token
4
+
5
+ def initialize(token=nil)
6
+ @token = token
7
+ end
8
+
9
+ # Makes an API call with @token.
10
+ def get(path, params={})
11
+ params['format'] = 'js'
12
+ params_uri = URI.escape(params.collect{|key,value| "#{key}=#{value}"}.join('&'))
13
+ url = "/api/#{path}/?#{params_uri}"
14
+ http = Net::HTTP.new("www.dopplr.com", 443)
15
+ http.use_ssl = true
16
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
17
+ http.start do |http|
18
+ request = Net::HTTP::Get.new url, 'Authorization' => "AuthSub token=\"#{@token}\""
19
+ JSON.parse(http.request(request).body)
20
+ end
21
+ end
22
+
23
+ # Returns a URL for getting a token.
24
+ def login_url(url)
25
+ return "https://www.dopplr.com/api/AuthSubRequest?scope=#{CGI.escape("http://www.dopplr.com/")}&next=#{CGI.escape(url)}&session=1"
26
+ end
27
+
28
+ # Changes @token into a session token.
29
+ def create_session
30
+ @token = get('AuthSubSessionToken')['token']
31
+ end
32
+
33
+ # Performs a search query.
34
+ def search(term, type=:all)
35
+ if type == :all
36
+ get 'search', :q => term
37
+ elsif type == :city
38
+ get 'city_search', :q => term
39
+ elsif type == :traveller
40
+ get 'traveller_search', :q => term
41
+ end
42
+ end
43
+
44
+ # Returns a new City object.
45
+ def city(city_id)
46
+ City.new(self, city_id)
47
+ end
48
+
49
+ # I'm feeling lucky city search.
50
+ def find_city(name)
51
+ city get('city_search', :q => name)['city'][0]['geoname_id']
52
+ end
53
+
54
+ # Returns a new Trip object.
55
+ def trip(trip_id)
56
+ Trip.new(self, trip_id)
57
+ end
58
+
59
+ # Returns a new Traveller object.
60
+ def traveller(username = nil)
61
+ if username
62
+ Traveller.new(self, username)
63
+ else
64
+ Traveller.new(self)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,72 @@
1
+ module Dopplr
2
+ class Traveller
3
+ attr_reader :nick, :name, :status, :travel_today, :icon_id, :email_sha1, :url
4
+
5
+ def initialize(client, user = nil)
6
+ @client = client
7
+ @params = {}
8
+ @params[:traveller] = user if user
9
+ populate
10
+ end
11
+
12
+ def populate
13
+ info = @client.get('traveller_info', @params)['traveller']
14
+ @nick = info['nick']
15
+ @name = info['name']
16
+ @status = info['status']
17
+ @travel_today = info['travel_today']
18
+ @icon_id = info['icon_id']
19
+ @email_sha1 = info['sha1email']
20
+ @url = info['url']
21
+ end
22
+
23
+ def current_city(options = {})
24
+ unless @current_city && !options[:force]
25
+ info = @client.get('traveller_info', @params)['traveller']
26
+ @current_city = City.new @client, info['current_city']['geoname_id']
27
+ end
28
+ @current_city
29
+ end
30
+
31
+ def home_city(options = {})
32
+ unless @home_city && !options[:force]
33
+ info = @client.get('traveller_info', @params)['traveller']
34
+ @home_city = City.new @client, info['home_city']['geoname_id']
35
+ end
36
+ @home_city
37
+ end
38
+
39
+ def trips(options = {})
40
+ unless @trips && !options[:force]
41
+ trips = @client.get('trips_info', @params)['trip']
42
+ if !trips.empty?
43
+ @trips = trips.map do |trip|
44
+ Trip.new @client, trip['id']
45
+ end
46
+ end
47
+ end
48
+ @trips ||= []
49
+ end
50
+
51
+ def fellows(options = {})
52
+ unless @fellows && !options[:force]
53
+ fellows = @client.get('fellows', @params)
54
+ fellows['can_see_trips_of'].map! do |fellow|
55
+ Traveller.new @client, fellow['nick']
56
+ end
57
+ fellows['shows_trips_to'].map! do |fellow|
58
+ hash = fellow.inject({}) do |memo, (key, value)|
59
+ memo[key.to_sym] = value
60
+ memo
61
+ end
62
+ Struct.new(*hash.keys).new(*hash.values_at(*hash.keys))
63
+ end
64
+ @fellows = fellows.inject({}) do |memo, (key, value)|
65
+ memo[key.to_sym] = value
66
+ memo
67
+ end
68
+ end
69
+ @fellows
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,31 @@
1
+ module Dopplr
2
+ class Trip
3
+ attr_reader :start, :finish, :city, :outgoing_transport, :return_transport, :tags, :notes
4
+ attr_reader :trip_id, :url
5
+
6
+ def initialize(client, id)
7
+ @client = client
8
+ @trip_id = id
9
+ populate
10
+ end
11
+
12
+ def populate
13
+ info = @client.get('trip_info', :trip_id => @trip_id)['trip']
14
+ @outgoing_transport = info['outgoing_transport_type']
15
+ @return_transport = info['return_transport_type']
16
+ @tags = info['tag']
17
+ @notes = info['note']
18
+ @url = info['url']
19
+ @start = Time.parse info['start']
20
+ @finish = Time.parse info['finish']
21
+ end
22
+
23
+ def city(options = {})
24
+ unless @city && !options[:force]
25
+ info = @client.get('trip_info', :trip_id => @trip_id)['trip']
26
+ @city = City.new @client, info['city']['geoname_id']
27
+ end
28
+ @city
29
+ end
30
+ end
31
+ end
data/lib/dopplr.rb ADDED
@@ -0,0 +1,12 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'time'
5
+ require 'cgi'
6
+ require 'net/https'
7
+ require 'json'
8
+
9
+ require 'dopplr/client'
10
+ require 'dopplr/traveller'
11
+ require 'dopplr/city'
12
+ require 'dopplr/trip'
data/test/helpers.rb ADDED
@@ -0,0 +1,7 @@
1
+ $: << File.dirname(__FILE__) + '/..'
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+
7
+ require 'lib/dopplr'
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/../helpers'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ context "A client" do
5
+ setup do
6
+ @client = Dopplr::Client.new
7
+ end
8
+
9
+ should "provide a proper login url" do
10
+ assert_equal @client.login_url("http://www.you.com/"), "https://www.dopplr.com/api/AuthSubRequest?scope=http%3A%2F%2Fwww.dopplr.com%2F&next=http%3A%2F%2Fwww.you.com%2F&session=1"
11
+ end
12
+
13
+ context "with a valid session token" do
14
+ setup do
15
+ @client.token = 'TOKEN'
16
+ end
17
+
18
+ should "have a base traveller" do
19
+ traveller = @client.traveller
20
+ assert_equal traveller.nick, 'mikeric'
21
+ end
22
+
23
+ should "be able to create new objects" do
24
+ assert @client.city 6173331
25
+ assert @client.trip 525522
26
+ end
27
+
28
+ should "be able to perform search queries" do
29
+ city_search = @client.search "Montreal", :city
30
+ assert city_search['city']
31
+ traveller_search = @client.search "Mike", :traveller
32
+ assert traveller_search['traveller']
33
+ end
34
+
35
+ should "be able to find a city" do
36
+ city = @client.find_city "Portland"
37
+ assert_equal city.geoname_id, 5746545
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dopplr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Richards
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-06 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: thoughtbot-shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description:
36
+ email: mike22e@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - README.md
45
+ - lib/dopplr.rb
46
+ - lib/dopplr/client.rb
47
+ - lib/dopplr/city.rb
48
+ - lib/dopplr/traveller.rb
49
+ - lib/dopplr/trip.rb
50
+ - test/helpers.rb
51
+ - test/unit/client_test.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/mikeric/dopplr
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: A Ruby library for talking to the Dopplr API
80
+ test_files: []
81
+