Sha256: fe483170aae64321ee5add99212614b9b4aca7f56ee1e1a04328f18127b1c133

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

# automate creating twitts
%w{net/http json platform utility_belt}.each {|lib| require lib}
UtilityBelt.equip(:clipboard)

module UtilityBelt
  module Twitty
    def twitt(message = nill)
      message ||= Clipboard.read if Clipboard.available?
      if message.length > 160
        Raise ArgumentError, "Message should be shorter than 160 characters"
      else
        http = Net::HTTP.new("twitter.com",80)
        http.start do |http|
          request = Net::HTTP::Post.new("/statuses/update.json")
          request.basic_auth(ENV['TWITTER_USER'], ENV['TWITTER_PWD'])
          response = http.request(request, "status=#{URI.escape(message)}")
          case response
            when Net::HTTPSuccess, Net::HTTPRedirection
                return "Success!"
            else
                return response.error!
          end
        end
        http.close
      end
    end

    def fetch_twitt(number=nil)
      http = Net::HTTP.new("twitter.com",80)
      http.start do |http|
        request = Net::HTTP::Get.new("/statuses/friends_timeline.json")
        request.basic_auth(ENV['TWITTER_USER'], ENV['TWITTER_PWD'])
        response = http.request(request)
        case response
          when Net::HTTPSuccess, Net::HTTPRedirection
            twitts = JSON.parse(response.body)
            twitts[0..((number-1 unless number.nil?) || twitts.length)].each do |twitt|
              screen_name = twitt["user"]["screen_name"]
              text = twitt["text"]
              case Platform::IMPL
                when :macosx, :linux
                  puts "\033[31m#{screen_name}\033\[0m => \033[33m#{text}\033\[0m"
                when :mswin
                   puts "#{screen_name} => #{value}"
              end
            end
            return "true"
         else
           return response.error!
        end
      end
    end
  end
end

class Object
  include UtilityBelt::Twitty
end if Object.const_defined? :IRB

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
FreedomCoder-utility_belt-1.1.1.2 lib/utility_belt/twitty.rb
FreedomCoder-utility_belt-1.1.1 lib/utility_belt/twitty.rb