duwanis-rubyku 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +19 -1
- data/Rakefile +7 -0
- data/lib/rubyku.rb +19 -3
- data/lib/rubyku/credentials.rb +103 -0
- data/lib/rubyku/settings.rb +28 -0
- data/lib/rubyku/user.rb +64 -0
- data/lib/rubyku/version.rb +1 -1
- metadata +5 -1
data/README
CHANGED
@@ -8,7 +8,25 @@ Rubyku is a ruby library for accessing data via the Jaiku API.
|
|
8
8
|
|
9
9
|
== FEATURES/PROBLEMS:
|
10
10
|
|
11
|
-
|
11
|
+
Authorization against Jaiku is done using the Rubyku::Credentials object.
|
12
|
+
While the Jaiku API uses an API key instead of the username/password, I
|
13
|
+
thought it would be nice to allow for both.
|
14
|
+
So the api_key property on credentials will lazy-load itself (by logging
|
15
|
+
into the site and fetching it for you) if you populate the Credentials
|
16
|
+
object with a proper username and password.
|
17
|
+
Example:
|
18
|
+
cred = Rubyku::Credentials.new
|
19
|
+
cred.username = 'duwanis'
|
20
|
+
cred.password = 'notmyrealpassword'
|
21
|
+
cred.api_key
|
22
|
+
> "1n2o3t4m5y6a7p8i9k0e1y"
|
23
|
+
|
24
|
+
Everything you may need to configure should be accessible via the
|
25
|
+
Rubyku::Settings object. So far:
|
26
|
+
proxy_url
|
27
|
+
proxy_port
|
28
|
+
jaiku_credentials #this is a place to save a default login, for a single-user app
|
29
|
+
logger #an instance of Logger that will be used by rubyku for, well, logging.
|
12
30
|
|
13
31
|
== SYNOPSIS:
|
14
32
|
|
data/Rakefile
ADDED
data/lib/rubyku.rb
CHANGED
@@ -1,6 +1,22 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Just to make our requires at the top of the gem a little easier.
|
5
|
+
require 'pathname'
|
6
|
+
module Kernel
|
7
|
+
def __DIR__
|
8
|
+
Pathname(__FILE__).dirname.expand_path
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'net/http'
|
13
|
+
require 'logger'
|
14
|
+
#rubyku requires
|
15
|
+
require __DIR__ + 'rubyku/settings'
|
16
|
+
require __DIR__ + 'rubyku/user'
|
17
|
+
require __DIR__ + 'rubyku/credentials'
|
18
|
+
require __DIR__ + 'rubyku/version'
|
19
|
+
|
20
|
+
# The basic Rubyku exception.
|
21
|
+
class RubykuError < RuntimeError
|
22
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Rubyku
|
2
|
+
class Credentials
|
3
|
+
attr_accessor :username
|
4
|
+
attr_accessor :password
|
5
|
+
attr_writer :api_key
|
6
|
+
|
7
|
+
##
|
8
|
+
# returns the Jaiku API key for these credentials.
|
9
|
+
# This is a lazy-load property. Since there is a method
|
10
|
+
# that can be used to retrieve the API key with the username
|
11
|
+
# and password, rubyku does not require you to specify that
|
12
|
+
# key if you would rather specify a username and password.
|
13
|
+
#
|
14
|
+
# @return <String> the API key for this user.
|
15
|
+
#
|
16
|
+
# @raise <Rubyku::InvalidCredentialsError> indicates that the provided
|
17
|
+
# username and password are not valid for Jaiku.
|
18
|
+
#
|
19
|
+
# @raise <RubykuError> indicates that there was an error communicating
|
20
|
+
# to the Jaiku servers.
|
21
|
+
#
|
22
|
+
def api_key
|
23
|
+
log = Rubyku::Settings.logger
|
24
|
+
unless @api_key
|
25
|
+
log.info("api key for user #{username} requested - attempting to fetch.")
|
26
|
+
unless(@username and @password)
|
27
|
+
raise Rubyku::InvalidCredentialsError, "Need both username and password to fetch api_key."
|
28
|
+
end
|
29
|
+
@api_key = Rubyku::Credentials.fetch_api_key(@username, @password)
|
30
|
+
end
|
31
|
+
|
32
|
+
@api_key
|
33
|
+
end
|
34
|
+
|
35
|
+
class << self
|
36
|
+
|
37
|
+
##
|
38
|
+
# Given a username and password, retrieve that user's API key.
|
39
|
+
# this works by logging the user in, stripping cookie information
|
40
|
+
# off of the response, and then using that cookie information
|
41
|
+
# to hit http://api.jaiku.com/key . All credit for this approach
|
42
|
+
# to this python snippet on DZone: http://snippets.dzone.com/posts/show/5539
|
43
|
+
# For the record, I'm torn about leaving this in. On the one hand it assumes
|
44
|
+
# a lot about the internals of jaiku - on the other, it allows the user
|
45
|
+
# to be oblivious to the API key and any changes it may undergo.
|
46
|
+
#
|
47
|
+
# @param username<String> the username to use for authentication.
|
48
|
+
#
|
49
|
+
# @param password<String> the password to use for authentication.
|
50
|
+
#
|
51
|
+
# @raise <Rubyku::InvalidCredentialsError> indicates that the provided
|
52
|
+
# username and password are not valid for Jaiku.
|
53
|
+
#
|
54
|
+
# @raise <RubykuError> indicates that there was an error communicating
|
55
|
+
# to the Jaiku servers.
|
56
|
+
#
|
57
|
+
def fetch_api_key(username, password)
|
58
|
+
log = Rubyku::Settings.logger
|
59
|
+
|
60
|
+
#login to the site
|
61
|
+
log.info "Attempting to log into Jaiku"
|
62
|
+
log.debug "Username: #{username}\nPassword: #{password}"
|
63
|
+
cookie_res = Net::HTTP::Proxy(Rubyku::Settings.proxy_url, Rubyku::Settings.proxy_port) \
|
64
|
+
.post_form(URI.parse('http://jaiku.com/login'),
|
65
|
+
{:log=> username, :pwd=> password, :rememberme => '1'})
|
66
|
+
log.info "Login response: " + cookie_res.to_s
|
67
|
+
if cookie_res.code == "200"
|
68
|
+
#If Jaiku returns a 200 OK, the login failed (bad credentials).
|
69
|
+
log.error "Jaiku returned 200 OK, this means the username and password weren't valid"
|
70
|
+
raise Rubyku::InvalidCredentialsError
|
71
|
+
end
|
72
|
+
unless cookie_res.code == "303"
|
73
|
+
#If Jaiku returns anything other than a redirect (which is expected
|
74
|
+
# for a successful logon), then something's broke.
|
75
|
+
log.error "Jaiku returned something other than a 303. Something borked."
|
76
|
+
raise RubykuError, "Could not connect to http://www.jaiku.com/login to retrieve API key."
|
77
|
+
end
|
78
|
+
|
79
|
+
#retrieve the cookie contents from the successful login result
|
80
|
+
cookie_match = /(jaikuuser_[^;]*).*(jaikupass_[^;]*)/.match(cookie_res['set-cookie'])
|
81
|
+
cookie = "%s; %s" % [cookie_match[1], cookie_match[2]]
|
82
|
+
log.debug "Cookie: #{cookie}"
|
83
|
+
log.info "Attempting to connect to api.jaiku.com and get the API key..."
|
84
|
+
#now that we have the cookie information, let's grab the API key.
|
85
|
+
url = URI.parse('http://api.jaiku.com/key')
|
86
|
+
api_req = Net::HTTP::Get.new(url.path)
|
87
|
+
api_req.add_field("Cookie", cookie)
|
88
|
+
api_res = Net::HTTP::Proxy(Rubyku::Settings.proxy_url, Rubyku::Settings.proxy_port) \
|
89
|
+
.start(url.host, url.port) {|http|
|
90
|
+
http.request(api_req)
|
91
|
+
}
|
92
|
+
log.debug "api.jaiku.com responded with " + api_res.code
|
93
|
+
log.debug api_res.to_s
|
94
|
+
#If this request was good, then the body should just be the API key.
|
95
|
+
api_res.body
|
96
|
+
end
|
97
|
+
end #end class << self
|
98
|
+
|
99
|
+
end #end class
|
100
|
+
|
101
|
+
class InvalidCredentialsError < RuntimeError
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Rubyku
|
2
|
+
class Settings
|
3
|
+
class << self
|
4
|
+
#Yay logger! Default logger is initialized below. Override it
|
5
|
+
# if you want to use your own logger.
|
6
|
+
attr_accessor :logger
|
7
|
+
|
8
|
+
# Anywhere a rubyku method needs credentials, it will default
|
9
|
+
# to this property.
|
10
|
+
# You shouldn't use this property if you are writing a multi-user
|
11
|
+
# app. If you're writing a single user app, however, setting the
|
12
|
+
# credentials here lets you set-and-forget.
|
13
|
+
attr_accessor :jaiku_credentials
|
14
|
+
|
15
|
+
# Proxy settings. If these are nil, net/http just ignores it.
|
16
|
+
# So set these to nil if you don't need a proxy.
|
17
|
+
attr_accessor :proxy_url
|
18
|
+
attr_accessor :proxy_port
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#initialize the default logger
|
24
|
+
Rubyku::Settings.logger= Logger.new(__DIR__ + '../logs/rubyku.log')
|
25
|
+
Rubyku::Settings.logger.sev_threshold = Logger::INFO #DON'T set this line to DEBUG.
|
26
|
+
# Do that in your own code if you want. Rubyku spews user information and such on DEBUG
|
27
|
+
# And so it would be a very bad idea for DEBUG to make it into the gem.
|
28
|
+
|
data/lib/rubyku/user.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Rubyku
|
2
|
+
|
3
|
+
class User
|
4
|
+
attr_accessor :avatar_url
|
5
|
+
attr_accessor :first_name
|
6
|
+
attr_accessor :last_name
|
7
|
+
attr_accessor :nick
|
8
|
+
attr_accessor :jaiku_url
|
9
|
+
attr_writer :contacts
|
10
|
+
|
11
|
+
##
|
12
|
+
# Returns the list of contacts for the current user.
|
13
|
+
# Contacts is the only property of User that is lazy-loaded.
|
14
|
+
# This is because if it wasn't lazy-loaded, you'd be perpetually
|
15
|
+
# creating user objects.
|
16
|
+
#
|
17
|
+
# @param jaiku_credentials<Rubyku::Credentials> the credentials to use
|
18
|
+
# for authenticating with Jaiku. Defaults to any credentials set in <Rubyku::Settings>.
|
19
|
+
#
|
20
|
+
# @return <Array{Rubyku::User}> the users that this user follows on Jaiku.
|
21
|
+
#
|
22
|
+
# @raise <Rubyku::UserNotFoundError> indicates that the user could not be found
|
23
|
+
# in Jaiku.
|
24
|
+
#
|
25
|
+
# @raise <RubykuError> indicates that there was an error communicating
|
26
|
+
# to the Jaiku servers.
|
27
|
+
#
|
28
|
+
def contacts(jaiku_credentials=Rubyku::Settings.jaiku_credentials)
|
29
|
+
unless @contacts
|
30
|
+
@contacts = User.fetchContacts(@username, jaiku_credentials)
|
31
|
+
end
|
32
|
+
|
33
|
+
@contacts
|
34
|
+
end
|
35
|
+
|
36
|
+
class << self
|
37
|
+
##
|
38
|
+
# Retrieves a user's information from Jaiku.
|
39
|
+
# All properties but contacts are loaded. @see <Rubyku::User.contacts>
|
40
|
+
#
|
41
|
+
# @param username<String> the user to fetch.
|
42
|
+
#
|
43
|
+
# @param jaiku_credentials<Rubyku::Credentials> the credentials to use
|
44
|
+
# for authenticating with Jaiku. Defaults to any credentials set in <Rubyku::Defaults>.
|
45
|
+
#
|
46
|
+
# @return <Rubyku::User> the requested user.
|
47
|
+
#
|
48
|
+
# @raise <Rubyku::UserNotFoundError> indicates that the user could not be found
|
49
|
+
# in Jaiku.
|
50
|
+
#
|
51
|
+
# @raise <RubykuError> indicates that there was an error communicating
|
52
|
+
# to the Jaiku servers.
|
53
|
+
#
|
54
|
+
def fetch(username,jaiku_credentials=Rubyku::Settings.jaiku_credentials)
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
class UserNotFoundError < RuntimeError
|
63
|
+
end
|
64
|
+
end
|
data/lib/rubyku/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duwanis-rubyku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tommy Morgan
|
@@ -25,8 +25,12 @@ extra_rdoc_files:
|
|
25
25
|
files:
|
26
26
|
- LICENSE
|
27
27
|
- README
|
28
|
+
- Rakefile
|
28
29
|
- lib/rubyku.rb
|
29
30
|
- lib/rubyku/version.rb
|
31
|
+
- lib/rubyku/credentials.rb
|
32
|
+
- lib/rubyku/settings.rb
|
33
|
+
- lib/rubyku/user.rb
|
30
34
|
- test/test_helper.rb
|
31
35
|
- test/test_rubyku.rb
|
32
36
|
has_rdoc: true
|