Class: Dopplr::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dopplr/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/dopplr/client.rb', line 5

def initialize(token=nil)
  @token = token
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/dopplr/client.rb', line 3

def token
  @token
end

Instance Method Details

#city(city_id) ⇒ Object

Returns a new City object.



45
46
47
# File 'lib/dopplr/client.rb', line 45

def city(city_id)
  City.new(self, city_id)
end

#create_sessionObject

Changes @token into a session token.



29
30
31
# File 'lib/dopplr/client.rb', line 29

def create_session
  @token = get('AuthSubSessionToken')['token']
end

#find_city(name) ⇒ Object

I’m feeling lucky city search.



50
51
52
# File 'lib/dopplr/client.rb', line 50

def find_city(name)
  city get('city_search', :q => name)['city'][0]['geoname_id']
end

#get(path, params = {}) ⇒ Object

Makes an API call with @token.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dopplr/client.rb', line 10

def get(path, params={})
  params['format'] = 'js'
  params_uri = URI.escape(params.collect{|key,value| "#{key}=#{value}"}.join('&'))
  url = "/api/#{path}/?#{params_uri}"
  http = Net::HTTP.new("www.dopplr.com", 443)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.start do |http|
    request = Net::HTTP::Get.new url, 'Authorization' => "AuthSub token=\"#{@token}\""
    JSON.parse(http.request(request).body)
  end
end

#login_url(url) ⇒ Object

Returns a URL for getting a token.



24
25
26
# File 'lib/dopplr/client.rb', line 24

def (url)
  return "https://www.dopplr.com/api/AuthSubRequest?scope=#{CGI.escape("http://www.dopplr.com/")}&next=#{CGI.escape(url)}&session=1"
end

#search(term, type = :all) ⇒ Object

Performs a search query.



34
35
36
37
38
39
40
41
42
# File 'lib/dopplr/client.rb', line 34

def search(term, type=:all)
  if type == :all
    get 'search', :q => term
  elsif type == :city
    get 'city_search', :q => term
  elsif type == :traveller
    get 'traveller_search', :q => term
  end
end

#traveller(username = nil) ⇒ Object

Returns a new Traveller object.



60
61
62
63
64
65
66
# File 'lib/dopplr/client.rb', line 60

def traveller(username = nil)
  if username
    Traveller.new(self, username)
  else
    Traveller.new(self)
  end
end

#trip(trip_id) ⇒ Object

Returns a new Trip object.



55
56
57
# File 'lib/dopplr/client.rb', line 55

def trip(trip_id)
  Trip.new(self, trip_id)
end