2 alias :old_inspect :inspect
6 s.gsub!(/@password=".*?"/, '@password="XXXX"')
10 attr_accessor :login, :password
12 # Returns the response of the HTTP connection.
13 def http_connect(body = nil, require_auth = true, service = :rest, &block)
14 require_block(block_given?)
15 connection = create_http_connection(service)
16 connection.start do |connection|
17 request = yield connection if block_given?
18 request.basic_auth(@login, @password) if require_auth
19 response = connection.request(request, body)
20 handle_rest_response(response)
25 # "Blesses" model object with client information
26 def bless_model(model)
27 model.bless(self) if model
30 def bless_models(list)
31 return bless_model(list) if list.respond_to?(:client=)
32 list.collect { |model| bless_model(model) } if list.respond_to?(:collect)
38 def raise_rest_error(response, uri = nil)
39 map = JSON.parse(response.body)
40 raise Twitter::RESTError.new(:code => response.code,
41 :message => response.message,
42 :error => map["error"],
46 def handle_rest_response(response, uri = nil)
47 unless response.is_a?(Net::HTTPSuccess)
48 raise_rest_error(response, uri)
52 def create_http_connection(service = :rest)
55 protocol, host, port = @@config.protocol, @@config.host, @@config.port
57 protocol, host, port = @@config.search_protocol, @@config.search_host, @@config.search_port
59 conn = Net::HTTP.new(host, port,
60 @@config.proxy_host, @@config.proxy_port,
61 @@config.proxy_user, @@config.proxy_pass)
64 conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
70 # can cache this in class variable since all "variables" used to
71 # create the contents of the HTTP header are determined by other
72 # class variables that are not designed to change after instantiation.
74 'User-Agent' => "Twitter4R v#{Twitter::Version.to_version} [#{@@config.user_agent}]",
75 'Accept' => 'text/x-json',
76 'X-Twitter-Client' => @@config.application_name,
77 'X-Twitter-Client-Version' => @@config.application_version,
78 'X-Twitter-Client-URL' => @@config.application_url,
83 def create_http_get_request(uri, params = {})
84 path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
85 Net::HTTP::Get.new(path, http_header)
88 def create_http_post_request(uri)
89 Net::HTTP::Post.new(uri, http_header)
92 def create_http_delete_request(uri, params = {})
93 path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
94 Net::HTTP::Delete.new(path, http_header)