Class: HomeAway::API::Client

Inherits:
Object
  • Object
show all
Includes:
Domain::AddMessage, Domain::Conversation, Domain::Listing, Domain::ListingReview, Domain::ListingReviews, Domain::Me, Domain::MyInbox, Domain::MyListings, Domain::MyReservations, Domain::Quote, Domain::Search, Domain::SubmitReview, Util::OAuth
Defined in:
lib/homeaway/api/client.rb,
lib/homeaway/api/domain/client_includes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Domain::Conversation

#conversation

Methods included from Domain::AddMessage

#add_message

Methods included from Domain::MyReservations

#my_reservations

Methods included from Domain::Me

#me

Methods included from Domain::SubmitReview

#submit_review

Methods included from Domain::MyInbox

#my_inbox

Methods included from Domain::MyListings

#my_listings

Methods included from Domain::Quote

#quote

Methods included from Domain::Search

#search

Methods included from Domain::ListingReview

#listing_review

Methods included from Domain::ListingReviews

#listing_reviews

Methods included from Domain::Listing

#listing

Methods included from Util::OAuth

#auth_url, #credentials, #oauth_code=, #state

Constructor Details

#initialize(opts = {}) ⇒ HomeAway::API::Client

Instantiates a new HomeAway API client

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :client_id (String)

    Your HomeAway API OAuth client id. Required here if not set globally

  • :client_secret (String)

    Your HomeAway API OAuth client secret. Required here if not set globally

  • :refresh_token (String)

    An existing token if you already have one saved from a previous usage of the api

  • :state (String)

    A value that will be returned via callback



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/homeaway/api/client.rb', line 58

def initialize(opts={})
  @configuration = Hashie::Mash.new(self.class.default_configuration.merge(opts))
  if opts.has_key?(:refresh_token)
    @configuration[:manual_token_supplied] = true
    @refresh_token = opts.delete(:refresh_token)
    refresh
  end
  validate_configuration
  logger.debug("client initialized with configuration: #{@configuration}")
  attempt_auth if @token.nil?
end

Instance Attribute Details

#configurationHash

Returns the current client configuration.

Returns:

  • (Hash)

    the current client configuration



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

def configuration
  @configuration
end

#modeSymbol (readonly)

Returns the current authentication state of this client. One of either :unauthorized, :two_legged, or :three_legged.

Returns:

  • (Symbol)

    the current authentication state of this client. One of either :unauthorized, :two_legged, or :three_legged



44
45
46
# File 'lib/homeaway/api/client.rb', line 44

def mode
  @mode
end

#refresh_tokenString (readonly)

Returns The current refresh token.

Returns:

  • (String)

    The current refresh token



91
92
93
# File 'lib/homeaway/api/client.rb', line 91

def refresh_token
  @refresh_token
end

#tokenString (readonly)

Returns The current bearer auth token.

Returns:

  • (String)

    The current bearer auth token

Raises:



41
42
43
# File 'lib/homeaway/api/client.rb', line 41

def token
  @token
end

#token_expiresTime (readonly)

Returns the time that the current token in use on this client will expire.

Returns:

  • (Time)

    the time that the current token in use on this client will expire



47
48
49
# File 'lib/homeaway/api/client.rb', line 47

def token_expires
  @token_expires
end

Class Method Details

.configure {|@@default_configuration| ... } ⇒ Hash

Pass a block expecting a single hash parameter and set any global configuration settings that will be inherited by all instances created in the future

Yields:

Returns:

  • (Hash)

    the global default configuration



28
29
30
31
# File 'lib/homeaway/api/client.rb', line 28

def self.configure
  yield @@default_configuration if block_given?
  @@default_configuration
end

.default_configurationHash

Returns the global default configuration.

Returns:

  • (Hash)

    the global default configuration



21
22
23
# File 'lib/homeaway/api/client.rb', line 21

def self.default_configuration
  @@default_configuration ||= HomeAway::API::Util::Defaults.instance.to_hash
end

Instance Method Details

#configure {|@configuration| ... } ⇒ Hash

Update the configuration of this particular instance of the client. Pass a block expecting a single hash parameter to update the configuration settings.

Yields:

Returns:

  • (Hash)

    This client’s configuration



73
74
75
76
77
# File 'lib/homeaway/api/client.rb', line 73

def configure
  yield @configuration if block_given?
  validate_configuration
  @configuration
end

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



138
139
140
# File 'lib/homeaway/api/client.rb', line 138

def delete(url, params={})
  method :delete, url, {}, params
end

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



123
124
125
# File 'lib/homeaway/api/client.rb', line 123

def get(url, params={})
  method :get, url, {}, params
end

#loggerObject

Returns The current logger that has been configured.

Returns:

  • (Object)

    The current logger that has been configured



80
81
82
# File 'lib/homeaway/api/client.rb', line 80

def logger
  @configuration.logger
end

#marshal_dumpObject



96
97
98
99
100
101
102
# File 'lib/homeaway/api/client.rb', line 96

def marshal_dump
  # we lose our logger instance naively here, marshal dump doesn't like
  # one of its fields
  dump_config = configuration.dup.to_hash
  dump_config.delete('logger')
  [@token, token_expires, dump_config]
end

#marshal_load(array) ⇒ Object



105
106
107
108
109
110
# File 'lib/homeaway/api/client.rb', line 105

def marshal_load(array)
  @token = array[0]
  @token_expires = array[1]
  @configuration = Hashie::Mash.new(array[2])
  @configuration.logger = HomeAway::API::Util::Defaults.instance.logger
end

#options(url, params = {}) ⇒ Object



143
144
145
# File 'lib/homeaway/api/client.rb', line 143

def options(url, params={})
  method :options, url, {}, params
end

#post(url, body, params = {}) ⇒ Object



133
134
135
# File 'lib/homeaway/api/client.rb', line 133

def post(url, body, params={})
  method :post, url, body, params
end

#put(url, body, params = {}) ⇒ Object



128
129
130
# File 'lib/homeaway/api/client.rb', line 128

def put(url, body, params={})
  method :put, url, body, params
end

#token_expired?Boolean

Returns Has the token that the client is currently using expired?.

Returns:

  • (Boolean)

    Has the token that the client is currently using expired?



113
114
115
116
117
118
119
120
# File 'lib/homeaway/api/client.rb', line 113

def token_expired?
  return false if @configuration[:manual_token_supplied]
  begin
    Time.now >= token_expires
  rescue
    true
  end
end