Created
June 19, 2012 06:09
-
-
Save pzaich/2952549 to your computer and use it in GitHub Desktop.
jstwitter
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
| require 'jumpstart_auth' | |
| require 'bitly' | |
| class JSTwitter | |
| attr_reader :client | |
| def initialize | |
| puts "Initializing..." | |
| @client = JumpstartAuth.twitter | |
| @screen_names = @client.followers.collect{|follower| follower.screen_name} | |
| end | |
| def tweet(message) | |
| if message.length <= 140 | |
| @client.update(message) | |
| else | |
| puts "Over 140 characters!" | |
| end | |
| end | |
| def run | |
| puts "Welcome to the JSL Twitter Client!" | |
| command = "" | |
| while command != "q" | |
| puts "" | |
| printf "enter command: " | |
| input= gets.chomp | |
| parts = input.split | |
| command = parts [0] | |
| case command | |
| when 'q' then puts "Goodbye!" | |
| when 't' then tweet(parts[1..-1].join(' ')) | |
| when 'dm' then dm(parts[1], parts [2..-1].join(' ')) | |
| when 'spam' then spam_my_friends(parts[1..-1].join(' ')) | |
| when 'elt' then everyones_last_tweet | |
| when 's' then shorten(parts[1..-1].join('')) | |
| when 'turl' then tweet(parts[1..-2].join(' ') + " " + shorten(parts[-1])) | |
| else | |
| puts "Sorry, I don't know how to #{ command }" | |
| end | |
| end | |
| end | |
| def dm(target, message) | |
| puts "Trying to send #{target} this direct message:" | |
| puts message | |
| if followers_list.include? target | |
| tweet("d #{target} #{message}") | |
| else | |
| printf "#{target} is not following you." | |
| end | |
| end | |
| def followers_list | |
| @screen_names | |
| end | |
| def everyones_last_tweet | |
| friends = @client.friends.sort_by {|friend| friend.screen_name} | |
| friends.each do |friend| | |
| timestamp = friend.status.created_at | |
| puts "#{friend.screen_name} said this on #{timestamp.strftime("%A, %b %d")}..." | |
| puts friend.status.text | |
| puts "-"*50 | |
| end | |
| end | |
| def spam_my_friends(message) | |
| followers_list.each do |follower| | |
| dm(follower, message) | |
| end | |
| end | |
| def shorten(original_url) | |
| Bitly.use_api_version_3 | |
| bitly = Bitly.new('hungryacademy', 'R_430e9f62250186d2612cca76eee2dbc6') | |
| puts "Shortening this URL: #{original_url}" | |
| shortened_url = bitly.shorten(original_url).short_url | |
| end | |
| end | |
| jst = JSTwitter.new | |
| jst.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment