Class: Twtr::Client

Inherits:
Object
  • Object
show all
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

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(options={})
  options[:method] = :get
  id = options.delete(:id) || @account_user
  request_status("/statuses/friends/#{id}", options)
end

#friends_timeline(options = {}) ⇒ Object



40
41
42
# File 'lib/twtr/client.rb', line 40

def friends_timeline(options={})
  get_status("/statuses/friends_timeline", options)
end

#get(path, params = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/twtr/client.rb', line 99

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



79
80
81
82
# File 'lib/twtr/client.rb', line 79

def get_status(path, options = {})
  options[:method] = :get
  request_status(path, options)
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



107
108
109
110
111
# File 'lib/twtr/client.rb', line 107

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



73
74
75
76
77
# File 'lib/twtr/client.rb', line 73

def post_status(path,options = {})
  options[:method] = :post
  sleep(SEC_SLEEP)
  request_status(path, options)
end

#public_timeline(options = {}) ⇒ Object



36
37
38
# File 'lib/twtr/client.rb', line 36

def public_timeline(options={})
  get_status("/statuses/public_timeline", options)
end

#replies(options = {}) ⇒ Object



55
56
57
# File 'lib/twtr/client.rb', line 55

def replies(options={})
  get_status("/statuses/replies", options)
end

#request(req) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/twtr/client.rb', line 113

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/twtr/client.rb', line 84

def request_status(path, options = {})
  format = options.delete(:format) || DEFAULT_FORMAT
  method = options.delete(:method) || DEFAULT_METHOD
  case method
  when :get
    res = get("#{path}.#{format}", options)
  when :post
    res = post("#{path}.#{format}", options)
  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



69
70
71
# File 'lib/twtr/client.rb', line 69

def test(options={})
  request_status("/help/test", options)
end

#update(options = {}) ⇒ Object



59
60
61
62
# File 'lib/twtr/client.rb', line 59

def update(options={})
  source = post_status("/statuses/update",options)
  friends_timeline()
end

#update_location(options = {}) ⇒ Object



64
65
66
67
# File 'lib/twtr/client.rb', line 64

def update_location(options = {})
  options[:method] = :post
  request_status("/account/update_location", options)
end

#user_timeline(options = {}) ⇒ Object



44
45
46
47
# File 'lib/twtr/client.rb', line 44

def user_timeline(options={})
  id = options.delete(:id) || @account_user
  get_status("/statuses/user_timeline/#{id}", options)
end