Added error message returned back in JSON when error HTTP response sent back from...
[twitter4r-core.git] / lib / twitter / client / base.rb
blob628dbc386cf0065767a0e6f5fee9096540ebc4c6
1 class Twitter::Client
2   alias :old_inspect :inspect
4   def inspect
5     s = old_inspect
6     s.gsub!(/@password=".*?"/, '@password="XXXX"')
7   end
9   protected
10     attr_accessor :login, :password
11     
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)
21                 response
22       end
23     end
24     
25     # "Blesses" model object with client information
26     def bless_model(model)
27         model.bless(self) if model
28     end
29     
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)
33     end
34     
35   private
36     @@http_header = nil
37     
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"],
43                                    :uri => uri)        
44     end
45     
46     def handle_rest_response(response, uri = nil)
47       unless response.is_a?(Net::HTTPSuccess)
48         raise_rest_error(response, uri)
49       end
50     end
51     
52     def create_http_connection(service = :rest)
53       case service
54       when :rest
55         protocol, host, port = @@config.protocol, @@config.host, @@config.port
56       when :search
57         protocol, host, port = @@config.search_protocol, @@config.search_host, @@config.search_port
58       end
59       conn = Net::HTTP.new(host, port, 
60                             @@config.proxy_host, @@config.proxy_port,
61                             @@config.proxy_user, @@config.proxy_pass)
62       if protocol == :ssl
63         conn.use_ssl = true
64         conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
65       end
66       conn
67     end
69     def http_header
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.
73       @@http_header ||= { 
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,
79       }
80       @@http_header
81     end
82     
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)
86     end
87     
88     def create_http_post_request(uri)
89         Net::HTTP::Post.new(uri, http_header)
90     end
91     
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)
95     end
96 end