client_api 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 613b6c809bbc1c8824484363e7cfc55d80e56af5
4
+ data.tar.gz: fa94a013a97ac14b5eefde283215ab9f882dda80
5
+ SHA512:
6
+ metadata.gz: 39f2618c4436007325f036fc8152f977768be47ba8f02c38b9209ca162fbf389e1fe64e650de00162463a6bb0906f73d7c15b8ca99d804335b5500f4ecd3618e
7
+ data.tar.gz: d12647033ed5d4e0fdcc0553e056205af8421d43e55533ba6ba8cc390305642d852ef3894053d2c5699f1bc35adcad9ad8be7f61404c7084788b8439da2b71f4
data/.autotest ADDED
@@ -0,0 +1,2 @@
1
+ require 'autotest/fsevent'
2
+ require 'autotest/growl'
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gem "autotest"
3
+ gem "autotest-growl"
4
+ gem "ZenTest"
5
+ # Specify your gem's dependencies in client_api.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 fabira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ClientApi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'client_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install client_api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/client_api/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'client_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "client_api"
8
+ spec.version = ClientApi::VERSION
9
+ spec.authors = ["fabira && atakem "]
10
+ spec.email = ["fabira90@gmail.com,atacraft@gmail.com"]
11
+ spec.summary = %q{"module client-API"}
12
+ spec.description = %q{"Module that interact with the API"}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = ">= 1.9"
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "redis"
25
+ spec.add_development_dependency "rest-client"
26
+ spec.add_development_dependency "rspec"
27
+
28
+ end
@@ -0,0 +1,183 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+ require_relative '../conf/conf_api'
4
+
5
+ class Handler
6
+
7
+ def self.connect_app(app_params)
8
+ waited_keys = [:object,:option]
9
+ call_api(app_params,'/apps/connect','POST',waited_keys)
10
+ end
11
+
12
+ def self.load_data(params)
13
+ waited_keys = [:engine_name,:data]
14
+ resp = invoke('/apps/load_data','POST',params)
15
+ resp.code == 200 ? true : false
16
+ end
17
+
18
+ def self.similar_users(params)
19
+ waited_keys = [:engine_name,:user_id,:nb_of_similars]
20
+ resp = invoke('/users/similar','POST',params)
21
+ resp.code == 200 ? true : false
22
+ end
23
+
24
+ def self.similar_items(params)
25
+ waited_keys = [:engine_name,:item_id,:nb_of_similars]
26
+ resp = invoke('/items/similar','POST',params)
27
+ resp.code == 200 ? true : false
28
+ end
29
+
30
+ def self.recommend(params)
31
+ waited_keys = [:engine_name,:user_id,:nb_of_recommendations]
32
+ resp = invoke('/users/recommend','POST',params)
33
+ resp.code == 200 ? true : false
34
+ end
35
+
36
+ private #######################################
37
+
38
+ #launch a request to the api server
39
+ def self.get_response(request,url,param)
40
+ request[:post] ? response = RestClient.post(url,param) : response = RestClient.get(url, {:params => param})
41
+ response
42
+ end
43
+
44
+ #return the response's code
45
+ def self.get_code(response)
46
+ response.code
47
+ end
48
+
49
+ #return the correct url to the api server
50
+ def self.get_url(url)
51
+ return API_SERVER+url
52
+ end
53
+
54
+ #determines the restfull method
55
+ def self.get_request(type)
56
+ request = {post: false,get: false}
57
+ type == 'POST' ? request[:post] = true : request[:get] = true
58
+ request
59
+ end
60
+
61
+ #invoke the api et return the response
62
+ def self.invoke(uri,req,params)
63
+ request = get_request(req)
64
+ url = get_url(uri)
65
+ response = get_response(request,url,params)
66
+ return response
67
+ end
68
+
69
+ #determines if the passed parameter is a hash
70
+ def self.valid_parameter?(param)
71
+ param.is_a?(Hash)
72
+ end
73
+
74
+ #determines if the waited keys are present in the parameter
75
+ def self.valid_keys?(param,waited_keys)
76
+ waited_keys.each do |k|
77
+ if !(param.has_key?(k))
78
+ return false
79
+ break
80
+ end
81
+ end
82
+ return true
83
+ end
84
+
85
+ #determines if the parameter's values are not empty Hash
86
+ def self.valid_values?(param)
87
+ parameter = {object: JSON.parse(param[:object],symbolize_names: true),option: JSON.parse(param[:option],symbolize_names: true)}
88
+ values = parameter.values
89
+ values.each do |v|
90
+ if !(v.is_a?(Hash)) || (v.empty?)
91
+ return false
92
+ break
93
+ end
94
+ end
95
+ return true
96
+ end
97
+
98
+ #determines if the option is valid
99
+ def self.valid_option?(option)
100
+ option = JSON.parse(option,symbolize_names: true)
101
+ waited_keys = [:user,:app]
102
+ if valid_keys?(option,waited_keys)
103
+ values = option.values
104
+ values.each do |v|
105
+ unless (v.is_a?(TrueClass) || v.is_a?(FalseClass))
106
+ return false
107
+ break
108
+ end
109
+ end
110
+ if (option[:user] && option[:app]) || (!option[:user] && !option[:app])
111
+ return false
112
+ else
113
+ return true
114
+ end
115
+ else
116
+ return false
117
+ end
118
+ end
119
+
120
+ #determines if the object's keys are valid
121
+ def self.valid_object_keys?(keys,object_keys)
122
+ object_keys.each do |k|
123
+ unless keys.include?(k)
124
+ return false
125
+ break
126
+ end
127
+ end
128
+ return true
129
+ end
130
+
131
+ #determines if the object's values are valid
132
+ def self.valid_object_values?(values)
133
+ values.each do |v|
134
+ if v.nil? || !(v.is_a?(String)) || v.empty?
135
+ return false
136
+ break
137
+ end
138
+ end
139
+ return true
140
+ end
141
+
142
+ #determines if the object is valid
143
+ def self.valid_param_object?(object_keys,keys,values)
144
+ response = true
145
+ if !(valid_object_keys?(keys,object_keys)) || !(valid_object_values?(values))
146
+ response = false
147
+ end
148
+ response
149
+ end
150
+
151
+ #determines if the object is valid according to the true option
152
+ def self.valid_object?(object,option)
153
+ object = JSON.parse(object,symbolize_names: true)
154
+ option = JSON.parse(option,symbolize_names: true)
155
+ keys = object.keys
156
+ values = object.values
157
+ user_keys = [:login,:password]
158
+ app_keys = [:api_key]
159
+ if option[:user]
160
+ return valid_param_object?(user_keys,keys,values)
161
+ else
162
+ return valid_param_object?(app_keys,keys,values)
163
+ end
164
+ end
165
+
166
+ #validate each method's parameter
167
+ def self.validate(param,waited_keys)
168
+ valid_parameter?(param) && valid_keys?(param,waited_keys) && valid_values?(param) && valid_option?(param[:option]) && valid_object?(param[:object],param[:option])
169
+ end
170
+
171
+ #invoke the api and analyze the response's code
172
+ def self.call_api(params,short_uri,rest_req,waited_keys)
173
+ if validate(params,waited_keys)
174
+ response = invoke(short_uri,rest_req,params)
175
+ get_code(response) == 200 ? true : false
176
+ else
177
+ false
178
+ end
179
+ rescue
180
+ false
181
+ end
182
+
183
+ end
@@ -0,0 +1,3 @@
1
+ module ClientApi
2
+ VERSION = "0.0.3"
3
+ end
data/lib/client_api.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "client_api/version"
2
+ require_relative 'client_api/handler'
3
+
4
+ module ClientApi
5
+
6
+ class Handler
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ API_SERVER = 'http://localhost:9295'
@@ -0,0 +1,109 @@
1
+ require_relative 'spec_helper'
2
+
3
+
4
+ describe Handler do
5
+ let(:app) {{ object: {api_key: 'an api_key'}.to_json, option: {user: false,app: true}.to_json }}
6
+ let(:redis) { Redis.current}
7
+ let(:user) {{ object: {login: 'login',password: 'password'}, option: {user: true,app: false} }}
8
+
9
+ before (:each) do
10
+ redis.del('applications')
11
+ redis.del('users')
12
+ end
13
+
14
+
15
+ describe '.connect_app' do
16
+
17
+ before (:each) do
18
+ redis.sadd('applications',app[:object])
19
+ redis.sadd('users',user[:object].to_json)
20
+ end
21
+
22
+ context "When an application attempts to log in" do
23
+ it { expect(Handler.connect_app(app)).to eq true }
24
+ end
25
+
26
+ context "When a inexistant app attemps to log in" do
27
+ bad_app = {object: {api_key: '013'}.to_json,option: {user: false,app: true}.to_json}
28
+ it { expect(Handler.connect_app(bad_app)).to eq false }
29
+ end
30
+
31
+ context "When the parameter is not a hash" do
32
+ it { expect(Handler.connect_app("coucou")).to eq false }
33
+ end
34
+
35
+ context "When the parameter has not valid keys" do
36
+ invalid_param = {obj: {api_key: 'ap'}.to_json,opti: {user: false,app: true}.to_json}
37
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
38
+ end
39
+
40
+ context "When the parameter's values are not hash" do
41
+ invalid_param = {object: "user",option: {user: false,app: true}.to_json}
42
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
43
+ end
44
+
45
+ context "When the parameter's values nil or empty" do
46
+ invalid_param = {object: {}.to_json,option: {user: false,app: true}.to_json}
47
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
48
+
49
+ invalid_param = {object: nil,option: nil}
50
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
51
+ end
52
+
53
+ context "When the option is invalid" do
54
+ invalid_param = {object: {api_key: 'a api_key'}.to_json,option: {use: false,ap: true}.to_json}
55
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
56
+
57
+ invalid_param = {object: {api_key: 'a api'}.to_json,option: {user: false,app: false}.to_json}
58
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
59
+
60
+ invalid_param = {object: {api_key: 'a api'}.to_json,option: {user: true,app: true}.to_json}
61
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
62
+
63
+ invalid_param = {object: {api_key: 'a api'}.to_json,option: {user: 'cou',app: true}.to_json}
64
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
65
+
66
+ invalid_param = {object: {api_key: 'a api'}.to_json,option: {user: false,app: nil}.to_json}
67
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
68
+ end
69
+
70
+ context "When the object is invalid" do
71
+ invalid_param = {object: {ap_key: 'a api'}.to_json,option: {user: false,app: true}.to_json}
72
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
73
+
74
+ invalid_param = {object: {api_key: nil}.to_json,option: {user: false,app: true}.to_json}
75
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
76
+
77
+ invalid_param = {object: {api_key: ''}.to_json,option: {user: false,app: true}.to_json}
78
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
79
+
80
+ invalid_param = {object: {api_key: 1}.to_json,option: {user: false,app: true}.to_json}
81
+ it { expect(Handler.connect_app(invalid_param)).to eq false }
82
+ end
83
+
84
+ end
85
+
86
+ describe '.load_data' do
87
+ context "When data are successfully load" do
88
+ it {expect(Handler.load_data(params_to_load)).to eq true }
89
+ end
90
+ end
91
+
92
+ describe '.similar_users' do
93
+ context "When similar users are found" do
94
+ it {expect(Handler.similar_users(params_user_based)).to eq true }
95
+ end
96
+ end
97
+
98
+ describe '.similar_items' do
99
+ context "When similar items are found" do
100
+ it {expect(Handler.similar_items(params_item_based)).to eq true }
101
+ end
102
+ end
103
+
104
+ describe '.recommend' do
105
+ context "When recommendations are sent" do
106
+ it {expect(Handler.recommend(params_item_based)).to eq true }
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,28 @@
1
+ require 'client_api'
2
+ require 'redis'
3
+ require 'json'
4
+
5
+
6
+
7
+ def engine_data_sample
8
+ engine_data = []
9
+ engine_data << {customer_id: 1,item_id: 1,preference: 1}
10
+ 100.times do
11
+ engine_data << {customer_id: rand(1..5),item_id: rand(1..10),preference: rand(1..5)}
12
+ end
13
+ engine_data
14
+ end
15
+
16
+ def params_user_based
17
+ {engine_name: 'eng869',api_key: '926eb0b0-e99d-0131-7e06-080027ab4d7b',id: 1,nb_wanted: 2}
18
+ end
19
+
20
+ def params_item_based
21
+ {engine_name: 'eng111',api_key: '926eb0b0-e99d-0131-7e06-080027ab4d7b',id: 1,nb_wanted: 2}
22
+ end
23
+
24
+
25
+ def params_to_load
26
+ {engine_name: 'eng111',data: engine_data_sample.to_json}
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: client_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - 'fabira && atakem '
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: '"Module that interact with the API"'
84
+ email:
85
+ - fabira90@gmail.com,atacraft@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".autotest"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - client_api.gemspec
98
+ - lib/client_api.rb
99
+ - lib/client_api/handler.rb
100
+ - lib/client_api/version.rb
101
+ - lib/conf/conf_api.rb
102
+ - spec/handler_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '1.9'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: '"module client-API"'
128
+ test_files:
129
+ - spec/handler_spec.rb
130
+ - spec/spec_helper.rb