Class: EventMachine::TwitterClient

Inherits:
Object
  • Object
show all
Defined in:
lib/em-twitter-client/twitter_client.rb

Overview

Takes the following hash => ”,

:consumer_secret  => '',
:access_token     => '',
:access_token_secret => ''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_config) ⇒ TwitterClient

Returns a new instance of TwitterClient.



13
14
15
16
17
18
# File 'lib/em-twitter-client/twitter_client.rb', line 13

def initialize(oauth_config)
  EventMachine::HttpRequest.use EventMachine::Middleware::JSONResponse
  @host = {:api => 'http://api.twitter.com', :search => 'http://search.twitter.com'}
  @oauth = oauth_config
  @errback = Proc.new {|data| puts data.inspect}
end

Instance Attribute Details

#errbackObject

Returns the value of attribute errback.



11
12
13
# File 'lib/em-twitter-client/twitter_client.rb', line 11

def errback
  @errback
end

Instance Method Details

#home_timeline(pages = 1, &blk) ⇒ Object

these are private for the user. as in only available for that user.



71
72
73
74
75
76
77
78
79
80
# File 'lib/em-twitter-client/twitter_client.rb', line 71

def home_timeline(pages = 1, &blk)
  multi = EventMachine::Synchrony::Multi.new
  Array(1..pages).each do |page|
    conn = connection("1/statuses/home_timeline.json?page=#{page}&count=200")
    multi.add(page, conn.aget)
    puts "Adding 1/statuses/home_timeline.json?page=#{page}"
  end
  process_paged_responses multi.perform, blk
  self
end

#mentions(pages = 1, &blk) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/em-twitter-client/twitter_client.rb', line 82

def mentions(pages = 1, &blk)
  multi = EventMachine::Synchrony::Multi.new
  Array(1..pages).each do |page|
    conn = connection("1/statuses/mentions.json?page=#{page}&count=200")
    multi.add(page, conn.aget)
    puts "Adding 1/statuses/mentions.json?page=#{page}"
  end
  process_paged_responses multi.perform, blk
  self
end

#rate_limit(&blk) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/em-twitter-client/twitter_client.rb', line 20

def rate_limit(&blk)
  http = connection("1/account/rate_limit_status.json").aget
  http.callback do 
     @current_ratelimit = http.response
     puts http.response.inspect
     blk(@current_ratelimit) if block_given?
  end
end

#search(text, pages = 1, search_opts = {}, &blk) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/em-twitter-client/twitter_client.rb', line 37

def search( text, pages = 1 ,search_opts = {}, &blk)
  multi = EventMachine::Synchrony::Multi.new
  Array(1..pages).each do |page|
    conn = connection("search.json?q=#{URI::escape(text)}&page=#{page}", :search)
    multi.add(page, conn.aget)
    puts "q=#{URI::escape(text)}&page=#{page}"
  end
  
  process_paged_responses multi.perform, blk
end

#user_friends(user, pages = 1, &blk) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/em-twitter-client/twitter_client.rb', line 48

def user_friends(user, pages = 1, &blk)
  multi = EventMachine::Synchrony::Multi.new
  Array(1..pages).each do |page|
    conn = connection("1/statuses/friends.json?screen_name=#{user}&page=#{page}")
    multi.add(page, conn.aget)
    puts "Adding 1/statuses/friends.json?screen_name=#{user}&page=#{page}"
  end
  
  process_paged_responses multi.perform, blk
  self
end

#user_info(user, &blk) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/em-twitter-client/twitter_client.rb', line 29

def (user, &blk)
  path = "1/users/show.json?screen_name=#{user.strip}"
  http = connection(path).aget
  http.callback { yield(http.response) if block_given?}
  http.errback { @errback.call(http.response) } 
  self
end

#user_timeline(user, pages = 1, &blk) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/em-twitter-client/twitter_client.rb', line 60

def user_timeline(user, pages = 1, &blk)
  multi = EventMachine::Synchrony::Multi.new
  Array(1..pages).each do |page|
    conn = connection("1/statuses/user_timeline.json?screen_name=#{user}&page=#{page}&count=200")
    multi.add(page, conn.aget)
  end
  process_paged_responses multi.perform, blk
  self
end