Added v 0.2.2 snapshot.
[twitter4r-core.git] / lib / twitter / client / base.rb
blob279f63030eb5d838c68cb6fef97251578c99b531
1 class Twitter::Client
2   protected
3     attr_accessor :login, :password
4     
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)
14                 response
15       end
16     end
17     
18     # "Blesses" model object with client information
19     def bless_model(model)
20         model.bless(self) if model
21     end
22     
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)
26     end
27     
28   private
29     @@http_header = nil
30     
31     def raise_rest_error(response, uri = nil)
32       raise Twitter::RESTError.new(:code => response.code, 
33                                    :message => response.message,
34                                    :uri => uri)        
35     end
36     
37     def handle_rest_response(response, uri = nil)
38       unless response.is_a?(Net::HTTPSuccess)
39         raise_rest_error(response, uri)
40       end
41     end
42     
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
48         conn.use_ssl = true
49         conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
50       end
51       conn
52     end
54     def http_header
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.
58       @@http_header ||= { 
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,
64       }
65       @@http_header
66     end
67     
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)
71     end
72     
73     def create_http_post_request(uri)
74         Net::HTTP::Post.new(uri, http_header)
75     end
76     
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)
80     end
81 end