simply_suggest 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57a9ee50d1b1ef3aaee9e99d59c987064134ff1d
4
- data.tar.gz: 68122da2a464064c89b189cc7e940ecd3bb9be5f
3
+ metadata.gz: 7c78798b5aff267bae7ade50c16084b536ce015f
4
+ data.tar.gz: a154d4338345e32eb951adc15dbec05392051831
5
5
  SHA512:
6
- metadata.gz: 7ac137b635018f9bfc7d7bf5d6439e9b858a2701719f9d074e7c0682a2f17420d7166ae43c2ace486edebc7590488e80fcc5d69963dd100c499487475343b88f
7
- data.tar.gz: a78ef3bef11be6d3f0ab02d123d376b00cb1417d971ff52edd89094d8014d5e4224be88bf35b95d93e44f290ac8729b64bd54ee1aa4794986a702a347838d650
6
+ metadata.gz: 315207c8ba09413717422f01e61d39e6b414dc0a9ddcb03a33e425b7fc383c5dbcfc8a888c9c3ebc6f95fe09f1fa641ab1a5da5f527006b2a0ef001ba9a1e1be
7
+ data.tar.gz: e6c30a30ad6227fb0f35cb09ff8e79d879e290b82c5e9b5fec8a0f2a19a5a7db8cdc1d8c2ba75cc246c962e4ada85beac1fad7060c0caf5c0bdc6c499a203b63
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+
8
+ notifications:
9
+ email:
10
+ on_success: change
11
+ on_failure: always
12
+
13
+ addons:
14
+ code_climate:
15
+ repo_token: 4e890328584d6a68879ecba465a2f7dd164c87e09452bbbec6dcf38b918b5321
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
- # BotDetection
1
+ # SimplySuggest
2
2
 
