Class: Rubyku::Credentials

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

Overview

This class represents the credentials necessary to log in to the Jaiku service in order to access non-public portions of the API. It has 3 fields:

  • username

  • password

  • api_key

Only username and api_key are required to log in to Jaiku. Password is offered as a way to make things easier for the user: since API keys aren’t easily memorable (and subject to change, in Jaiku’s case), rubyku allows for setting the username and password alone. It will then fetch the api_key for the user. Example:

cred = Rubyku::Credentials.new
cred.username = 'duwanis'
cred.password = 'notmyrealpassword'
cred.api_key
=> 'apikey1234567890'

For a single-user app, one instance of Rubyku::Credentials should be created and saved on the Rubyku::Settings class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

returns the Jaiku API key for these credentials. This is a lazy-load property. Since there is a method that can be used to retrieve the API key with the username and password, rubyku does not require you to specify that key if you would rather specify a username and password.

returns: String the API key for this user.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubyku/credentials.rb', line 38

def api_key
  log = Rubyku::Settings.logger
  unless @api_key
    log.info("api key for user #{username} requested - attempting to fetch.")
    unless(@username and @password)
      raise Rubyku::InvalidCredentialsError, "Need both username and password to fetch api_key."
    end
    @api_key = Rubyku::Credentials.fetch_api_key(@username, @password)
  end
  
  @api_key
end

#passwordObject

Returns the value of attribute password.



26
27
28
# File 'lib/rubyku/credentials.rb', line 26

def password
  @password
end

#usernameObject

Returns the value of attribute username.



25
26
27
# File 'lib/rubyku/credentials.rb', line 25

def username
  @username
end

Class Method Details

.fetch_api_key(username, password) ⇒ Object

Given a username and password, retrieve that user’s API key. this works by logging the user in, stripping cookie information off of the response, and then using that cookie information to hit api.jaiku.com/key . All credit for this approach to this python snippet on DZone: snippets.dzone.com/posts/show/5539 For the record, I’m torn about leaving this in. On the one hand it assumes a lot about the internals of jaiku - on the other, it allows the user to be oblivious to the API key and any changes it may undergo.

returns: String the API key for the user, if found.

raises:

  • Rubyku::InvalidCredentialsError - username/password were not valid.

  • Rubyku::Error - there was an error communicating with the Jaiku servers.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubyku/credentials.rb', line 75

def fetch_api_key(username, password)
  log = Rubyku::Settings.logger
  
  #login to the site
  log.info "Attempting to log into Jaiku"
  log.debug "Username: #{username}\nPassword: #{password}"
  cookie_res = Net::HTTP::Proxy(Rubyku::Settings.proxy_url, Rubyku::Settings.proxy_port) \
              .post_form(URI.parse('http://jaiku.com/login'),
              {:log=> username, :pwd=> password, :rememberme => '1'})
  log.info "Login response: " + cookie_res.to_s
  if cookie_res.code == "200"
    #If Jaiku returns a 200 OK, the login failed (bad credentials).
    log.error "Jaiku returned 200 OK, this means the username and password weren't valid"
    raise Rubyku::InvalidCredentialsError
  end
  unless cookie_res.code == "303"
    #If Jaiku returns anything other than a redirect (which is expected
    # for a successful logon), then something's broke.
    log.error "Jaiku returned something other than a 303. Something borked."
    raise Rubyku::Error, "Could not connect to http://www.jaiku.com/login to retrieve API key."
  end
  
  #retrieve the cookie contents from the successful login result
  cookie_match = /(jaikuuser_[^;]*).*(jaikupass_[^;]*)/.match(cookie_res['set-cookie'])
  cookie = "%s; %s" % [cookie_match[1], cookie_match[2]]
  log.debug "Cookie: #{cookie}"
  log.info "Attempting to connect to api.jaiku.com and get the API key..."
  #now that we have the cookie information, let's grab the API key.
  url = URI.parse('http://api.jaiku.com/key')
  api_req = Net::HTTP::Get.new(url.path)
  api_req.add_field("Cookie", cookie)
  api_res = Net::HTTP::Proxy(Rubyku::Settings.proxy_url, Rubyku::Settings.proxy_port) \
                .start(url.host, url.port) {|http|
    http.request(api_req)
  }
  log.debug "api.jaiku.com responded with " + api_res.code
  log.debug api_res.to_s
  #If this request was good, then the body should just be the API key.
  api_res.body
end

Instance Method Details

#request_stringObject

returns the Jaiku-specified authentication string to attach to all requests (GETS) against the API.



54
55
56
# File 'lib/rubyku/credentials.rb', line 54

def request_string
  "?user=%s&personal_key=%s" % [self.username, self.api_key]
end