client_api 0.0.6 → 0.0.7
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.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/lib/client_api/client.rb +86 -0
- data/lib/client_api/version.rb +1 -1
- data/lib/client_api.rb +2 -1
- data/spec/client_spec.rb +51 -0
- data/spec/handler_spec.rb +3 -1
- data/spec/spec_helper.rb +27 -5
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c676ea36f31d7b2e7557c063c76a32076be2241
|
4
|
+
data.tar.gz: b3a618898f38ceea740ed80bfd5e63b243156b7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 729fec766c741bd7467288f160538f16c21940d6b12b14d71354d5363fad973e074bc846918bf62ec38bdf223cb780ceac3221a2b92a14e3547f81e20be24be4
|
7
|
+
data.tar.gz: 7166dbc2230799322b8540c8262b361c7cc3ecdfcb7eb29d63e3a474491b5c8d6609b1cefaad316bdd4a356731add01b623d98eea6afdde2887501ceee045685
|
data/Gemfile
CHANGED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'rest-client'
|
3
|
+
require_relative '../conf/conf_api'
|
4
|
+
require_relative 'handler'
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
module Recommender
|
9
|
+
class Client
|
10
|
+
|
11
|
+
|
12
|
+
def self.connect(api_key)
|
13
|
+
new(api_key)
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_item(add_param)
|
17
|
+
score = get_action_score(add_param[:engine_name],add_param[:action])
|
18
|
+
score != nil ? Handler.load_data(get_load_parameter(add_param,score)) : false
|
19
|
+
end
|
20
|
+
|
21
|
+
def load_data(param)
|
22
|
+
parameter = {engine_name: param[:engine_name],data: param[:data],api_key: @api_key,token: @token}
|
23
|
+
Handler.load_data(parameter)
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_similar_users(param)
|
27
|
+
parameter = {engine_name: param[:engine_name],api_key: @api_key,token: @token,id: param[:id],nb_wanted: param[:n]}
|
28
|
+
Handler.similar_users(parameter)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_similar_items(param)
|
32
|
+
parameter = {engine_name: param[:engine_name],api_key: @api_key,token: @token,id: param[:id],nb_wanted: param[:n]}
|
33
|
+
Handler.similar_items(parameter)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_recommendations(param)
|
37
|
+
parameter = {engine_name: param[:engine_name],api_key: @api_key,token: @token,id: param[:id],nb_wanted: param[:n]}
|
38
|
+
Handler.recommend(parameter)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def initialize(api_key)
|
44
|
+
@api_key = api_key
|
45
|
+
@token = Handler.connect_app(get_connect_param(api_key))
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_connect_param(api_key)
|
49
|
+
{object: {api_key: api_key}.to_json,option: {user: false,app: true}.to_json}
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_engine(engine_name)
|
53
|
+
param = {model_name: 'Engine',condition: {name: engine_name}.to_json}
|
54
|
+
object = call_api('/restricted/read',param)
|
55
|
+
engine = JSON.parse(object)
|
56
|
+
engine[0]
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_load_parameter(add_param,score)
|
60
|
+
{engine_name: add_param[:engine_name],api_key: @api_key,token: @token,data: [{customer_id: add_param[:customer_id],item_id: add_param[:item_id],preference: score}].to_json}
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_action_score(engine_name,action)
|
65
|
+
score = nil
|
66
|
+
engine = get_engine(engine_name)
|
67
|
+
actions = engine["engine_conf"]["actions"]
|
68
|
+
actions.each do |a|
|
69
|
+
if a["action"] == action
|
70
|
+
score = a["score"].to_i
|
71
|
+
break
|
72
|
+
end
|
73
|
+
end
|
74
|
+
score
|
75
|
+
end
|
76
|
+
|
77
|
+
def call_api(uri,param)
|
78
|
+
begin
|
79
|
+
RestClient.get API_SERVER+uri,params: param
|
80
|
+
rescue => e
|
81
|
+
e.response
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
data/lib/client_api/version.rb
CHANGED
data/lib/client_api.rb
CHANGED
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Recommender::Client do
|
5
|
+
|
6
|
+
|
7
|
+
let(:api_key) { "e26b56a0-f08f-0131-ddaf-080027ab4d7b" }
|
8
|
+
let(:add_param) {{type: 'article',engine_name: 'engine1',customer_id: 5,item_id: 2,action: 'Like'}}
|
9
|
+
let(:client) { Recommender::Client.connect(api_key)}
|
10
|
+
let(:param_similar_users) {{engine_name: 'engine1',id: 1,n: 4}}
|
11
|
+
let(:param_similar_items) {{type: 'article',engine_name: 'engine2',id: 1,n: 4}}
|
12
|
+
|
13
|
+
describe '.connect' do
|
14
|
+
context "When a application attemps to log in" do
|
15
|
+
it{expect(Recommender::Client.connect(api_key)).to be_a Recommender::Client}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#add_item' do
|
20
|
+
context "When an application wants to add new data" do
|
21
|
+
it{expect(client.add_item(add_param)).to be_truthy}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#load_data' do
|
26
|
+
context "When an application wants to load data" do
|
27
|
+
it{expect(client.load_data(param_load('engine1'))).to be_truthy}
|
28
|
+
it{expect(client.load_data(param_load('engine2'))).to be_truthy}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#get_similar_users' do
|
33
|
+
context "When an application wants to get similar users" do
|
34
|
+
it{expect(client.get_similar_users(param_similar_users)).to_not eq nil}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#get_similar_items' do
|
39
|
+
context "When an application wants to get similar items" do
|
40
|
+
it{expect(client.get_similar_items(param_similar_items)).to_not eq nil}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#get_recommandations' do
|
45
|
+
context "When an application wants to get recommandations for a user" do
|
46
|
+
it{expect(client.get_recommendations(param_similar_items)).to_not eq nil}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
end
|
data/spec/handler_spec.rb
CHANGED
@@ -93,7 +93,9 @@ describe Handler do
|
|
93
93
|
|
94
94
|
describe '.load_data' do
|
95
95
|
context "When data are successfully load" do
|
96
|
-
|
96
|
+
populate_engine
|
97
|
+
it {expect(Handler.load_data(params_to_load1)).to eq true }
|
98
|
+
it {expect(Handler.load_data(params_to_load2)).to eq true }
|
97
99
|
end
|
98
100
|
end
|
99
101
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,28 +1,50 @@
|
|
1
1
|
require 'client_api'
|
2
|
+
require 'uuid'
|
2
3
|
require 'redis'
|
3
4
|
require 'json'
|
5
|
+
require 'rest-client'
|
6
|
+
require_relative '../lib/conf/conf_api'
|
4
7
|
|
5
8
|
|
6
9
|
|
7
10
|
def engine_data_sample
|
8
11
|
engine_data = []
|
9
12
|
engine_data << {customer_id: 1,item_id: 1,preference: 1}
|
10
|
-
|
13
|
+
200.times do
|
11
14
|
engine_data << {customer_id: rand(1..5),item_id: rand(1..10),preference: rand(1..5)}
|
12
15
|
end
|
16
|
+
100.times do
|
17
|
+
engine_data << {customer_id: rand(2..5),item_id: rand(10..20),preference: rand(1..5)}
|
18
|
+
end
|
13
19
|
engine_data
|
14
20
|
end
|
15
21
|
|
16
22
|
def params_user_based
|
17
|
-
{engine_name: '
|
23
|
+
{engine_name: 'engUser',api_key: '926eb0b0-e99d-0131-7e06-080027ab4d7b',token: 'token1',id: 1,nb_wanted: 2}
|
18
24
|
end
|
19
25
|
|
20
26
|
def params_item_based
|
21
|
-
{engine_name: '
|
27
|
+
{engine_name: 'engItem',api_key: '926eb0b0-e99d-0131-7e06-080027ab4d7b',token: 'token2',id: 1,nb_wanted: 2}
|
28
|
+
end
|
29
|
+
|
30
|
+
def populate_engine(api_key)
|
31
|
+
nb = rand(10000)
|
32
|
+
RestClient.post(API_SERVER+'/restricted/object/new',{model_name: 'Application',content: {name: "app",api_key: api_key,url: "http://www.app#{nb}.com",user_id: 1}.to_json})
|
33
|
+
#RestClient.post(API_SERVER+'/restricted/object/new',{model_name: 'Engine',content: {name: "engUser",engine_conf: {similarity_metric: "PearsonCorrelationSimilarity",reco_algo:"GenericUserBasedRecommender", actions: [{action: "Like",score: "1"}],neighborhood: 6}.to_json,application_id: 391}.to_json})
|
34
|
+
#RestClient.post(API_SERVER+'/restricted/object/new',{model_name: 'Engine',content: {name: "engItem",engine_conf: {similarity_metric: "PearsonCorrelationSimilarity",reco_algo:"GenericItemBasedRecommender", actions: [{action: "Like",score: "1"}],neighborhood: 6}.to_json,application_id: 391}.to_json})
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def params_to_load1
|
39
|
+
{engine_name: 'engItem',data: engine_data_sample.to_json,api_key: '926eb0b0-e99d-0131-7e06-080027ab4d7b',token: 'token2'}
|
22
40
|
end
|
23
41
|
|
24
42
|
|
25
|
-
def
|
26
|
-
{engine_name: '
|
43
|
+
def params_to_load2
|
44
|
+
{engine_name: 'engUser',data: engine_data_sample.to_json,api_key: '926eb0b0-e99d-0131-7e06-080027ab4d7b',token: 'token2'}
|
45
|
+
end
|
46
|
+
|
47
|
+
def param_load(engine_name)
|
48
|
+
{engine_name: engine_name,data: engine_data_sample.to_json}
|
27
49
|
end
|
28
50
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: client_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 'fabira && atakem '
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,9 +96,11 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- client_api.gemspec
|
98
98
|
- lib/client_api.rb
|
99
|
+
- lib/client_api/client.rb
|
99
100
|
- lib/client_api/handler.rb
|
100
101
|
- lib/client_api/version.rb
|
101
102
|
- lib/conf/conf_api.rb
|
103
|
+
- spec/client_spec.rb
|
102
104
|
- spec/handler_spec.rb
|
103
105
|
- spec/spec_helper.rb
|
104
106
|
homepage: ''
|
@@ -126,5 +128,6 @@ signing_key:
|
|
126
128
|
specification_version: 4
|
127
129
|
summary: '"module client-API"'
|
128
130
|
test_files:
|
131
|
+
- spec/client_spec.rb
|
129
132
|
- spec/handler_spec.rb
|
130
133
|
- spec/spec_helper.rb
|