wonderfl 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -0
- data/VERSION +1 -1
- data/lib/wonderfl.rb +2 -1
- data/lib/wonderfl/client.rb +38 -10
- data/lib/wonderfl/code.rb +0 -1
- data/lib/wonderfl/utils.rb +23 -0
- data/spec/test_client.rb +50 -0
- data/spec/wonderfl_spec.rb +1 -1
- data/wonderfl.gemspec +3 -2
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -27,6 +27,12 @@ Create a wonderfl API client using your api_key as follows:
|
|
27
27
|
client = Wonderfl::Client.new
|
28
28
|
client.api_key = 'your api_key'
|
29
29
|
|
30
|
+
if you want to cache the response from APIs
|
31
|
+
|
32
|
+
client = Wonderfl::Client.new('your api_key,
|
33
|
+
:cache_file => 'cache.db',
|
34
|
+
:expire => 60*10)
|
35
|
+
|
30
36
|
You can then use that client to retrieve information about user or posted codes as so:
|
31
37
|
|
32
38
|
client.get_user('user_name') #=> Wonderfl::User
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/wonderfl.rb
CHANGED
@@ -2,13 +2,14 @@ $:.unshift(File.dirname(__FILE__) + "/wonderfl/")
|
|
2
2
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'time'
|
5
|
+
require 'pstore'
|
5
6
|
require 'rubygems'
|
6
7
|
require 'json'
|
7
8
|
|
8
9
|
Net::HTTP.version_1_2
|
9
10
|
|
10
11
|
module Wonderfl
|
11
|
-
VERSION = '0.0
|
12
|
+
VERSION = '0.2.0'
|
12
13
|
BASE_URL = 'http://api.wonderfl.net'
|
13
14
|
class BadRequest < StandardError; end
|
14
15
|
class Unauthorized < StandardError; end
|
data/lib/wonderfl/client.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
module Wonderfl
|
2
1
|
# The main client object through which the Wonderfl service may be accessed.
|
2
|
+
module Wonderfl
|
3
3
|
class Client
|
4
|
-
# api key
|
5
4
|
attr_accessor :api_key
|
6
5
|
|
7
6
|
# Requires api key, get yours from your account page
|
8
7
|
# (wonderfl.net/account/api_keys/create)
|
9
8
|
#
|
10
|
-
def initialize(api_key = nil)
|
9
|
+
def initialize(api_key = nil, options = {})
|
11
10
|
@api_key = api_key
|
11
|
+
@cache = Wonderfl::Utils::Cache.new(options[:cache_file],
|
12
|
+
options[:expire])
|
12
13
|
end
|
13
14
|
|
14
15
|
# Returns a Wonderfl::User object detailing the user information.
|
@@ -16,7 +17,14 @@ module Wonderfl
|
|
16
17
|
def get_user(user_name)
|
17
18
|
_validate?(user_name)
|
18
19
|
path = '/user/' + user_name
|
19
|
-
|
20
|
+
@cache.transaction do
|
21
|
+
data = @cache[path]
|
22
|
+
if data.nil?
|
23
|
+
@cache[path] = User.new(_request(path)[:body]['user'])
|
24
|
+
else
|
25
|
+
data
|
26
|
+
end
|
27
|
+
end
|
20
28
|
end
|
21
29
|
|
22
30
|
# Returns a Wonderfl::UserCodes object (Wonderfl::Code collection)
|
@@ -25,7 +33,14 @@ module Wonderfl
|
|
25
33
|
def get_user_codes(user_name)
|
26
34
|
_validate?(user_name)
|
27
35
|
path = '/user/' + user_name + '/codes'
|
28
|
-
|
36
|
+
@cache.transaction do
|
37
|
+
data = @cache[path]
|
38
|
+
if data.nil?
|
39
|
+
@cache[path] = UserCodes.new(_request(path)[:body]['codes'])
|
40
|
+
else
|
41
|
+
data
|
42
|
+
end
|
43
|
+
end
|
29
44
|
end
|
30
45
|
|
31
46
|
# Returns a Wonderfl::Code object detailing the code information.
|
@@ -33,7 +48,14 @@ module Wonderfl
|
|
33
48
|
def get_code(code_id)
|
34
49
|
_validate?(code_id)
|
35
50
|
path = '/code/' + code_id
|
36
|
-
|
51
|
+
@cache.transaction do
|
52
|
+
data = @cache[path]
|
53
|
+
if data.nil?
|
54
|
+
@cache[path] = Code.new(_request(path)[:body]['code'], code_id)
|
55
|
+
else
|
56
|
+
data
|
57
|
+
end
|
58
|
+
end
|
37
59
|
end
|
38
60
|
|
39
61
|
# Returns a Wonderfl::CodeForks object (Wonderfl::Code collection)
|
@@ -42,10 +64,17 @@ module Wonderfl
|
|
42
64
|
def get_code_forks(code_id)
|
43
65
|
_validate?(code_id)
|
44
66
|
path = '/code/' + code_id + '/forks'
|
45
|
-
|
67
|
+
@cache.transaction do
|
68
|
+
data = @cache[path]
|
69
|
+
if data.nil?
|
70
|
+
@cache[path] = CodeForks.new(_request(path)[:body]['forks'])
|
71
|
+
else
|
72
|
+
data
|
73
|
+
end
|
74
|
+
end
|
46
75
|
end
|
47
76
|
|
48
|
-
#
|
77
|
+
# Private methods.
|
49
78
|
#
|
50
79
|
private
|
51
80
|
def _request(path)
|
@@ -70,8 +99,7 @@ module Wonderfl
|
|
70
99
|
end
|
71
100
|
{ :code => response.code, :body => parsed }
|
72
101
|
end
|
73
|
-
|
74
|
-
#
|
102
|
+
|
75
103
|
def _validate?(arg)
|
76
104
|
return true if arg.is_a? String
|
77
105
|
method_name = caller.first.scan(/`(.*)'/).to_s
|
data/lib/wonderfl/code.rb
CHANGED
data/lib/wonderfl/utils.rb
CHANGED
@@ -10,4 +10,27 @@ module Wonderfl::Utils
|
|
10
10
|
def parse_time(date)
|
11
11
|
date ? Time.at(date) : nil
|
12
12
|
end
|
13
|
+
# Caches response data from APIs
|
14
|
+
#
|
15
|
+
class Cache < PStore
|
16
|
+
def initialize(filename, expire)
|
17
|
+
@filename = filename || 'wonderfl_cache.db'
|
18
|
+
@expire = expire || 0
|
19
|
+
super(@filename)
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](key)
|
23
|
+
value = super(key) || {}
|
24
|
+
expired = (value[:timestamp].nil? ||
|
25
|
+
value[:timestamp] + @expire < Time.now)
|
26
|
+
expired ? nil : value[:entity]
|
27
|
+
end
|
28
|
+
|
29
|
+
def []=(key, value)
|
30
|
+
super(key, {
|
31
|
+
:entity => value,
|
32
|
+
:timestamp => Time.now
|
33
|
+
})
|
34
|
+
end
|
35
|
+
end
|
13
36
|
end
|
data/spec/test_client.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
4
|
+
|
5
|
+
require 'wonderfl'
|
6
|
+
|
7
|
+
begin
|
8
|
+
client = Wonderfl::Client.new('588b655bbd16a987675d0b574078659e55e6119c',
|
9
|
+
# :cache_file => 'a.db',
|
10
|
+
:expire => 10)
|
11
|
+
#client = Wonderfl::Client.new
|
12
|
+
#client.api_key = '588b655bbd16a987675d0b574078659e55e6119c'
|
13
|
+
p client
|
14
|
+
|
15
|
+
puts "*** get_user ***\n"
|
16
|
+
user = client.get_user('nondelion')
|
17
|
+
puts user
|
18
|
+
|
19
|
+
puts "*** get_user_codes ***\n"
|
20
|
+
codes = client.get_user_codes('mari')
|
21
|
+
puts "codes count: " + codes.count.to_s
|
22
|
+
codes.each do |code|
|
23
|
+
puts code
|
24
|
+
end
|
25
|
+
|
26
|
+
puts "*** get_code ***\n"
|
27
|
+
code = client.get_code('4v7t')
|
28
|
+
puts code
|
29
|
+
|
30
|
+
|
31
|
+
puts "*** get_code_forks ***\n"
|
32
|
+
forks = client.get_code_forks('4v7t')
|
33
|
+
puts "forks count: " + forks.count.to_s
|
34
|
+
forks.each do |code|
|
35
|
+
puts code
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
rescue Wonderfl::Unauthorized => e
|
40
|
+
puts 'Authentication Error: ' + e
|
41
|
+
|
42
|
+
rescue Wonderfl::NotFound => e
|
43
|
+
puts 'NotFound Error: '+ e
|
44
|
+
|
45
|
+
rescue Wonderfl::BadRequest => e
|
46
|
+
puts 'BadRequest Error: ' + e
|
47
|
+
end
|
48
|
+
|
49
|
+
include Wonderfl::Utils
|
50
|
+
#puts dump('/user/mari/codes', '588b655bbd16a987675d0b574078659e55e6119c')
|
data/spec/wonderfl_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe Wonderfl::Client do
|
4
4
|
before do
|
5
|
-
@client = Wonderfl::Client.new
|
5
|
+
@client = Wonderfl::Client.new(nil, :expire => 0)
|
6
6
|
@user_name = 'wellflat'
|
7
7
|
@api_key = 'foo'
|
8
8
|
@stub = WebMock.stub_request(:get, /http:\/\/api\.wonderfl\.net\/.*/)
|
data/wonderfl.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{wonderfl}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["wellflat"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-09-04}
|
13
13
|
s.description = %q{Ruby wrapper for the wonderfl API. See README for more details.}
|
14
14
|
s.email = %q{wellflat@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -46,6 +46,7 @@ Gem::Specification.new do |s|
|
|
46
46
|
s.summary = %q{Ruby wrapper for the wonderfl API}
|
47
47
|
s.test_files = [
|
48
48
|
"spec/spec_helper.rb",
|
49
|
+
"spec/test_client.rb",
|
49
50
|
"spec/wonderfl_spec.rb"
|
50
51
|
]
|
51
52
|
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- wellflat
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-09-04 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -122,4 +122,5 @@ specification_version: 3
|
|
122
122
|
summary: Ruby wrapper for the wonderfl API
|
123
123
|
test_files:
|
124
124
|
- spec/spec_helper.rb
|
125
|
+
- spec/test_client.rb
|
125
126
|
- spec/wonderfl_spec.rb
|