Class: Twtr::Client
- Inherits:
-
Object
- Object
- Twtr::Client
- Defined in:
- lib/twtr/client.rb
Constant Summary collapse
- HOST =
"twitter.com"
- PORT =
80
- DEFAULT_FORMAT =
:xml
- DEFAULT_METHOD =
:get
- SEC_SLEEP =
wait update.
1
- ATTR_NAMES =
[:account_user, :account_password, :proxy_host, :proxy_port]
Instance Method Summary collapse
- #config_from_attr(attr_name, config) ⇒ Object
- #friends(options = {}) ⇒ Object
- #friends_timeline(options = {}) ⇒ Object
- #get(path, params = {}) ⇒ Object
- #get_status(path, options = {}) ⇒ Object
-
#initialize(config = nil) ⇒ Client
constructor
A new instance of Client.
- #load_config(attr_name, config) ⇒ Object
- #post(path, params = {}) ⇒ Object
- #post_status(path, options = {}) ⇒ Object
- #public_timeline(options = {}) ⇒ Object
- #rate_limit_status(options = {}) ⇒ Object
- #replies(options = {}) ⇒ Object
- #request(req) ⇒ Object
- #request_status(path, options = {}) ⇒ Object
- #test(options = {}) ⇒ Object
- #update(options = {}) ⇒ Object
- #update_location(options = {}) ⇒ Object
- #user_timeline(options = {}) ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ Client
Returns a new instance of Client.
18 19 20 21 |
# File 'lib/twtr/client.rb', line 18 def initialize(config = nil) @account_user = @account_password = @proxy_host = @proxy_port = nil ATTR_NAMES.each {|name| load_config(name, config) } if config end |
Instance Method Details
#config_from_attr(attr_name, config) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/twtr/client.rb', line 29 def config_from_attr(attr_name, config) section, name = attr_name.to_s.split("_") val ||= (config[section] && config[section][name]) val ||= (config[section.to_sym] && config[section.to_sym][name.to_sym]) val end |
#friends(options = {}) ⇒ Object
49 50 51 52 53 |
# File 'lib/twtr/client.rb', line 49 def friends(={}) [:method] = :get id = .delete(:id) || @account_user request_status("/statuses/friends/#{id}", ) end |
#friends_timeline(options = {}) ⇒ Object
40 41 42 |
# File 'lib/twtr/client.rb', line 40 def friends_timeline(={}) get_status("/statuses/friends_timeline", ) end |
#get(path, params = {}) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/twtr/client.rb', line 103 def get(path, params = {}) query = params.to_a.map {|pair| "#{pair.shift}=#{CGI.escape(pair.pop.to_s)}" if pair.last} path = path.gsub(/^#{HOST}/,'') path += "?#{query.join('&')}" unless query.empty? req = Net::HTTP::Get.new(path) request(req) end |
#get_status(path, options = {}) ⇒ Object
83 84 85 86 |
# File 'lib/twtr/client.rb', line 83 def get_status(path, = {}) [:method] = :get request_status(path, ) end |
#load_config(attr_name, config) ⇒ Object
23 24 25 26 27 |
# File 'lib/twtr/client.rb', line 23 def load_config(attr_name, config) if val = config_from_attr(attr_name, config) self.instance_variable_set("@#{attr_name}", val ) end end |
#post(path, params = {}) ⇒ Object
111 112 113 114 115 |
# File 'lib/twtr/client.rb', line 111 def post(path, params = {}) req = Net::HTTP::Post.new(path) req.set_form_data(params) unless params.empty? request(req) end |
#post_status(path, options = {}) ⇒ Object
77 78 79 80 81 |
# File 'lib/twtr/client.rb', line 77 def post_status(path, = {}) [:method] = :post sleep(SEC_SLEEP) request_status(path, ) end |
#public_timeline(options = {}) ⇒ Object
36 37 38 |
# File 'lib/twtr/client.rb', line 36 def public_timeline(={}) get_status("/statuses/public_timeline", ) end |
#rate_limit_status(options = {}) ⇒ Object
69 70 71 |
# File 'lib/twtr/client.rb', line 69 def rate_limit_status( = {}) request_status("/account/rate_limit_status", ) end |
#replies(options = {}) ⇒ Object
55 56 57 |
# File 'lib/twtr/client.rb', line 55 def replies(={}) get_status("/statuses/replies", ) end |
#request(req) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/twtr/client.rb', line 117 def request(req) req.basic_auth(@account_user, @account_password) if @account_user if(@proxy_host) @proxy_host.gsub!(%r|http://|,'') Net::HTTP::Proxy(@proxy_host, @proxy_port.to_i).start(HOST, PORT) do |http| http.request(req) end else Net::HTTP.start(HOST, PORT){|http| http.request(req)} end end |
#request_status(path, options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/twtr/client.rb', line 88 def request_status(path, = {}) format = .delete(:format) || DEFAULT_FORMAT method = .delete(:method) || DEFAULT_METHOD case method when :get res = get("#{path}.#{format}", ) when :post res = post("#{path}.#{format}", ) end raise Twtr::ResponseErrorException.new("#{res.code}: #{HTTP_ERRORS[res.code]}") \ if Twtr::HTTP_ERRORS.has_key?(res.code) res.body end |
#test(options = {}) ⇒ Object
73 74 75 |
# File 'lib/twtr/client.rb', line 73 def test(={}) request_status("/help/test", ) end |
#update(options = {}) ⇒ Object
59 60 61 62 |
# File 'lib/twtr/client.rb', line 59 def update(={}) source = post_status("/statuses/update",) friends_timeline() end |
#update_location(options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/twtr/client.rb', line 64 def update_location( = {}) [:method] = :post request_status("/account/update_location", ) end |
#user_timeline(options = {}) ⇒ Object
44 45 46 47 |
# File 'lib/twtr/client.rb', line 44 def user_timeline(={}) id = .delete(:id) || @account_user get_status("/statuses/user_timeline/#{id}", ) end |