3
- Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
3
+ [![Build Status](https://travis-ci.org/SimplySuggest/simply_suggest_ruby.svg?branch=master)](https://travis-ci.org/SimplySuggest/simply_suggest_ruby)
4
+ [![security](https://hakiri.io/github/SimplySuggest/simply_suggest_ruby/master.svg)](https://hakiri.io/github/SimplySuggest/simply_suggest_ruby/master)
5
+ [![Code Climate](https://codeclimate.com/github/SimplySuggest/simply_suggest_ruby/badges/gpa.svg)](https://codeclimate.com/github/SimplySuggest/simply_suggest_ruby)
6
+
7
+ SimplySuggest implementation for Ruby.
8
+ You need an account at www.simply-suggest.com to use this gem.
9
+
10
+ Direct API-access to all the methods which are available and several helpers to get things a bit less complex.
4
11
 
5
12
  ## Installation
6
13
 
@@ -16,32 +23,89 @@ Or install it yourself as:
16
23
 
17
24
  $ gem install simply_suggest
18
25
 
19
- ## Changes
26
+ ## Configuration
20
27
 
21
- See the [CHANGELOG.md](CHANGELOG.md) file for details.
28
+ For rails put initialization into config/initializers/simply_suggest.rb
29
+
30
+ SimplySuggest.configure do |c|
31
+ c.secret_key = "YOUR SECRET KEY"
32
+ c.public_key = "YOUR PUBLIC KEY"
33
+ end
22
34
 
23
35
  ## Usage
24
36
 
25
37
  After installing the GEM you can use within your controllers or helpers the following methods
26
38
 
27
- # this will return an array of object ids which are related to this object
28
- recommendations_for object
39
+ This will return an array of object ids which are related to this object
40
+ recommendations_for @product
41
+ # => [1,2,3,4,5]
42
+
43
+ This will autoload all objects from your database
44
+
45
+ recommendations_for @product, load: true
46
+ # => [Product, Product, Product]
47
+
48
+ This will return an hash of data which are recommended for the user
49
+
50
+ user_recommendations current_user.id
51
+ # => [{ type: "article", id: 1 }, { type: "product", id: 1 }]
52
+
53
+ This will autoload the data from your database
54
+
55
+ user_recommendations current_user.id, load: true
56
+ # => [Product, Article, Product]
57
+
58
+ This will load the current trending objects
59
+
60
+ get_trending_objects "article", load: true
61
+ # => [Article, Article, Article]
62
+
63
+ You can use the search api accessing the following method in your controller
29
64
 
30
- # this will return an hash of data which are recommended for the user
31
- user_recommendations user_id
65
+ search_objects "lorem ipsum", "article", load: true
66
+ # => { results: [Article, Article, Article], facets: nil, conditions: nil }
67
+
68
+ You can specify facet fields
69
+
70
+ search_objects "lorem ipsum", "article", load: true, facets: [:category_id]
71
+ # => { results: [Article, Article, Article], facets: { category_id: { key: 4, doc_count: 2 } }, conditions: nil }
72
+
73
+ you can set autoload within the config to let load default to true
74
+
75
+ SimplySuggest.configure do |c|
76
+ c.autoload = false
77
+ end
78
+
79
+ All helpers will raise a ``SimplySuggest::Error`` if something went wrong so you easily catch them.
32
80
 
33
81
  ### JavaScript-Implementation
34
82
 
35
- # to use the javascript tracking methods you need to add this line to your <head> or footer
83
+ to use the javascript tracking methods you need to add this line to your <head> or footer
84
+
36
85
  <%= simply_suggest_script %>
37
86
 
38
- # then within the footer you can call this function to get the javascript calls
39
- <%= get_tracking_code user_id: unique_user_id, object_id: unique_object_id, object_type: object_type, event: "" %>
87
+ then somewhere on your side you can call this function to get the javascript calls
88
+
89
+ <%= get_tracking_code user_id: unique_user_id, object_id: unique_object_id, object_type: object_type, event: "view" %>
90
+
91
+ available event types are
92
+
93
+ :buy
94
+ :like
95
+ :dislike
96
+ :favorite
97
+ :view
98
+ :add-to-basket
99
+
100
+ You should use the primary key of your user or generate and send a unique user id which is saved to the session or cookie so you send always the same one
101
+
102
+ ## Changes
103
+
104
+ See the [CHANGELOG.md](CHANGELOG.md) file for details.
40
105
 
41
- ## Contributing
106
+ ## Contributing to SimplySuggest
42
107
 
43
- 1. Fork it ( https://github.com/SimplySuggest/simply_suggest_ruby.git/fork )
44
- 2. Create your feature branch (`git checkout -b my-new-feature`)
45
- 3. Commit your changes (`git commit -am 'Add some feature'`)
46
- 4. Push to the branch (`git push origin my-new-feature`)
47
- 5. Create a new Pull Request
108
+ 1. Check out the Master
109
+ 2. Fork the project.
110
+ 3. Start a feature/bugfix branch.
111
+ 4. Commit and push until you are happy with your contribution.
@@ -1,8 +1,20 @@
1
1
  module SimplySuggest
2
2
  class ApiRequest
3
3
  def get url, params = {}, headers = {}
4
+ execute_request :get, url, params, headers
5
+ end
6
+
7
+ def post url, params = {}, headers = {}
8
+ execute_request :post, url, params, headers
9
+ end
10
+
11
+ def patch url, params = {}, headers = {}
12
+ execute_request :patch, url, params, headers
13
+ end
14
+
15
+ def execute_request method, url, params = {}, headers = {}
4
16
  begin
5
- response = rest_client.get do |request|
17
+ response = rest_client.send(method) do |request|
6
18
  configure_request(url, request: request, params: params, headers: headers)
7
19
  end
8
20
  parse_response(response.body)
@@ -63,8 +75,8 @@ module SimplySuggest
63
75
 
64
76
  if parsed_response
65
77
  error_to_raise.body = parsed_response
66
- error_to_raise.title = parsed_response["title"] if parsed_response["title"]
67
- error_to_raise.detail = parsed_response["detail"] if parsed_response["detail"]
78
+ error_to_raise.error_code = parsed_response["error_code"] if parsed_response["error_code"]
79
+ error_to_raise.message = parsed_response["message"] if parsed_response["message"]
68
80
  end
69
81
 
70
82
  error_to_raise.status_code = error.response[:status]
@@ -25,12 +25,24 @@ module SimplySuggest
25
25
  # default: simply-suggest.com
26
26
  attr_accessor :api_domain
27
27
 
28
+ # Api Domain
29
+ #
30
+ # default: simply-suggest.com
31
+ attr_accessor :domain
32
+
33
+ # Autoload objects
34
+ #
35
+ # default: false
36
+ attr_accessor :autoload
37
+
28
38
  def initialize
29
39
  @secret_key = nil
30
40
  @public_key = nil
31
41
  @timeout = 2
32
42
  @api_version = "v1"
43
+ @domain = "simply-suggest.com"
33
44
  @api_domain = "http://v1.simply-suggest.com"
45
+ @autoload = false
34
46
  end
35
47
  end
36
48
 
@@ -8,14 +8,16 @@ module SimplySuggest
8
8
  # options : hash [optional]
9
9
  #
10
10
  def recommendations_for item, options = {}
11
- limit = options.delete(:limit) || 10
12
- page = options.delete(:page) || 1
11
+ options = options.reverse_merge(default_options)
12
+
13
13
  klass = item.is_a?(Hash) ? item.delete(:class) : item.class.to_s.downcase
14
14
  object_id = item.is_a?(Hash) ? item.delete(:object_id) : item.id
15
15
 
16
- data = request_object.object_types.send(klass).object.recommendations.page(page).limit(limit).get(object_id)
17
- return [] if data["record_predictions"].blank?
18
- data["record_predictions"].map { |d| d["recommendation_id"] }
16
+ data = request_object.object_types.send(klass).object.recommendations.page(options[:page]).limit(options[:limit]).get(object_id)
17
+ return [] if data["recommendations"].blank?
18
+ ids = data["recommendations"].map { |d| d["recommendation_id"] }
19
+ return item.class.where(id: ids) if options[:load]
20
+ ids
19
21
  end
20
22
 
21
23
  # returns recommendations for the user_id
@@ -24,22 +26,64 @@ module SimplySuggest
24
26
  # options : hash [optional]
25
27
  #
26
28
  def user_recommendations user_id, options = {}
27
- object_type = options.delete(:object_type) || options.delete(:class)
28
- limit = options.delete(:limit) || 10
29
- page = options.delete(:page) || 1
29
+ options = options.reverse_merge(default_options)
30
30
 
31
- if object_type.present?
32
- data = request_object.user.send(object_type).recommendations.page(page).limit(limit).get(user_id)
31
+ if options[:object_type].present?
32
+ data = request_object.user.send(options[:object_type]).recommendations.page(options[:page]).limit(options[:limit]).get(user_id)
33
33
  else
34
34
  data = request_object.user.recommendations.page(page).limit(limit).get(user_id)
35
35
  end
36
- return [] if data["user_predictions"].blank?
37
- data["user_predictions"].map { |d| { type: d["object_type"], id: d["recommendation_id"] } }
36
+ return [] if data["recommendations"].blank?
37
+ return data["recommendations"].map { |d| d["object_type"].classify.constantize.where(id: d["recommendation_id"]).first }.reject(&:nil?) if options[:load]
38
+ data["recommendations"].map { |d| { type: d["object_type"], id: d["recommendation_id"] } }
39
+ end
40
+
41
+ # returns trending data for the object type
42
+ #
43
+ # klass : string
44
+ # options : hash [optional]
45
+ #
46
+ def get_trending_objects klass, options = {}
47
+ options = options.reverse_merge(default_options)
48
+
49
+ data = request_object.object_type.trending.send(klass).page(page).limit(limit).get
50
+ return [] if data["trending"].blank?
51
+ return klass.classify.constantize.where(id: data["trending"].map { |d| d["external_id"] }) if options[:load]
52
+ data["trending"].map { |d| { type: d["object_type"], id: d["external_id"] } }
53
+ end
54
+
55
+ def search_objects query, klass, options = {}
56
+ options = options.reverse_merge(default_options)
57
+
58
+ request = request_object.object_type.search.send(klass).page(options[:page]).limit(options[:limit]).add_params(query: query)
59
+ request.add_param(:conditions, options[:conditions]) if options[:conditions].present?
60
+ request.add_param(:facets, options[:facets]) if options[:facets].present?
61
+
62
+ data = request.get
63
+ return [] if data["objects"].blank?
64
+ if options[:load]
65
+ values = {}
66
+ values[:results] = data["objects"].map { |d| d["object_type"].classify.constantize.where(id: d["external_id"]).first }.reject(&:nil?)
67
+ values[:facets] = data["facets"]
68
+ values[:conditions] = data["conditions"]
69
+
70
+ return values
71
+ end
72
+
73
+ data
38
74
  end
39
75
 
40
76
  protected
41
77
  def request_object
42
78
  @request_object ||= SimplySuggest::Request.new
43
79
  end
80
+
81
+ def default_options
82
+ {
83
+ limit: 10,
84
+ page: 1,
85
+ load: SimplySuggest.config.autoload
86
+ }
87
+ end
44
88
  end
45
89
  end
@@ -1,5 +1,5 @@
1
1
  module SimplySuggest
2
2
  class Error < StandardError
3
- attr_accessor :title, :detail, :body, :raw_body, :status_code
3
+ attr_accessor :error_code, :message, :body, :raw_body, :status_code
4
4
  end
5
5
  end
@@ -25,13 +25,39 @@ module SimplySuggest
25
25
  values
26
26
  end
27
27
 
28
+ def post *args
29
+ @path_parts += args.to_a
30
+ values = api_request.post(path, @params)
31
+ reset
32
+ values
33
+ end
34
+
35
+ def patch *args
36
+ @path_parts += args.to_a
37
+ values = api_request.patch(path, @params)
38
+ reset
39
+ values
40
+ end
41
+
28
42
  def limit num
29
- @params[:limit] = num
43
+ add_param :limit, num
30
44
  self
31
45
  end
32
46
 
33
47
  def page num
34
- @params[:page] = num
48
+ add_param :page, num
49
+ self
50
+ end
51
+
52
+ def add_param name, value
53
+ @params[name.to_sym] = value
54
+ self
55
+ end
56
+
57
+ def add_params params = {}
58
+ params.each_pair do |name, value|
59
+ @params[name.to_sym] = value
60
+ end
35
61
  self
36
62
  end
37
63
 
@@ -1,3 +1,3 @@
1
1
  module SimplySuggest
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -11,7 +11,7 @@ module SimplySuggest
11
11
  content_tag :script do
12
12
  "(function() {
13
13
  var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
14
- po.src = '//static.#{SimplySuggest.config.domain}/script/v2.js';
14
+ po.src = '//static.#{SimplySuggest.config.domain}/script/v1.js';
15
15
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
16
16
  })();".html_safe
17
17
  end
@@ -34,8 +34,12 @@ module SimplySuggest
34
34
  content_tag :script, script
35
35
  end
36
36
 
37
+ def track_click source_id, destination, user_id, options = {}
38
+ track_recommendation_click(source_id, destination.id, destination.class.to_s.downcase, user_id, options)
39
+ end
40
+
37
41
  def track_recommendation_click source_id, destination_id, user_id, klass, options = {}
38
- use_script = options[:script] == nil ? true : options[:script]
42
+ use_script = options[:script] == nil ? true : options[:script]
39
43
 
40
44
  script = "
41
45
  window.track_recommendation = window.track_recommendation || [];
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = SimplySuggest::VERSION
9
9
  spec.authors = ["Simply Suggest"]
10
10
  spec.email = ["info@simply-suggest.com"]
11
- spec.summary = %q{Api for www.simply-suggest.com here}
11
+ spec.summary = %q{Api for http://www.simply-suggest.com here}
12
12
  spec.homepage = "http://www.simply-suggest.com"
13
13
  spec.license = "MIT"
14
14
 
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency 'actionpack', '~> 3.0.0', '>= 3.0.0'
20
+ spec.add_dependency 'actionpack', '>= 3.0.0'
21
21
  spec.add_dependency 'faraday', "~> 0.9"
22
22
  spec.add_dependency 'multi_json', '~> 1.11'
23
23
 
@@ -1,30 +1,43 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class TestController < ActionController::Base
4
- def test1
5
- @bot_detector = BotDetection::Detector.new(user_agent: BotDetection::BotUserAgents.first, remote_ip: "127.0.0.1")
6
- end
7
-
8
- def test2
9
- @bot_detector = BotDetection::Detector.new(user_agent: "Googlebot/2.1 (+http://www.googlebot.com/bot.html)", remote_ip: "66.249.66.1")
10
- end
11
4
  end
12
5
 
13
6
  describe "Api::Calls" do
14
7
  before {
15
8
  SimplySuggest.configure do |c|
16
- c.secret_key = "secret-2"
17
- c.public_key = "public-2"
18
- c.api_domain = "http://v1.recommendation.dev"
9
+ c.secret_key = "58f38a8b-ca16-4d44-acc9-0a6f0bf0919f"
10
+ c.public_key = "cbb8f914-d89f-49ef-b732-8b0e133f6cb5"
19
11
  end
20
12
  }
21
13
 
22
14
  context 'api calls' do
23
- it 'check several api methods' do
15
+ it "creates an object_types and raises an error" do
16
+ expect { SimplySuggest::Request.object_type.create.add_params(name: "article").post }.to raise_exception(SimplySuggest::Error)
17
+ end
18
+
19
+ it "updates an object_type" do
20
+ result = SimplySuggest::Request.object_type.update.send("article").add_params(name: "article").patch
21
+ expect(result["error"]).to eq(false)
22
+ end
23
+
24
+ it "creates an object in the article object type" do
25
+ expect { SimplySuggest::Request.object_type.send("article").object.create.add_params(object_id: "1").post }.to raise_exception(SimplySuggest::Error)
26
+ end
27
+
28
+ it "updates an object with some data" do
29
+ result = SimplySuggest::Request.object_type.send("article").object.update.add_params(data: { title: "lorem", description: "test" }).patch(1)
30
+ expect(result["error"]).to eq(false)
31
+ end
32
+
33
+ it 'checks object type api methods' do
24
34
  expect(SimplySuggest::Request.object_types.get.length).to be > 0
25
35
  expect(SimplySuggest::Request.object_types.limit(1).get.length).to be == 1
26
- expect(SimplySuggest::Request.object_types.album.objects.get.length).to be > 0
27
- expect(SimplySuggest::Request.object_types.album.object.recommendations.get(1026).length).to be > 0
36
+ end
37
+
38
+ it "checks object api methods" do
39
+ expect(SimplySuggest::Request.object_type.article.get.length).to be > 0
40
+ expect { SimplySuggest::Request.object_types.article.object.recommendations.get(1026) }.to raise_exception(SimplySuggest::Error)
28
41
  end
29
42
  end
30
43
 
@@ -33,15 +46,20 @@ describe "Api::Calls" do
33
46
  expect(ActionController::Base.included_modules).to include(SimplySuggest::ControllerHelper)
34
47
  end
35
48
 
36
- it 'should test controller helpers with object recommendations' do
37
- expect(TestController.new.recommendations_for(class: "album", object_id: 1026).length).to be > 0
38
- expect { TestController.new.recommendations_for(class: "album", object_id: 54684) }.to raise_exception(SimplySuggest::Error)
49
+ it "tests search methods" do
50
+ result = TestController.new.search_objects("test", "article")
51
+ expect(result["objects"].length).to eq(1)
39
52
  end
40
53
 
41
- it 'should test controller helpers with user recommendations' do
42
- expect(TestController.new.user_recommendations("ffc0635b-e32b-4a3d-a5b6-2703e08bdc14").length).to be > 0
43
- expect(TestController.new.user_recommendations("ffc0635b-e32b-4a3d-a5b6-2703e08bdc14", object_type: "series").length).to be > 0
44
- expect(TestController.new.user_recommendations("ffc0635b-e32b-4a3d-a5b6-2703e08bdc14", object_type: "album").length).to be == 0
45
- end
54
+ # it 'should test controller helpers with object recommendations' do
55
+ # expect(TestController.new.recommendations_for(class: "article", object_id: 1).length).to be > 0
56
+ # expect { TestController.new.recommendations_for(class: "article", object_id: 123) }.to raise_exception(SimplySuggest::Error)
57
+ # end
58
+
59
+ # it 'should test controller helpers with user recommendations' do
60
+ # expect(TestController.new.user_recommendations("ffc0635b-e32b-4a3d-a5b6-2703e08bdc14").length).to be > 0
61
+ # expect(TestController.new.user_recommendations("ffc0635b-e32b-4a3d-a5b6-2703e08bdc14", object_type: "series").length).to be > 0
62
+ # expect(TestController.new.user_recommendations("ffc0635b-e32b-4a3d-a5b6-2703e08bdc14", object_type: "album").length).to be == 0
63
+ # end
46
64
  end
47
65
  end
metadata CHANGED
@@ -1,22 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simply_suggest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simply Suggest
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-19 00:00:00.000000000 Z
11
+ date: 2015-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 3.0.0
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
19
  version: 3.0.0
@@ -24,9 +21,6 @@ dependencies:
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: 3.0.0
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
26
  version: 3.0.0
@@ -108,6 +102,7 @@ extensions: []
108
102
  extra_rdoc_files: []
109
103
  files:
110
104
  - ".gitignore"
105
+ - ".travis.yml"
111
106
  - CHANGELOG.md
112
107
  - Gemfile
113
108
  - LICENSE.txt
@@ -147,5 +142,5 @@ rubyforge_project:
147
142
  rubygems_version: 2.4.6
148
143
  signing_key:
149
144
  specification_version: 4
150
- summary: Api for www.simply-suggest.com here
145
+ summary: Api for http://www.simply-suggest.com here
151
146
  test_files: []