Created
          July 7, 2010 10:10 
        
      - 
      
- 
        Save yuroyoro/466526 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/ruby | |
| # rubyの練習で書いたTwitte APIをほげるヤツ | |
| require 'net/http' | |
| require 'json' | |
| require 'optparse' | |
| class TwitterApi | |
| # コンストラクタ | |
| def initialize( user, passwd ) | |
| @user = user | |
| @passwd = passwd | |
| end | |
| # GET投げてjson返すよ | |
| def get( api_url ) | |
| Net::HTTP.version_1_2 | |
| # Basic認証 | |
| req = Net::HTTP::Get.new( api_url ) | |
| req.basic_auth( @user, @passwd ) | |
| Net::HTTP.start('api.twitter.com'){|http| | |
| response = http.request( req ) | |
| json = response.body | |
| JSON.parse( json ) | |
| } | |
| end | |
| def post( api_url , data) | |
| Net::HTTP.version_1_2 | |
| # Basic認証 | |
| req = Net::HTTP::Post.new( api_url ) | |
| req.basic_auth( @user, @passwd ) | |
| data[:source] = 'ゆろよろが書いたRunyでTwitter APIをほげるヤツ' | |
| req.set_form_data(data, "&") | |
| Net::HTTP.start('api.twitter.com'){|http| | |
| response = http.request( req ) | |
| } | |
| end | |
| def print_json( json ) | |
| json.each do |status| | |
| yield status | |
| end | |
| nil | |
| end | |
| # statusを出力するよ | |
| def print_status( json ) | |
| print_json( json ) do |status| | |
| user = status['user'] | |
| print "#{user['screen_name']}(#{user['name']}) : #{status['text']}\n" | |
| end | |
| end | |
| # friends_timelineを表示するよ | |
| def friends_timeline | |
| print_status(get('/1/statuses/friends_timeline.json')) | |
| end | |
| # mentionsを表示するよ | |
| def mentions | |
| print_status(get('/1/statuses/mentions.json')) | |
| end | |
| # retweets_of_meを表示するよ | |
| def retweets_of_me | |
| print_status(get('/1/statuses/retweets_of_me.json')) | |
| end | |
| def update( msg ) | |
| post('/1/statuses/update.json', {:status => msg }) | |
| end | |
| end | |
| # コマンドライン引数を解析する | |
| opt = OptionParser.new | |
| def usage | |
| puts "#{__FILE__} -u screen_name -p passwd [-fmrp] [msg]";exit | |
| end | |
| o = Hash.new | |
| begin | |
| opt.on('-u MANDATORY') {|v| o[:user] = v } | |
| opt.on('-p MANDATORY') {|v| o[:passwd] = v } | |
| opt.on('-f') {|v| o[:f] = v } | |
| opt.on('-m') {|v| o[:m] = v } | |
| opt.on('-r') {|v| o[:r] = v } | |
| opt.on('-s [OPTIONAL]') {|v| o[:msg] = v } | |
| opt.parse!(ARGV) | |
| rescue | |
| puts "#{__FILE__}: unrecognized option \`#{ARGV[0]} \'" | |
| exit | |
| end | |
| usage if o[:user].nil? || o[:passwd].nil? | |
| api = TwitterApi.new( o[:user], o[:passwd] ) | |
| api.friends_timeline if o[:f] | |
| api.mentions if o[:m] | |
| api.retweets_of_me if o[:r] | |
| api.update( o[:msg] ) if o[:msg] | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment