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.



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

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

#create_sessionObject

Changes @token into a session token.



38
39
40
# File 'lib/dopplr/client.rb', line 38

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

#find_city(name) ⇒ Object

I’m feeling lucky city search.



59
60
61
# File 'lib/dopplr/client.rb', line 59

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

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

GET request with @token.



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

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

#login_url(url) ⇒ Object

Returns a URL for getting a token.



33
34
35
# File 'lib/dopplr/client.rb', line 33

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

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

POST request with @token.



21
22
23
24
25
26
27
28
29
30
# File 'lib/dopplr/client.rb', line 21

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

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

Performs a search query.



43
44
45
46
47
48
49
50
51
# File 'lib/dopplr/client.rb', line 43

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.



69
70
71
72
73
74
75
# File 'lib/dopplr/client.rb', line 69

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.



64
65
66
# File 'lib/dopplr/client.rb', line 64

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