Twitter4R v0.2.0: Configuration API

Written on 2:48:00 PM by S. Potter

The Configuration API allows applications using Twitter4R configure static connection options such as:

  • connection protocol (e.g. HTTP and SSL)
  • host name and port (good for testing or if the Twitter REST API moved to a different subdomain)
  • proxy settings (i.e. proxy host, port, username and password)
  • user agent identifier
  • X-Twitter-Client* HTTP headers for Twitter's internal usage analysis
This API intentionally tries to look like the Rails initializer/configuration API:

require('rubygems')
gem('twitter4r', '0.2.0')
require('twitter')

Twitter::Client.configure do |conf|
  # We can set Twitter4R to use :ssl or :http to connect to the Twitter API.
  # Defaults to :ssl
  conf.protocol = :ssl

  # We can set Twitter4R to use another host name (perhaps for internal
  # testing purposes).
  # Defaults to 'twitter.com'
  conf.host = 'twitter.com'

  # We can set Twitter4R to use another port (also for internal
  # testing purposes).
  # Defaults to 443
  conf.port = 443

  # We can set proxy information for Twitter4R
  # By default all following values are set to nil.
  conf.proxy_host = 'myproxy.host'
  conf.proxy_port = 8080
  conf.proxy_user = 'myuser'
  conf.proxy_pass = 'mypass'

  # We can also change the User-Agent and X-Twitter-Client* HTTP headers
  conf.user_agent = 'MyAppAgentName'
  conf.application_name = 'MyAppName'
  conf.application_version = 'v1.5.6'
  conf.application_url = 'http://myapp.url'
end
If using Twitter4R (open source Ruby library for Twitter REST API) in a Rails application I would recommend adding the necessary Twitter4R configuration code in config/twitter.rb then including it in the config/environment.rb file to keep configurations separate.

Upgrading from 0.1.1

In Twitter4R v0.1.1 you could configure protocol, server host, server port and proxy settings using the following code:
Twitter::Client.conf(
  :ssl => true, 
  :port => 443, 
  :proxy_host => 'myproxy.host', 
  :proxy_port => 8080)
This API no longer exists, so if upgrading to Twitter4R 0.1.1 you will need to change the above to something like the following:
Twitter::Client.configure do |conf|
  conf.protocol = :ssl
  conf.port = 443
  conf.proxy_host = 'myproxy.host'
  conf.proxy_port = 8080
end
This change shouldn't be too painful for people I hope. Please refer to the Twitter4R v0.2.0 RDoc for more information.

If you enjoyed this post Subscribe to our feed

No Comment

Post a Comment