3 attr_accessor :login, :password
5 # Returns the response of the HTTP connection.
6 def http_connect(body = nil, require_auth = true, &block)
7 require_block(block_given?)
8 connection = create_http_connection
9 connection.start do |connection|
10 request = yield connection if block_given?
11 request.basic_auth(@login, @password) if require_auth
12 response = connection.request(request, body)
13 handle_rest_response(response)
18 # "Blesses" model object with client information
19 def bless_model(model)
20 model.bless(self) if model
23 def bless_models(list)
24 return bless_model(list) if list.respond_to?(:client=)
25 list.collect { |model| bless_model(model) } if list.respond_to?(:collect)
31 def raise_rest_error(response, uri = nil)
32 raise Twitter::RESTError.new(:code => response.code,
33 :message => response.message,
37 def handle_rest_response(response, uri = nil)
38 unless response.is_a?(Net::HTTPSuccess)
39 raise_rest_error(response, uri)
43 def create_http_connection
44 conn = Net::HTTP.new(@@config.host, @@config.port,
45 @@config.proxy_host, @@config.proxy_port,
46 @@config.proxy_user, @@config.proxy_pass)
47 if @@config.protocol == :ssl
49 conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
55 # can cache this in class variable since all "variables" used to
56 # create the contents of the HTTP header are determined by other
57 # class variables that are not designed to change after instantiation.
59 'User-Agent' => "Twitter4R v#{Twitter::Version.to_version} [#{@@config.user_agent}]",
60 'Accept' => 'text/x-json',
61 'X-Twitter-Client' => @@config.application_name,
62 'X-Twitter-Client-Version' => @@config.application_version,
63 'X-Twitter-Client-URL' => @@config.application_url,
68 def create_http_get_request(uri, params = {})
69 path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
70 Net::HTTP::Get.new(path, http_header)
73 def create_http_post_request(uri)
74 Net::HTTP::Post.new(uri, http_header)
77 def create_http_delete_request(uri, params = {})
78 path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
79 Net::HTTP::Delete.new(path, http_header)