Class: Twterm::Client
- Inherits:
-
Object
- Object
- Twterm::Client
- Defined in:
- lib/twterm/client.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#screen_name ⇒ Object
readonly
Returns the value of attribute screen_name.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Class Method Summary collapse
Instance Method Summary collapse
- #connect_stream ⇒ Object
- #favorite(status) ⇒ Object
- #home_timeline ⇒ Object
-
#initialize(user_id, screen_name, token, secret) ⇒ Client
constructor
A new instance of Client.
- #list(list) ⇒ Object
- #lists ⇒ Object
- #mentions ⇒ Object
- #on_mention(&block) ⇒ Object
- #on_timeline_status(&block) ⇒ Object
- #post(text, in_reply_to = nil) ⇒ Object
- #retweet(status) ⇒ Object
- #search(query) ⇒ Object
- #show_status(status_id) ⇒ Object
- #stream ⇒ Object
- #unfavorite(status) ⇒ Object
- #user_timeline(user_id) ⇒ Object
Constructor Details
#initialize(user_id, screen_name, token, secret) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/twterm/client.rb', line 9 def initialize(user_id, screen_name, token, secret) @user_id = user_id @screen_name = screen_name @rest_client = Twitter::REST::Client.new do |config| config.consumer_key = 'vLNSVFgXclBJQJRZ7VLMxL9lA' config.consumer_secret = 'OFLKzrepRG2p1hq0nUB9j2S9ndFQoNTPheTpmOY0GYw55jGgS5' config.access_token = token config.access_token_secret = secret end TweetStream.configure do |config| config.consumer_key = 'vLNSVFgXclBJQJRZ7VLMxL9lA' config.consumer_secret = 'OFLKzrepRG2p1hq0nUB9j2S9ndFQoNTPheTpmOY0GYw55jGgS5' config.oauth_token = token config.oauth_token_secret = secret config.auth_method = :oauth end @stream_client = TweetStream::Client.new @callbacks = {} @@instances << self end |
Instance Attribute Details
#screen_name ⇒ Object (readonly)
Returns the value of attribute screen_name.
3 4 5 |
# File 'lib/twterm/client.rb', line 3 def screen_name @screen_name end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
3 4 5 |
# File 'lib/twterm/client.rb', line 3 def user_id @user_id end |
Class Method Details
.current ⇒ Object
182 183 184 |
# File 'lib/twterm/client.rb', line 182 def current @@instances[0] end |
.new(user_id, screen_name, token, secret) ⇒ Object
176 177 178 179 180 |
# File 'lib/twterm/client.rb', line 176 def new(user_id, screen_name, token, secret) detector = -> (instance) { instance.user_id == user_id } instance = @@instances.find(&detector) instance.nil? ? super : instance end |
Instance Method Details
#connect_stream ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/twterm/client.rb', line 58 def connect_stream @stream_client.stop_stream @streaming_thread.kill if @streaming_thread.is_a? Thread Notifier.instance. 'Trying to connect to Twitter...' @streaming_thread = Thread.new do begin @stream_client.userstream rescue EventMachine::ConnectionError Notifier.instance.show_error 'Connection failed' sleep 30 retry end Notifier.instance. 'Connection established' end end |
#favorite(status) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/twterm/client.rb', line 129 def favorite(status) return false unless status.is_a? Status send_request do @rest_client.favorite(status.id) status.favorite! yield status if block_given? end self end |
#home_timeline ⇒ Object
86 87 88 89 90 |
# File 'lib/twterm/client.rb', line 86 def home_timeline send_request do yield @rest_client.home_timeline(count: 200).map(&CREATE_STATUS_PROC) end end |
#list(list) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/twterm/client.rb', line 110 def list(list) fail ArgumentError, 'argument must be an instance of List class' unless list.is_a? List send_request do yield @rest_client.list_timeline(list.id, count: 200).map(&CREATE_STATUS_PROC) end end |
#lists ⇒ Object
104 105 106 107 108 |
# File 'lib/twterm/client.rb', line 104 def lists send_request do yield @rest_client.lists.map { |list| List.new(list) } end end |
#mentions ⇒ Object
92 93 94 95 96 |
# File 'lib/twterm/client.rb', line 92 def mentions send_request do yield @rest_client.mentions(count: 200).map(&CREATE_STATUS_PROC) end end |
#on_mention(&block) ⇒ Object
170 171 172 173 |
# File 'lib/twterm/client.rb', line 170 def on_mention(&block) fail ArgumentError, 'no block given' unless block_given? on(:mention, &block) end |
#on_timeline_status(&block) ⇒ Object
165 166 167 168 |
# File 'lib/twterm/client.rb', line 165 def on_timeline_status(&block) fail ArgumentError, 'no block given' unless block_given? on(:timeline_status, &block) end |
#post(text, in_reply_to = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/twterm/client.rb', line 75 def post(text, in_reply_to = nil) send_request do if in_reply_to.is_a? Status text = "@#{in_reply_to.user.screen_name} #{text}" @rest_client.update(text, in_reply_to_status_id: in_reply_to.id) else @rest_client.update(text) end end end |
#retweet(status) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/twterm/client.rb', line 151 def retweet(status) return false unless status.is_a? Status send_request do begin @rest_client.retweet!(status.id) status.retweet! yield status if block_given? rescue Twitter::Error::AlreadyRetweeted, Twitter::Error::NotFound, Twitter::Error::Forbidden Notifier.instance.show_error 'Retweet attempt failed' end end end |
#search(query) ⇒ Object
117 118 119 120 121 |
# File 'lib/twterm/client.rb', line 117 def search(query) send_request do yield @rest_client.search(query, count: 100).map(&CREATE_STATUS_PROC) end end |
#show_status(status_id) ⇒ Object
123 124 125 126 127 |
# File 'lib/twterm/client.rb', line 123 def show_status(status_id) send_request do yield Status.new(@rest_client.status(status_id)) end end |
#stream ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/twterm/client.rb', line 34 def stream @stream_client.on_timeline_status do |tweet| status = Status.new(tweet) invoke_callbacks(:timeline_status, status) invoke_callbacks(:mention, status) if status.text.include? "@#{@screen_name}" end @stream_client.on_delete do |status_id| timeline.delete_status(status_id) end @stream_client.on_event(:favorite) do |event| break if event[:source][:screen_name] == @screen_name = "@#{event[:source][:screen_name]} has favorited your tweet: #{event[:target_object][:text]}" Notifier.instance.() end @stream_client.on_no_data_received do connect_stream end connect_stream end |
#unfavorite(status) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/twterm/client.rb', line 141 def unfavorite(status) fail ArgumentError, 'argument must be an instance of Status class' unless status.is_a? Status send_request do @rest_client.unfavorite(status.id) status.unfavorite! yield status if block_given? end end |
#user_timeline(user_id) ⇒ Object
98 99 100 101 102 |
# File 'lib/twterm/client.rb', line 98 def user_timeline(user_id) send_request do yield @rest_client.user_timeline(user_id, count: 200).map(&CREATE_STATUS_PROC) end end |