Class: TwitRuby
- Inherits:
-
Object
- Object
- TwitRuby
- Defined in:
- lib/twitruby.rb,
lib/twitruby/version.rb
Constant Summary collapse
- VERSION =
"0.0.121"
Instance Method Summary collapse
-
#dm_msgs(since_id = "", max_id = "", count = "", include_entities = "", skip_status = "") ⇒ Object
GET direct_messages.
-
#dm_send(user_id = "", screen_name = "", text) ⇒ Object
POST direct_messages/new.
-
#favorite(id) ⇒ Object
POST favorites/create.
-
#follow(screen_name = "", user_id = "") ⇒ Object
POST friendships/create.
-
#follower_ids(user_id = "", screen_name = "", cursor = "", stringify_ids = "", count = "") ⇒ Object
GET followers/ids.
-
#friendships?(source_id = "", source_screen_name = "", target_id = "", target_screen_name = "") ⇒ Boolean
GET friendships/show.
-
#gen_parms(api, parms) ⇒ Object
Generate api parameters function.
-
#get_rate_limit ⇒ Object
GET account/rate_limit_status.
-
#get_rom(count, since_id = "", max_id = "", trim_user = "", include_entities = "", include_user_entities = "") ⇒ Object
GET statuses/retweets_of_me.
-
#get_sent_dm(since_id = "", max_id = "", count = "", page = "", include_entities = "") ⇒ Object
GET direct_messages/sent.
-
#home_timeline(count, since_id = "", max_id = "", trim_user = "", include_entities = "", include_user_entities = "") ⇒ Object
GET statuses/home_timeline.
- #initalize_connection(consumer_keys) ⇒ Object
-
#mentions_timeline(count = "", since_id = "", max_id = "", trim_user = "", contributor_details = "", include_entities = "") ⇒ Object
GET statuses/mentions_timeline.
-
#oauth_init ⇒ Object
end of function.
-
#post_delete(id) ⇒ Object
POST statuses/destroy.
-
#post_dm_destory(id, include_entities = "") ⇒ Object
POST direct_messages/destroy.
-
#public_sample(&block) ⇒ Object
Public Sample.
-
#remove(screen_name = "", user_id = "") ⇒ Object
POST friendships/destroy.
-
#retweet(id) ⇒ Object
POST statuses/retweet.
-
#unfavorite(id) ⇒ Object
POST favorites/destroy.
-
#update(str, id = "") ⇒ Object
POST statuses/update.
-
#user_ava?(user_name) ⇒ Boolean
GET username_available? 存在確認なので真偽値は逆となる trueなら使用可(IDとして新規作成時に).
-
#user_stream(&block) ⇒ Object
UserStream.
-
#user_timeline(user_id = "", screen_name = "", since_id = "", count = "", max_id = "", trim_user = "", exclude_replies = "", contributor_details = "", include_rts = "") ⇒ Object
GET statuses/user_timeline.
-
#verify_credentials ⇒ Object
GET account/verify_credentials.
Instance Method Details
#dm_msgs(since_id = "", max_id = "", count = "", include_entities = "", skip_status = "") ⇒ Object
GET direct_messages
237 238 239 240 241 242 243 244 245 |
# File 'lib/twitruby.rb', line 237 def dm_msgs(since_id="",max_id="",count="",include_entities="",skip_status="") return JSON.parse(@access_token.get("/1.1/direct_messages.json", "since_id" => since_id, "max_id" => max_id, "count" => count, "include_entities" => include_entities, "skip_status" => skip_status ).body) end |
#dm_send(user_id = "", screen_name = "", text) ⇒ Object
POST direct_messages/new
211 212 213 214 215 216 217 218 219 |
# File 'lib/twitruby.rb', line 211 def dm_send(user_id="",screen_name="",text) if user_id.empty? then return "Error. user_id => empty" end @access_token.get("/1.1/direct_messages/new.json", "user_id" => user_id, "screen_name" => screen_name, "text" => text) end |
#favorite(id) ⇒ Object
POST favorites/create
116 117 118 119 |
# File 'lib/twitruby.rb', line 116 def favorite(id) @access_token.post("/1.1/favorites/create.json", "id" => id.to_s) end |
#follow(screen_name = "", user_id = "") ⇒ Object
POST friendships/create
143 144 145 146 147 148 |
# File 'lib/twitruby.rb', line 143 def follow(screen_name="",user_id="") @access_token.post("/1.1/friendships/create.json", "screen_name" => screen_name, "user_id" => user_id, "follow" => true) end |
#follower_ids(user_id = "", screen_name = "", cursor = "", stringify_ids = "", count = "") ⇒ Object
GET followers/ids
257 258 259 260 261 262 263 264 265 |
# File 'lib/twitruby.rb', line 257 def follower_ids(user_id="",screen_name="",cursor="",stringify_ids="",count="") return JSON.parse(@access_token.get("/1.1/followers/ids.json", "user_id" => user_id, "screen_name" => screen_name, "cursor" => cursor, "stringify_ids" => stringify_ids, "count" => count ).body) end |
#friendships?(source_id = "", source_screen_name = "", target_id = "", target_screen_name = "") ⇒ Boolean
GET friendships/show
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/twitruby.rb', line 293 def friendships?(source_id="",source_screen_name="",target_id="",target_screen_name="") #Convert and Build if source_id.empty? == true && source_screen_name.empty? == false source_ = "source_screen_name" source=source_screen_name elsif source_id.empty? == false && source_screen_name.empty? == true source_ = "source_id" source = source_id else return "ERROR" end if target_id.empty? ==true && target_screen_name.empty? == false target_ = "target_screen_name" target = target_screen_name elsif target_id.empty? ==false && target_screen_name.empty? == true target_ = "target_id" target = target_id else return "ERROR" end return JSON.parse(@access_token.get("/1.1/friendships/show.json?#{source_}=#{source}&#{target_}=#{target}").body) end |
#gen_parms(api, parms) ⇒ Object
Generate api parameters function
93 94 95 96 97 98 99 100 |
# File 'lib/twitruby.rb', line 93 def gen_parms(api,parms) hash_str="?" parms.each{|key,value| hash_str+=key.to_s+"="+value.to_s+"&" } hash_str[hash_str.size-1]="" return hash_str.chomp end |
#get_rate_limit ⇒ Object
GET account/rate_limit_status
288 289 290 |
# File 'lib/twitruby.rb', line 288 def get_rate_limit return JSON.parse((@access_token.get("/1.1/application/rate_limit_status.json")).body) end |
#get_rom(count, since_id = "", max_id = "", trim_user = "", include_entities = "", include_user_entities = "") ⇒ Object
GET statuses/retweets_of_me
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/twitruby.rb', line 197 def get_rom(count,since_id="",max_id="",trim_user="",include_entities="",include_user_entities="") return JSON.parse(@access_token.get("/1.1/statuses/retweets_of_me.json", "count" =>count, "since_id" => since_id, "max_id" => max_id, "trim_user" => trim_user, "include_entities" => include_entities, "include_user_entities" => include_user_entities ).body) end |
#get_sent_dm(since_id = "", max_id = "", count = "", page = "", include_entities = "") ⇒ Object
GET direct_messages/sent
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/twitruby.rb', line 222 def get_sent_dm(since_id="",max_id="",count="",page="",include_entities="") if since_id.empty? || max_id.empty? || count.empty? || page.empty? || include_entities.empty? then return "Error. same parms is empty" end return JSON.parse(@access_token.get("/1.1/direct_messages/sent.json", "since_id" => since_id, "max_id" => max_id, "count" => count, "page" => page, "include_entities" => include_entities, ).body) end |
#home_timeline(count, since_id = "", max_id = "", trim_user = "", include_entities = "", include_user_entities = "") ⇒ Object
GET statuses/home_timeline
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/twitruby.rb', line 170 def home_timeline(count,since_id="",max_id="",trim_user="",include_entities="",include_user_entities="") return JSON.parse(@access_token.get("/1.1/statuses/home_timeline.json", "count" =>count, "since_id" => since_id, "max_id" => max_id, "trim_user" => trim_user, "include_entities" => include_entities, "include_user_entities" => include_user_entities ).body) end |
#initalize_connection(consumer_keys) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/twitruby.rb', line 26 def initalize_connection(consumer_keys) ak_exist=true #デフォ状態での引数にアクセストークン系があるかないか 初期になかった場合は処理中にfalseに変更 if consumer_keys.size !=4 then puts "error" puts "wrong number of arguments" puts "arguments array is require 4 element" exit end consumer_key = consumer_keys[0] consumer_secret = consumer_keys[1] access_token = consumer_keys[2] access_token_secret = consumer_keys[3] @consumer = OAuth::Consumer.new( consumer_key, consumer_secret, :site => "http://api.twitter.com/" ) if access_token == nil || access_token == "" || access_token_secret == nil || access_token_secret == "" puts "アクセストークンが設定されていないため、アクセストークンを取得します" oauth_array = oauth_init access_token = oauth_array[0] access_token_secret = oauth_array[1] ak_exist = false end #puts access_token #puts access_token_secret @access_token = OAuth::AccessToken.new( @consumer, access_token, access_token_secret ) end |
#mentions_timeline(count = "", since_id = "", max_id = "", trim_user = "", contributor_details = "", include_entities = "") ⇒ Object
GET statuses/mentions_timeline
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/twitruby.rb', line 158 def mentions_timeline(count="",since_id="",max_id="",trim_user="",contributor_details="",include_entities="") return JSON.parse((@access_token.get("/1.1/statuses/mentions_timeline.json", "count" => count, "since_id" => since_id, "max_id" => max_id, "trim_user" => trim_user, "contributor_details" =>contributor_details, "include_entities" =>include_entities) ).body) end |
#oauth_init ⇒ Object
end of function
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/twitruby.rb', line 64 def oauth_init begin request_token = @consumer.get_request_token # require "Win32API" # shellexecute = Win32API.new("shell32.dll","ShellExecuteA",%w(p p p p p i),"i") # shellexecute.call(0, "open", "#{request_token.authorize_url}", 0, 0, 1) puts("Access here: #{request_token.}\nand...") print("Please input pin:=>") pin = STDIN.gets.chomp puts ""#改行 access_token = request_token.get_access_token( :oauth_token => request_token.token, :oauth_verifier => pin ) access_tokens = [] access_tokens << access_token.token.to_s access_tokens << access_token.secret.to_s return access_tokens end#end of begin end |
#post_delete(id) ⇒ Object
POST statuses/destroy
133 134 135 |
# File 'lib/twitruby.rb', line 133 def post_delete(id) @access_token.post("/1.1/statuses/destroy/#{id}.json") end |
#post_dm_destory(id, include_entities = "") ⇒ Object
POST direct_messages/destroy
248 249 250 251 252 |
# File 'lib/twitruby.rb', line 248 def post_dm_destory(id,include_entities="") @access_token.post("/1.1/direct_messages/destroy.json", "id" => id, "include_entities" => include_entities) end |
#public_sample(&block) ⇒ Object
Public Sample
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/twitruby.rb', line 353 def public_sample(&block) uri = URI.parse("https://stream.twitter.com/1.1/statuses/sample.json") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.start do |https| request = Net::HTTP::Get.new(uri.request_uri+"track=twitter") request.oauth!(https, @consumer, @access_token) buf = "" https.request(request) do |response| response.read_body do |chunk| buf << chunk while(line = buf[/.*(\r\n)+/m]) begin buf.sub!(line,"") line.strip! status = JSON.parse(line) rescue break end yield status end end end end end |
#remove(screen_name = "", user_id = "") ⇒ Object
POST friendships/destroy
151 152 153 154 155 |
# File 'lib/twitruby.rb', line 151 def remove(screen_name="",user_id="") @access_token.post("/1.1/friendships/destroy.json", "screen_name" => screen_name, "user_id" => user_id) end |
#retweet(id) ⇒ Object
POST statuses/retweet
128 129 130 |
# File 'lib/twitruby.rb', line 128 def retweet(id) @access_token.post("/1.1/statuses/retweet/#{id}.json") end |
#unfavorite(id) ⇒ Object
POST favorites/destroy
122 123 124 125 |
# File 'lib/twitruby.rb', line 122 def unfavorite(id) @access_token.post("/1.1/favorites/destroy.json", "id" => id.to_s) end |
#update(str, id = "") ⇒ Object
POST statuses/update
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/twitruby.rb', line 104 def update(str, id="") if (id.empty?) then @access_token.post("/1.1/statuses/update.json", "status" => str) else p @access_token.post("/1.1/statuses/update.json", "status" => str, "in_reply_to_status_id" => id) end end |
#user_ava?(user_name) ⇒ Boolean
GET username_available? 存在確認なので真偽値は逆となる trueなら使用可(IDとして新規作成時に)
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/twitruby.rb', line 269 def user_ava?(user_name) #for https https = Net::HTTP.new("twitter.com",443) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE #get and parse get_torf = JSON.parse(https.get("/users/username_available", "user_name" => user_name).body)["valid"] if get_torf == false return true#存在する elsif get_torf == true return false#存在しない end end |
#user_stream(&block) ⇒ Object
UserStream
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/twitruby.rb', line 324 def user_stream(&block) uri = URI.parse("https://userstream.twitter.com/1.1/user.json") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.start do |https| request = Net::HTTP::Get.new(uri.request_uri) request.oauth!(https, @consumer, @access_token) buf = "" https.request(request) do |response| response.read_body do |chunk| buf << chunk while(line = buf[/.*(\r\n)+/m]) begin buf.sub!(line,"") line.strip! status = JSON.parse(line) rescue break end yield status end end end end end |
#user_timeline(user_id = "", screen_name = "", since_id = "", count = "", max_id = "", trim_user = "", exclude_replies = "", contributor_details = "", include_rts = "") ⇒ Object
GET statuses/user_timeline
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/twitruby.rb', line 182 def user_timeline(user_id="",screen_name="",since_id="",count="",max_id="",trim_user="",exclude_replies="",contributor_details="",include_rts="") return JSON.parse(@access_token.get("/1.1/statuses/user_timeline.json", "user_id" => user_id, "screen_name" => screen_name, "since_id" => since_id, "count" => count, "max_id" => max_id, "trim_user" => trim_user, "exclude_replies" => exclude_replies, "contributor_details" => contributor_details, "include_rts" => include_rts ).body) end |
#verify_credentials ⇒ Object
GET account/verify_credentials
138 139 140 |
# File 'lib/twitruby.rb', line 138 def verify_credentials return JSON.parse(@access_token.get("/1.1/account/verify_credentials.json").body) end |