Module: UtilityBelt::Twitty

Included in:
Object
Defined in:
lib/utility_belt/twitty.rb

Instance Method Summary collapse

Instance Method Details

#fetch_twitt(number = nil) ⇒ Object



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
# File 'lib/utility_belt/twitty.rb', line 28

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

#twitt(message = nill) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/utility_belt/twitty.rb', line 7

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