Added v 0.3.1 snapshot.
[twitter4r-core.git] / lib / twitter / config.rb
blob79f2cd9c6c77e48c6d868f0d46a90be05b84d427
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   class Config
20     include ClassUtilMixin
21     @@ATTRIBUTES = [
22       :protocol, 
23       :host, 
24       :port, 
25       :search_protocol,
26       :search_host,
27       :search_port,
28       :proxy_host, 
29       :proxy_port, 
30       :proxy_user, 
31       :proxy_pass, 
32       :user_agent,
33       :application_name,
34       :application_version,
35       :application_url,
36       :source,
37     ]
38     attr_accessor *@@ATTRIBUTES
39     
40     # Override of Object#eql? to ensure RSpec specifications run 
41     # correctly. Also done to follow Ruby best practices.
42     def eql?(other)
43       return true if self == other
44       @@ATTRIBUTES.each do |att|
45         return false unless self.send(att).eql?(other.send(att))
46       end
47       true
48     end
49   end
51   class Client
52     @@defaults = { :host => 'twitter.com', 
53                    :port => 443, 
54                    :protocol => :ssl,
55                    :search_host => 'search.twitter.com',
56                    :search_port => 80,
57                    :search_protocol => :http,
58                    :proxy_host => nil,
59                    :proxy_port => nil,
60                    :user_agent => "default",
61                    :application_name => 'Twitter4R',
62                    :application_version => Twitter::Version.to_version,
63                    :application_url => 'http://twitter4r.rubyforge.org',
64                    :source => 'twitter4r',
65     }
66     @@config = Twitter::Config.new(@@defaults)
68     # Twitter::Client class methods
69     class << self
70       # Yields to given <tt>block</tt> to configure the Twitter4R API.
71       def configure(&block)
72         raise ArgumentError, "Block must be provided to configure" unless block_given?
73         yield @@config
74       end # configure
75     end # class << self    
76   end # Client class
77 end # Twitter module