Class: TripIt::OAuth

Inherits:
Base
  • Object
show all
Defined in:
lib/trip_it/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#Boolean, #camelize, #chkAndPopulate, #chkObjAndPopulate, #convertDT, #to_hash, #to_json, #to_xml

Methods included from ParamUtil

#address_param, #airportcode_param, #array_param, #boolean_param, #boolean_read_param, #camelize, #date_param, #datetime_param, #exceptions, #float_param, #integer_param, #string_param, #time_param, #traveler_array_param, #traveler_param

Constructor Details

#initialize(ctoken, csecret) ⇒ OAuth

Returns a new instance of OAuth.



6
7
8
# File 'lib/trip_it/oauth.rb', line 6

def initialize(ctoken, csecret)
  @consumer = ::OAuth::Consumer.new(ctoken, csecret, :site => 'https://api.tripit.com')
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/trip_it/oauth.rb', line 3

def access_token
  @access_token
end

#consumerObject (readonly)

Returns the value of attribute consumer.



3
4
5
# File 'lib/trip_it/oauth.rb', line 3

def consumer
  @consumer
end

Instance Method Details

#authorize_from_access(atoken, asecret) ⇒ Object



25
26
27
# File 'lib/trip_it/oauth.rb', line 25

def authorize_from_access(atoken, asecret)
  @access_token = ::OAuth::AccessToken.new(consumer, atoken, asecret)
end

#authorize_from_request(rtoken, rsecret, verifier) ⇒ Object



19
20
21
22
23
# File 'lib/trip_it/oauth.rb', line 19

def authorize_from_request(rtoken, rsecret, verifier)
  request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
  access = request_token.get_access_token(:oauth_verifier => verifier)
  @access_token = ::OAuth::AccessToken.new(consumer, access.token, access.secret)
end

#create(param) ⇒ Object

Only takes XML



44
45
46
47
# File 'lib/trip_it/oauth.rb', line 44

def create(param)
  request = access_token.post("/v1/create", "xml=<Request>#{URI.escape(param)}</Request>", {'Content-Type' => 'application/x-www-form-urlencoded'})
  returnResponse(request, "xml")
end

#delete(resource, params = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/trip_it/oauth.rb', line 55

def delete(resource, params={})
  params.merge!(:format => "json")
  params_string = params.collect{|k, v| "#{k}/#{v}"}.join('/')
  request = access_token.get("/v1/delete#{resource}/#{URI.escape(params_string)}")
  returnResponse(request)
end

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



29
30
31
32
33
34
# File 'lib/trip_it/oauth.rb', line 29

def get(resource, params={})
  params.merge!(:format => "json")
  params_string = params.collect{|k, v| "#{k}/#{v}"}.join('/')
  request = access_token.get("/v1/get#{resource}/#{URI.escape(params_string)}")
  returnResponse(request)
end

#list(resource, params = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/trip_it/oauth.rb', line 36

def list(resource, params={})
  params.merge!(:format => "json")
  params_string = params.collect{|k, v| "#{k}/#{v}"}.join('/')
  request = access_token.get("/v1/list#{resource}/#{URI.escape(params_string)}")
  returnResponse(request)
end

#replace(resource, param) ⇒ Object

Only takes XML



50
51
52
53
# File 'lib/trip_it/oauth.rb', line 50

def replace(resource, param)
  request = access_token.post("/v1/replace#{resource}", "xml=<Request>#{URI.escape(param)}</Request>", {'Content-Type' => 'application/x-www-form-urlencoded'})
  returnResponse(request, "xml")
end

#request_token(options = {}) ⇒ Object



15
16
17
# File 'lib/trip_it/oauth.rb', line 15

def request_token(options={})
  @request_token ||= consumer.get_request_token(options)
end

#returnResponse(request, format = "") ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/trip_it/oauth.rb', line 72

def returnResponse(request, format = "")
  case request
  when Net::HTTPOK
    if format == "xml"
      return request.body
    else
      return JSON.parse(request.body) 
    end
  when Net::HTTPBadRequest
    raise BadRequestException, request.body
  when Net::HTTPUnauthorized
    raise UnauthorizedException, request.body
  when Net::HTTPNotFound
    raise NotFoundException, request.body
  when Net::HTTPInternalServerError
    raise ServerError, request.body
  when Net::HTTPForbidden
    raise ForbiddenException, request.body
  when Net::HTTPServiceUnavailable
    raise ServiceUnavailableError, request.body
  end
end

#set_callback_url(url) ⇒ Object



10
11
12
13
# File 'lib/trip_it/oauth.rb', line 10

def set_callback_url(url)
  @request_token = nil
  request_token(:oauth_callback => url)
end

#subscribe(resource) ⇒ Object



62
63
64
65
# File 'lib/trip_it/oauth.rb', line 62

def subscribe(resource)
  request = access_token.get("/v1/subscribe/type#{resource}")
  returnResponse(request)
end

#unsubscribeObject



67
68
69
70
# File 'lib/trip_it/oauth.rb', line 67

def unsubscribe
  request = access_token.get("/v1/unsubscribe")
  returnResponse(request)
end