zunnit 0.2.0 → 0.3.1
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/.gitignore +6 -1
- data/Gemfile.lock +2 -2
- data/lib/zunnit/api.rb +55 -50
- data/lib/zunnit/setup.rb +24 -24
- data/lib/zunnit/utils.rb +15 -15
- data/lib/zunnit/version.rb +1 -1
- data/spec/spec_helper.rb +5 -5
- data/spec/zunnit_spec.rb +72 -70
- data/zunnit.gemspec +2 -2
- metadata +2 -2
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
zunnit (0.
|
4
|
+
zunnit (0.3.0)
|
5
5
|
faraday
|
6
6
|
|
7
7
|
GEM
|
@@ -12,7 +12,7 @@ GEM
|
|
12
12
|
faraday (0.7.2)
|
13
13
|
addressable (~> 2.2.6)
|
14
14
|
multipart-post (~> 1.1.0)
|
15
|
-
rack (
|
15
|
+
rack (>= 1.1.0, < 2)
|
16
16
|
multipart-post (1.1.2)
|
17
17
|
rack (1.3.0)
|
18
18
|
rspec (2.6.0)
|
data/lib/zunnit/api.rb
CHANGED
@@ -6,59 +6,64 @@ module Zunnit
|
|
6
6
|
end
|
7
7
|
|
8
8
|
# API Class
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
self.client = Zunnit.client
|
14
|
-
self.key = Zunnit.key
|
15
|
-
self.version = Zunnit.version
|
16
|
-
|
17
|
-
yield self if block_given?
|
18
|
-
end
|
19
|
-
|
20
|
-
# Make a GET request to Zunnit's API
|
21
|
-
def get(action, options={})
|
22
|
-
request(:get, action, options)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Make a POST request to Zunnit's API
|
26
|
-
def post(action, options={})
|
27
|
-
request(:post, action, options)
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
def connection
|
32
|
-
@connection ||= Faraday.new(:url => Zunnit::URL)
|
33
|
-
end
|
9
|
+
class Api
|
10
|
+
attr_accessor :client, :key, :version
|
34
11
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
12
|
+
def initialize
|
13
|
+
self.client = Zunnit.client
|
14
|
+
self.key = Zunnit.key
|
15
|
+
self.version = Zunnit.version
|
40
16
|
|
41
|
-
|
42
|
-
|
43
|
-
# Raise error if the actions is invalid
|
44
|
-
unless Zunnit.actions[action]
|
45
|
-
raise "Invalid action \"#{action}\""
|
46
|
-
end
|
17
|
+
yield self if block_given?
|
18
|
+
end
|
47
19
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
20
|
+
# Make a GET request to Zunnit's API
|
21
|
+
def get(action, options={})
|
22
|
+
request(:get, action, options)
|
23
|
+
end
|
52
24
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
req.params = params_for options
|
58
|
-
end
|
25
|
+
# Make a POST request to Zunnit's API
|
26
|
+
def post(action, options={})
|
27
|
+
request(:post, action, options)
|
28
|
+
end
|
59
29
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
30
|
+
private
|
31
|
+
def connection
|
32
|
+
@connection ||= Faraday.new(:url => Zunnit::URL)
|
33
|
+
end
|
34
|
+
|
35
|
+
def params_for(options={})
|
36
|
+
return {
|
37
|
+
:api_key => self.key
|
38
|
+
}.merge(options)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Make a request to Zunnit's API
|
42
|
+
def request(http_method, action, options={})
|
43
|
+
# Raise error if the actions is invalid
|
44
|
+
unless Zunnit.actions[action]
|
45
|
+
raise "Invalid action \"#{action}\""
|
46
|
+
end
|
47
|
+
|
48
|
+
# Raise error if the http method is invalid
|
49
|
+
unless [:get, :post].include? http_method
|
50
|
+
raise "Invalid http_method \"#{http_method}\""
|
51
|
+
end
|
52
|
+
|
53
|
+
# Make the Request
|
54
|
+
response = connection.send(http_method) do |req|
|
55
|
+
action_url = "/#{client}#{Zunnit.actions[action]}"
|
56
|
+
req.url(action_url)
|
57
|
+
if http_method.to_s =~ /post|put/
|
58
|
+
req.body = params_for options
|
59
|
+
else
|
60
|
+
req.params = params_for options
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# return HASH for body JSON
|
65
|
+
JSON response.body, :symbolize_names => true
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
64
69
|
end
|
data/lib/zunnit/setup.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
module Zunnit
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
2
|
+
extend Utils
|
3
|
+
mattr_accessor :client, :key, :version, :actions
|
4
|
+
|
5
|
+
# Fixed constants
|
6
|
+
URL = "http://api.zunnit.com/"
|
7
|
+
ACTIONS = {
|
8
|
+
:related_items => "/related/items",
|
9
|
+
:recommendation_items_for_user => "/recommendation/items/for_user",
|
10
|
+
:recommendation_groups_for_user => "/recommendation/groups/for_user",
|
11
|
+
:recommendation_users_for_user => "/recommendation/users/for_user",
|
12
|
+
:recommendation_users_for_item => "/recommendation/users/for_item",
|
13
|
+
:recommendation_tags_for_item => "/recommendation/tags/for_item",
|
14
|
+
:recommendation_cluster_for_item => "/recommendation/cluster/for_item",
|
15
|
+
:action_items_add => "/action/items/add",
|
16
|
+
:action_items_rate => "/action/items/rate",
|
17
|
+
:action_items_view => "/action/items/view",
|
18
|
+
:action_user_follow => "/action/user/follow"
|
19
|
+
}
|
20
|
+
|
21
|
+
def self.setup
|
22
|
+
self.actions = ACTIONS
|
23
|
+
yield self if block_given?
|
24
|
+
self
|
25
|
+
end
|
26
26
|
end
|
data/lib/zunnit/utils.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module Zunnit
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
module Utils
|
3
|
+
def mattr_accessor *attrs
|
4
|
+
attrs.each do |attr|
|
5
|
+
module_eval %[
|
6
|
+
def self.#{attr.to_s}=(value)
|
7
|
+
@#{attr.to_s} = value
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
10
|
+
def self.#{attr.to_s}
|
11
|
+
@#{attr.to_s}
|
12
|
+
end
|
13
|
+
]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/zunnit/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -5,11 +5,11 @@ require 'rspec'
|
|
5
5
|
require 'zunnit'
|
6
6
|
|
7
7
|
RSpec.configure do |config|
|
8
|
-
config.fail_fast
|
8
|
+
config.fail_fast = true
|
9
9
|
end
|
10
10
|
|
11
11
|
Zunnit.setup do |z|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
12
|
+
z.client = "busk"
|
13
|
+
z.key = "busk"
|
14
|
+
z.version = "1.0"
|
15
|
+
end
|
data/spec/zunnit_spec.rb
CHANGED
@@ -3,91 +3,93 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
|
4
4
|
describe Zunnit do
|
5
5
|
# let(zunnit) { Zunnit::Api.new }
|
6
|
-
|
7
|
-
|
6
|
+
|
7
|
+
describe "get" do
|
8
8
|
it "should instanciate a Zunnit::Api object" do
|
9
9
|
default_key = Zunnit.key
|
10
10
|
default_client = Zunnit.client
|
11
11
|
default_version = Zunnit.version
|
12
|
-
|
12
|
+
|
13
13
|
zunnit = Zunnit::Api.new do |z|
|
14
14
|
z.key = "new-key"
|
15
15
|
z.version = "1.1"
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
zunnit.class.should == Zunnit::Api
|
19
19
|
zunnit.client.should == default_client
|
20
20
|
zunnit.key.should == "new-key"
|
21
21
|
zunnit.version.should == "1.1"
|
22
|
-
|
22
|
+
|
23
23
|
Zunnit.client.should == default_client
|
24
24
|
Zunnit.key.should == default_key
|
25
25
|
Zunnit.version.should == default_version
|
26
26
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
27
|
+
|
28
|
+
it "should return related items" do
|
29
|
+
response = Zunnit.api.get :related_items,
|
30
|
+
:item_id => "20275919",
|
31
|
+
:user_id => "255"
|
32
|
+
validate_response(response, :items)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should get 400 error if :item_id is not given to related items" do
|
36
|
+
response = Zunnit.api.get(:related_items)
|
37
|
+
response[:status].should == 400
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return recommendation items for user" do
|
41
|
+
response = Zunnit.api.get :recommendation_items_for_user,
|
42
|
+
:user_id => "255"
|
43
|
+
validate_response(response, :items)
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
it "should return recommendation groups for user" do
|
48
|
+
pending "404"
|
49
|
+
response = Zunnit.api.get :recommendation_groups_for_user,
|
50
|
+
:user_id => "255"
|
51
|
+
validate_response(response, :groups)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return recommendation users for user" do
|
55
|
+
pending "404"
|
56
|
+
response = Zunnit.api.get :recommendation_groups_for_user,
|
57
|
+
:user_id => "2155"
|
58
|
+
validate_response(response, :users)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return recommendation users for item" do
|
62
|
+
pending "It should return a collection of users but is returning items"
|
63
|
+
response = Zunnit.api.get :recommendation_items_for_user,
|
64
|
+
:item_id => "20275919",
|
65
|
+
:user_id => "255"
|
66
|
+
validate_response(response, :users)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return recommendation tags for item" do
|
70
|
+
pending "404"
|
71
|
+
response = Zunnit.api.get :recommendation_tags_for_item,
|
72
|
+
:item_id => "20275919"
|
73
|
+
validate_response(response, :tags)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return recommendation cluster for item" do
|
77
|
+
pending "404"
|
78
|
+
response = Zunnit.api.get :recommendation_cluster_for_item,
|
79
|
+
:item_id => "20275919"
|
80
|
+
validate_response(response, :clusters)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "post" do
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def validate_response(response, results_key)
|
90
|
+
response[:status].should == 200
|
91
|
+
response[:msg].should == "Ok"
|
92
|
+
response[results_key].size.should >= 1
|
93
|
+
end
|
92
94
|
end
|
93
95
|
|
data/zunnit.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
# Dependencies
|
23
|
+
s.add_dependency "faraday"
|
24
24
|
|
25
25
|
# Development Dependencies
|
26
26
|
s.add_development_dependency "rspec"
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: zunnit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Marcelo Eden
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-27 00:00:00 -03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|