class HomeAway::API::Client

Attributes

configuration[RW]
mode[R]
refresh_token[R]
token[R]
token_expires[R]

Public Class Methods

configure() { |default_configuration| ... } click to toggle source

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 @return [Hash] the global default configuration

# File lib/homeaway/api/client.rb, line 28
def self.configure
  yield @@default_configuration if block_given?
  @@default_configuration
end
default_configuration() click to toggle source

@return [Hash] the global default configuration

# File lib/homeaway/api/client.rb, line 21
def self.default_configuration
  @@default_configuration ||= HomeAway::API::Util::Defaults.instance.to_hash
end
new(opts={}) click to toggle source

Instantiates a new HomeAway API client

@option opts [String] :client_id Your HomeAway API OAuth client id. Required here if not set globally @option opts [String] :client_secret Your HomeAway API OAuth client secret. Required here if not set globally @option opts [String] :refresh_token An existing token if you already have one saved from a previous usage of the api @option opts [String] :state A value that will be returned via callback @return [HomeAway::API::Client] a newly instantiated HomeAway API client

# 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

Public Instance Methods

configure() { |configuration| ... } click to toggle source

Update the configuration of this particular instance of the client. Pass a block expecting a single hash parameter to update the configuration settings. @return [Hash] This client's configuration

# File lib/homeaway/api/client.rb, line 73
def configure
  yield @configuration if block_given?
  validate_configuration
  @configuration
end
delete(url, params={}) click to toggle source

@private

# File lib/homeaway/api/client.rb, line 138
def delete(url, params={})
  method :delete, url, {}, params
end
get(url, params={}) click to toggle source

@private

# File lib/homeaway/api/client.rb, line 123
def get(url, params={})
  method :get, url, {}, params
end
logger() click to toggle source

@return [Object] The current logger that has been configured

# File lib/homeaway/api/client.rb, line 80
def logger
  @configuration.logger
end
marshal_dump() click to toggle source

@private

# 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) click to toggle source

@private

# 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={}) click to toggle source

@private

# File lib/homeaway/api/client.rb, line 143
def options(url, params={})
  method :options, url, {}, params
end
post(url, body, params={}) click to toggle source

@private

# File lib/homeaway/api/client.rb, line 133
def post(url, body, params={})
  method :post, url, body, params
end
put(url, body, params={}) click to toggle source

@private

# File lib/homeaway/api/client.rb, line 128
def put(url, body, params={})
  method :put, url, body, params
end
token_expired?() click to toggle source

@return [Boolean] Has the token that the client is currently using expired?

# 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

Private Instance Methods

adapter() click to toggle source
# File lib/homeaway/api/client.rb, line 161
def adapter
  registered_adapters = {
      :faraday => HomeAway::API::Adapters::FaradayAdapter,
      :hurley => HomeAway::API::Adapters::HurleyAdapter
  }
  raise ArgumentError.new("Invalid adapter #{@configuration[:adapter]}") unless registered_adapters.keys.include? @configuration[:adapter]
  logger.debug("using adapter: #{@configuration[:adapter]}")
  registered_adapters[@configuration[:adapter]]
end
attempt_auth() click to toggle source
# File lib/homeaway/api/client.rb, line 200
def attempt_auth
  begin
    two_legged!
    @mode = :two_legged
  rescue => e
    logger.info("failed to perform automatic 2 legged oauth due to: #{e.to_s}")
    @mode = :unauthorized
  end
end
headers() click to toggle source
# File lib/homeaway/api/client.rb, line 171
def headers
  headers = {}
  headers['content-type'] = 'application/json'
  headers['Cache-control'] = @configuration[:cache_control]
  headers['Authorization'] = "Bearer #{token}"
  headers['User-Agent'] = "HomeAway API ruby_sdk/#{HomeAway::API::VERSION}"
  headers['X-HomeAway-RequestMarker'] = SecureRandom.uuid
  headers['X-HomeAway-TestMode'] = 'true' if @configuration[:test_mode]
  logger.debug("Sending headers: #{headers.to_json}")
  headers
end
method(method, url, body, params) click to toggle source
# File lib/homeaway/api/client.rb, line 183
def method(method, url, body, params)
  if token_expired?
    if @configuration[:auto_reauth]
      logger.info('Token expired and auth_reauth is enabled, attempting 2 legged oauth.')
      attempt_auth
      logger.info("Re-authentication attempt completed. Current client status: #{@mode}")
    else
      raise HomeAway::API::Errors::TokenExpiredError.new('token is expired, please login again via the oauth url')
    end
  end
  logger.info("#{method.to_s.upcase} to #{url} with params #{params.to_json}")
  site = @configuration[:api_site] ||= @configuration[:site]
  response = adapter.call(site, @configuration[:connection_opts], headers, method, url, body, params)
  logger.debug("returning payload: #{response.to_json}")
  response
end
validate_configuration() click to toggle source
# File lib/homeaway/api/client.rb, line 149
def validate_configuration
  required_uuids = [:client_id, :client_secret]
  [required_uuids].flatten.each do |required_configuration_directive|
    raise ArgumentError.new("#{required_configuration_directive.to_s} is required but not supplied") if (@configuration[required_configuration_directive] == nil || @configuration[required_configuration_directive].nil? || !configuration.has_key?(required_configuration_directive))
  end
  required_uuids.each do |uuid|
    HomeAway::API::Util::Validators.uuid(@configuration[uuid])
  end
  true
end