Class: Twterm::Client
- Inherits:
-
Object
- Object
- Twterm::Client
- Defined in:
- lib/twterm/client.rb
Constant Summary collapse
- CREATE_STATUS_PROC =
-> (s) { Status.new(s) }
- CONSUMER_KEY =
'vLNSVFgXclBJQJRZ7VLMxL9lA'.freeze
- CONSUMER_SECRET =
'OFLKzrepRG2p1hq0nUB9j2S9ndFQoNTPheTpmOY0GYw55jGgS5'.freeze
- @@instances =
[]
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
- #destroy_status(status) ⇒ Object
- #favorite(status) ⇒ Object
- #fetch_muted_users ⇒ Object
- #home_timeline ⇒ Object
-
#initialize(user_id, screen_name, access_token, access_token_secret) ⇒ Client
constructor
A new instance of Client.
- #list(list_id) ⇒ Object
- #list_timeline(list) ⇒ Object
- #lists ⇒ Object
- #mentions ⇒ Object
- #on_mention(&block) ⇒ Object
- #on_timeline_status(&block) ⇒ Object
- #post(text, in_reply_to = nil) ⇒ Object
- #rest_client ⇒ Object
- #retweet(status) ⇒ Object
- #saved_search ⇒ Object
- #search(query) ⇒ Object
- #show_status(status_id) ⇒ Object
- #show_user(query) ⇒ Object
- #stream ⇒ Object
- #stream_client ⇒ Object
- #unfavorite(status) ⇒ Object
- #user_timeline(user_id) ⇒ Object
Constructor Details
#initialize(user_id, screen_name, access_token, access_token_secret) ⇒ Client
Returns a new instance of Client.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/twterm/client.rb', line 66 def initialize(user_id, screen_name, access_token, access_token_secret) @user_id, @screen_name = user_id, screen_name @access_token, @access_token_secret = access_token, access_token_secret @callbacks = {} @mute_filter = -> _ { true } fetch_muted_users do |muted_user_ids| @mute_filter = lambda do |status| !muted_user_ids.include?(status.user.id) && !(status.retweeted_status.is_a?(Twitter::NullObject) && muted_user_ids.include?(status.retweeted_status.user.id)) end end @@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
282 283 284 |
# File 'lib/twterm/client.rb', line 282 def self.current @@instances[0] end |
.new(user_id, screen_name, token, secret) ⇒ Object
276 277 278 279 280 |
# File 'lib/twterm/client.rb', line 276 def self.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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/twterm/client.rb', line 11 def connect_stream stream_client.stop_stream @streaming_thread = Thread.new do begin Notifier.instance. 'Trying to connect to Twitter...' stream_client.userstream rescue EventMachine::ConnectionError Notifier.instance.show_error 'Connection failed' sleep 30 retry end end end |
#destroy_status(status) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/twterm/client.rb', line 26 def destroy_status(status) send_request do begin rest_client.destroy_status(status.id) yield if block_given? rescue Twitter::Error::NotFound, Twitter::Error::Forbidden Notifier.instance.show_error 'You cannot destroy that status' end end end |
#favorite(status) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/twterm/client.rb', line 37 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 |
#fetch_muted_users ⇒ Object
49 50 51 52 53 54 |
# File 'lib/twterm/client.rb', line 49 def fetch_muted_users send_request do @muted_user_ids = rest_client.muted_ids.to_a yield @muted_user_ids if block_given? end end |
#home_timeline ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/twterm/client.rb', line 56 def home_timeline send_request do statuses = rest_client .home_timeline(count: 100) .select(&@mute_filter) .map(&CREATE_STATUS_PROC) yield statuses end end |
#list(list_id) ⇒ Object
84 85 86 87 88 |
# File 'lib/twterm/client.rb', line 84 def list(list_id) send_request do yield List.new(rest_client.list(list_id)) end end |
#list_timeline(list) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/twterm/client.rb', line 90 def list_timeline(list) fail ArgumentError, 'argument must be an instance of List class' unless list.is_a? List send_request do statuses = rest_client .list_timeline(list.id, count: 100) .select(&@mute_filter) .map(&CREATE_STATUS_PROC) yield statuses end end |
#lists ⇒ Object
102 103 104 105 106 |
# File 'lib/twterm/client.rb', line 102 def lists send_request do yield rest_client.lists.map { |list| List.new(list) } end end |
#mentions ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/twterm/client.rb', line 108 def mentions send_request do statuses = rest_client .mentions(count: 100) .select(&@mute_filter) .map(&CREATE_STATUS_PROC) yield statuses end end |
#on_mention(&block) ⇒ Object
118 119 120 121 |
# File 'lib/twterm/client.rb', line 118 def on_mention(&block) fail ArgumentError, 'no block given' unless block_given? on(:mention, &block) end |
#on_timeline_status(&block) ⇒ Object
123 124 125 126 |
# File 'lib/twterm/client.rb', line 123 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
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/twterm/client.rb', line 128 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 |
#rest_client ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/twterm/client.rb', line 139 def rest_client @rest_client ||= Twitter::REST::Client.new do |config| config.consumer_key = CONSUMER_KEY config.consumer_secret = CONSUMER_SECRET config.access_token = @access_token config.access_token_secret = @access_token_secret end end |
#retweet(status) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/twterm/client.rb', line 148 def retweet(status) fail ArgumentError, 'argument must be an instance of Status class' unless status.is_a? Status send_request do begin rest_client.retweet!(status.id) status.retweet! yield status if block_given? rescue => e = case e when Twitter::Error::AlreadyRetweeted 'The status is already retweeted' when Twitter::Error::NotFound 'The status is not found' when Twitter::Error::Forbidden if status.user.id == user_id # when the status is mine 'You cannot retweet your own status' else # when the status is not mine 'The status is protected' end else raise e end Notifier.instance.show_error "Retweet attempt failed: #{}" end end end |
#saved_search ⇒ Object
178 179 180 181 182 |
# File 'lib/twterm/client.rb', line 178 def saved_search send_request do yield rest_client.saved_searches end end |
#search(query) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/twterm/client.rb', line 184 def search(query) send_request do statuses = rest_client .search(query, count: 100) .select(&@mute_filter) .map(&CREATE_STATUS_PROC) yield statuses end end |
#show_status(status_id) ⇒ Object
194 195 196 197 198 |
# File 'lib/twterm/client.rb', line 194 def show_status(status_id) send_request do yield Status.new(rest_client.status(status_id)) end end |
#show_user(query) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/twterm/client.rb', line 200 def show_user(query) send_request do user = begin User.new(rest_client.user(query)) rescue Twitter::Error::NotFound nil end yield user end end |
#stream ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/twterm/client.rb', line 212 def stream stream_client.on_friends do Notifier.instance. 'Connection established' unless @stream_connected @stream_connected = true end 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 user = event[:source][:screen_name] text = event[:target_object][:text] = "@#{user} has favorited your tweet: #{text}" Notifier.instance.() end stream_client.on_no_data_received do @stream_connected = false connect_stream end connect_stream end |
#stream_client ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/twterm/client.rb', line 245 def stream_client @stream_client ||= TweetStream::Client.new( consumer_key: CONSUMER_KEY, consumer_secret: CONSUMER_SECRET, oauth_token: @access_token, oauth_token_secret: @access_token_secret, auth_method: :oauth ) end |
#unfavorite(status) ⇒ Object
255 256 257 258 259 260 261 262 263 264 |
# File 'lib/twterm/client.rb', line 255 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
266 267 268 269 270 271 272 273 274 |
# File 'lib/twterm/client.rb', line 266 def user_timeline(user_id) send_request do statuses = rest_client .user_timeline(user_id, count: 100) .select(&@mute_filter) .map(&CREATE_STATUS_PROC) yield statuses end end |