Class: Bluetail

Inherits:
Object
  • Object
show all
Defined in:
lib/bluetail.rb

Constant Summary collapse

@@url =
"https://api.twitter.com/1.1/statuses/update.json"

Instance Method Summary collapse

Constructor Details

#initialize(oauth_settings = {}, log_file = STDOUT) ⇒ Bluetail

Returns a new instance of Bluetail.



8
9
10
11
# File 'lib/bluetail.rb', line 8

def initialize(oauth_settings = {}, log_file = STDOUT)
  @oauth_settings = oauth_settings
  @logger = Logger.new(log_file)
end

Instance Method Details

#tweet(status) ⇒ Object

Raises:

  • (RuntimeError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bluetail.rb', line 13

def tweet(status)
  raise RuntimeError.new("Status too long") if status.length > 140

  header  = SimpleOAuth::Header.new("POST", @@url, {status: status}, @oauth_settings)
  body    = URI.encode("status=#{status}")
  uri     = URI(@@url)
  request = create_request_for(uri.path, header, body)

  Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
    response = http.request request

    if response.code.to_s == "200"
      @logger.info("Success")
    else
      @logger.error("#{response.code} - #{response.body}")
    end
  end
end