Probando Twitter4R: Twitter API en Ruby
Twitter4R es una libreria de Ruby para el API de Twitter. Es de codigo abierto y esta desarrollada por Susan Potter. Segun dicen en su pagina web la totalidad del API de Twitter esta cubierta. La verdad que salvo algunos problemas ,fruto de mi ignorancia, que tuve al principio todo corre de maravilla. De hecho hice un pequeño programita para probar la funcionalidad de la libreria.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 63 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 | require('rubygems') gem('twitter4r', '>=0.3.0') require('twitter') require('time') # Will not present timeline if removed! # Helpers def tweetLength tweet while tweet.length > 140 puts 'Tweet is too long, make it 140 characters or less' tweet = gets.chomp end end # Connect to twitter def authentication puts "User:" @username = gets.chomp puts "Password:" @passcode = gets.chomp $client = Twitter::Client.new( :login => @username , :password => @passcode ) end def connectToTwitter while $client.authenticate?(@username,@passcode) == false puts "Your credentials appear to be incorrect :O. Please try again" authentication end puts "You have been succesfully logged in =)" stuffToDo end # Twitter Functions def stuffToDo # Will ask you what you want to do and depending # on the number you choose you will be # send to that determinate method puts 'What you want to do now?, Please choose a number' puts '1 - Update Status' puts '2 - Send a message to a friend' puts '3 - Show my Timeline' selection = gets.to_i if selection == 1 updateStatus end if selection == 2 sendMessage end if selection == 3 getTimeline end end def updateStatus puts 'Tweet:' tweet = gets.chomp tweetLength tweet $client.status(:post, tweet) stuffToDo end def sendMessage puts 'Who do you want to recieve this message?' friend = gets.chomp puts 'Write your message please' tweet = gets.chomp tweetLength tweet $client.message(:post, tweet , friend) stuffToDo end def getTimeline $client.timeline_for(:friends) do |status| puts status.user.screen_name, status.text, "-----------------------------" end stuffToDo end authentication connectToTwitter |
Muy interesante software. Definitivamente te ahorra mucho trabajo. Pero sobre todo te ayuda a mantener limpio el codigo en lugar de andar ensuciandose las manos trabajando directamente con el API.