Class: Resourceful::HttpAccessor

Inherits:
Object
  • Object
show all
Includes:
OptionsInterpretation
Defined in:
lib/resourceful/http_accessor.rb

Overview

This class provides a simple interface to the functionality provided by the Resourceful library. Conceptually this object acts a collection of all the resources available via HTTP.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsInterpretation

#extract_opts

Constructor Details

#initialize(options = {}) ⇒ HttpAccessor

Initializes a new HttpAccessor. Valid options:

`:logger`
:    A Logger object that the new HTTP accessor should send log messages

`:user_agent`
:    One or more additional user agent tokens to added to the user agent string.

`:cache_manager`
:    The cache manager this accessor should use.

`:authenticator`
:    Add a single authenticator for this accessor.

`:authenticators`
:    Enumerable of the authenticators for this accessor.

`http_adapter`
:    The HttpAdapter to be used by this accessor


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/resourceful/http_accessor.rb', line 69

def initialize(options = {})
  @user_agent_tokens = [RESOURCEFUL_USER_AGENT_TOKEN]
  @auth_manager = AuthenticationManager.new()

  extract_opts(options) do |opts|
    @user_agent_tokens.push(*opts.extract(:user_agent, :default => []) {|ua| [ua].flatten})
    
    self.logger    = opts.extract(:logger, :default => BitBucketLogger.new)
    @cache_manager = opts.extract(:cache_manager, :default => NullCacheManager.new)
    @http_adapter  = opts.extract(:http_adapter, :default => lambda{NetHttpAdapter.new})
    
    opts.extract(:authenticator, :required => false).tap{|a| add_authenticator(a) if a}
    opts.extract(:authenticators, :default => []).each { |a| add_authenticator(a) }
  end
end

Instance Attribute Details

#auth_managerObject (readonly)

Returns the value of attribute auth_manager.



41
42
43
# File 'lib/resourceful/http_accessor.rb', line 41

def auth_manager
  @auth_manager
end

#cache_managerObject

A logger object to which messages about the activities of this object will be written. This should be an object that responds to #info(message) and #debug(message).

Errors will not be logged. Instead an exception will be raised and the application code should log it if appropriate.



39
40
41
# File 'lib/resourceful/http_accessor.rb', line 39

def cache_manager
  @cache_manager
end

#http_adapterObject (readonly)

The adapter this accessor will use to make the actual HTTP requests.



46
47
48
# File 'lib/resourceful/http_accessor.rb', line 46

def http_adapter
  @http_adapter
end

#loggerObject

A logger object to which messages about the activities of this object will be written. This should be an object that responds to #info(message) and #debug(message).

Errors will not be logged. Instead an exception will be raised and the application code should log it if appropriate.



39
40
41
# File 'lib/resourceful/http_accessor.rb', line 39

def logger
  @logger
end

#user_agent_tokensObject (readonly)

Returns the value of attribute user_agent_tokens.



42
43
44
# File 'lib/resourceful/http_accessor.rb', line 42

def user_agent_tokens
  @user_agent_tokens
end

Instance Method Details

#add_authenticator(an_authenticator) ⇒ Object

Adds an Authenticator to the set used by the accessor.



100
101
102
# File 'lib/resourceful/http_accessor.rb', line 100

def add_authenticator(an_authenticator)
  auth_manager.add_auth_handler(an_authenticator)
end

#resource(uri, opts = {}) ⇒ Object Also known as: []

Returns a resource object representing the resource indicated by the specified URI. A resource object will be created if necessary.



94
95
96
# File 'lib/resourceful/http_accessor.rb', line 94

def resource(uri, opts = {})
  resource = Resource.new(self, uri, opts)
end

#user_agent_stringObject

Returns the string that identifies this HTTP accessor. If you want to add a token to the user agent string simply add the new token to the end of #user_agent_tokens.



88
89
90
# File 'lib/resourceful/http_accessor.rb', line 88

def user_agent_string
  user_agent_tokens.reverse.join(' ')
end