Class: Dopplr::Client
- Inherits:
-
Object
- Object
- Dopplr::Client
- Defined in:
- lib/dopplr/client.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#city(city_id) ⇒ Object
Returns a new City object.
-
#create_session ⇒ Object
Changes @token into a session token.
-
#find_city(name) ⇒ Object
I’m feeling lucky city search.
-
#get(path, params = {}) ⇒ Object
GET request with @token.
-
#initialize(token = nil) ⇒ Client
constructor
A new instance of Client.
-
#login_url(url) ⇒ Object
Returns a URL for getting a token.
-
#post(path, params = {}) ⇒ Object
POST request with @token.
-
#search(term, type = :all) ⇒ Object
Performs a search query.
-
#traveller(username = nil) ⇒ Object
Returns a new Traveller object.
-
#trip(trip_id) ⇒ Object
Returns a new Trip object.
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
#token ⇒ Object
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_session ⇒ Object
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 login_url(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 |