Class: Resourceful::HttpAccessor

Inherits:
Object
  • Object
show all
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

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


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

def initialize(options = {})
  options = Options.for(options).validate(:logger, :user_agent, :cache_manager, :authenticator, :authenticators, :http_adapter)

  @user_agent_tokens = [RESOURCEFUL_USER_AGENT_TOKEN]
  @auth_manager = AuthenticationManager.new()

  
  @user_agent_tokens.push(*Array(options.getopt(:user_agent)).flatten.reverse)
  self.logger    = options.getopt(:logger) || BitBucketLogger.new
  @cache_manager = options.getopt(:cache_manager) || NullCacheManager.new
  @http_adapter  = options.getopt(:http_adapter) || NetHttpAdapter.new
  
  Array(options.getopt([:authenticator, :authenticators])).flatten.each do |an_authenticator|
    add_authenticator(an_authenticator)
  end
end

Instance Attribute Details

#auth_managerObject (readonly)

Returns the value of attribute auth_manager.



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

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.



37
38
39
# File 'lib/resourceful/http_accessor.rb', line 37

def cache_manager
  @cache_manager
end

#http_adapterObject (readonly)

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



44
45
46
# File 'lib/resourceful/http_accessor.rb', line 44

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.



37
38
39
# File 'lib/resourceful/http_accessor.rb', line 37

def logger
  @logger
end

#user_agent_tokensObject (readonly)

Returns the value of attribute user_agent_tokens.



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

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.



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

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.



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

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.



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

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