Edited sethco's patch to add timeout option.
[twitter4r-core.git] / lib / twitter / config.rb
blob18181cbd29ba2843cf3f1658a09185afe57c0ed1
1 # config.rb contains classes, methods and extends existing Twitter4R classes 
2 # to provide easy configuration facilities.
4 module Twitter
5   # Represents global configuration for Twitter::Client.
6   # Can override the following configuration options:
7   # * <tt>protocol</tt> - <tt>:http</tt>, <tt>:https</tt> or <tt>:ssl</tt> supported.  <tt>:ssl</tt> is an alias for <tt>:https</tt>.  Defaults to <tt>:ssl</tt>
8   # * <tt>host</tt> - hostname to connect to for the Twitter service.  Defaults to <tt>'twitter.com'</tt>.
9   # * <tt>port</tt> - port to connect to for the Twitter service.  Defaults to <tt>443</tt>.
10   # * <tt>proxy_host</tt> - proxy host to use.  Defaults to nil.
11   # * <tt>proxy_port</tt> - proxy host to use.  Defaults to nil.
12   # * <tt>proxy_user</tt> - proxy username to use.  Defaults to nil.
13   # * <tt>proxy_pass</tt> - proxy password to use.  Defaults to nil.
14   # * <tt>user_agent</tt> - user agent string to use for each request of the HTTP header.
15   # * <tt>application_name</tt> - name of your client application.  Defaults to 'Twitter4R'
16   # * <tt>application_version</tt> - version of your client application.  Defaults to current <tt>Twitter::Version.to_version</tt>.
17   # * <tt>application_url</tt> - URL of your client application.  Defaults to http://twitter4r.rubyforge.org.
18   # * <tt>source</tt> - the source id given to you by Twitter to identify your application in their web interface.  Note: you must contact Twitter.com developer directly so they can configure their servers appropriately.
19   # * <tt>timeout</tt> - the timeout in second for HTTP requests.
20   class Config
21     include ClassUtilMixin
22     @@ATTRIBUTES = [
23       :protocol, 
24       :host, 
25       :port, 
26       :search_protocol,
27       :search_host,
28       :search_port,
29       :proxy_host, 
30       :proxy_port, 
31       :proxy_user, 
32       :proxy_pass, 
33       :user_agent,
34       :application_name,
35       :application_version,
36       :application_url,
37       :source,
38       :timeout,
39     ]
40     attr_accessor *@@ATTRIBUTES
41     
42     # Override of Object#eql? to ensure RSpec specifications run 
43     # correctly. Also done to follow Ruby best practices.
44     def eql?(other)
45       return true if self == other
46       @@ATTRIBUTES.each do |att|
47         return false unless self.send(att).eql?(other.send(att))
48       end
49       true
50     end
51   end
53   class Client
54     @@defaults = { :host => 'twitter.com', 
55                    :port => 443, 
56                    :protocol => :ssl,
57                    :search_host => 'search.twitter.com',
58                    :search_port => 80,
59                    :search_protocol => :http,
60                    :proxy_host => nil,
61                    :proxy_port => nil,
62                    :user_agent => "default",
63                    :application_name => 'Twitter4R',
64                    :application_version => Twitter::Version.to_version,
65                    :application_url => 'http://twitter4r.rubyforge.org',
66                    :source => 'twitter4r',
67                    :timeout => 20,
68     }
69     @@config = Twitter::Config.new(@@defaults)
71     # Twitter::Client class methods
72     class << self
73       # Yields to given <tt>block</tt> to configure the Twitter4R API.
74       def configure(&block)
75         raise ArgumentError, "Block must be provided to configure" unless block_given?
76         yield @@config
77       end # configure
78     end # class << self    
79   end # Client class
80 end # Twitter module