Class: Rescuetime::Uploader

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/rescuetime/uploader.rb

Constant Summary collapse

API_HOST =

require ‘yaml’

"api.rescuetime.com"
API_HANDSHAKE_PATH =
"/api/handshake"
API_UPLOAD_PATH =
"/api/userlogs"
USER_AGENT =

TODO: use own USER_AGENT

'RescueTimeLinuxUploader/0.91 +https://launchpad.net/rescuetime-linux-uploader'

Instance Method Summary collapse

Methods included from Debug

#debug?

Constructor Details

#initialize(options = {}) ⇒ Uploader

Usage:

Rescuetime::Uploader.new({
  :email    => "[email protected]",
  :password => "bar",
  :debug    => false
})


22
23
24
25
26
27
28
29
30
# File 'lib/rescuetime/uploader.rb', line 22

def initialize(options = {})
  debug! if options[:debug]

  @http   = Net::HTTP.new(API_HOST, 443)
  @http.use_ssl = true

  @email    = options[:email]
  @password = options[:password]
end

Instance Method Details

#connection_alive?Boolean

Service available?

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/rescuetime/uploader.rb', line 33

def connection_alive?
  resp = @http.get(API_HANDSHAKE_PATH)
  resp.code == "200"
end

#handshakeObject

Handshake with login credentials

Returns:

true if handshake successful, false otherwise


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rescuetime/uploader.rb', line 72

def handshake
  data    = "email=#{@email}&password=#{@password}"
  headers = { 'User-agent' => USER_AGENT }

  begin
    resp = @http.post(API_HANDSHAKE_PATH, data, headers)
  rescue SocketError
    return false
  end

  debug "[HANDSHAKE]" do puts resp.body end

  return (resp.code == "200" && !resp.body["login failure"])
end

#upload(options = {}) ⇒ Object

Uploads yaml-formatted data

Usage:

@uploader.upload(:yamldata => yamldata)

Returns:

true if upload successful, false otherwise


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
# File 'lib/rescuetime/uploader.rb', line 43

def upload(options = {})
  hash = { :email    => @email,
           :password => @password,
           :yamldata => options[:yamldata] }

  data = []
  hash.each do |key, value|
    data << "#{key.to_s}=#{CGI.escape(value)}" if value
  end

  data = data.join("&")
  debug "[YAMLDATA]" do puts data end

  headers = { 'User-agent' => USER_AGENT }

  begin
    resp = @http.post(API_UPLOAD_PATH, data, headers)
  rescue
    return false
  end

  debug "[UPLOAD]" do puts resp.body end

  return (resp.code == "200" && !resp.body["<error>"])
end