forked from pezra/resourceful
/
http_accessor.rb
79 lines (66 loc) · 2.77 KB
/
http_accessor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'net/http'
require 'resourceful/version'
require 'resourceful/options_interpreter'
require 'resourceful/authentication_manager'
require 'resourceful/cache_manager'
require 'resourceful/resource'
require 'resourceful/stubbed_resource_proxy'
module Resourceful
# 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.
class HttpAccessor
RESOURCEFUL_USER_AGENT_TOKEN = "Resourceful/#{RESOURCEFUL_VERSION}(Ruby/#{RUBY_VERSION})"
# This is an imitation Logger used when no real logger is
# registered. This allows most of the code to assume that there
# is always a logger available, which significantly improved the
# readability of the logging related code.
class BitBucketLogger
def warn(*args); end
def info(*args); end
def debug(*args); end
end
# 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.
attr_accessor :logger
attr_reader :auth_manager, :cache_manager
attr_reader :user_agent_tokens
INIT_OPTIONS = OptionsInterpreter.new do
option(:logger, :default => BitBucketLogger.new)
option(:user_agent, :default => []) {|ua| [ua].flatten}
option(:cache_manager, :default => NullCacheManager.new)
end
# 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.
def initialize(options = {})
@user_agent_tokens = [RESOURCEFUL_USER_AGENT_TOKEN]
INIT_OPTIONS.interpret(options) do |opts|
@user_agent_tokens.push(*opts[:user_agent].reverse)
self.logger = opts[:logger]
@auth_manager = AuthenticationManager.new(opts[:authentication_info_provider])
@cache_manager = opts[:cache_manager]
end
end
# 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+.
def user_agent_string
user_agent_tokens.reverse.join(' ')
end
# Returns a resource object representing the resource indicated
# by the specified URI. A resource object will be created if necessary.
def resource(uri)
resource = Resource.new(self, uri)
end
alias [] resource
end
end