You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@predictionio.apache.org by do...@apache.org on 2017/06/05 20:40:52 UTC
incubator-predictionio-sdk-ruby git commit: Add ability to push
events to specific channels
Repository: incubator-predictionio-sdk-ruby
Updated Branches:
refs/heads/develop 92ba576f5 -> f71cf0e66
Add ability to push events to specific channels
* Use Ruby >=2
* Add ability to pass options with symbol keys for create event method
Closes #19
Project: http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/commit/f71cf0e6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/tree/f71cf0e6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/diff/f71cf0e6
Branch: refs/heads/develop
Commit: f71cf0e6682d79528fcbc4a31ae93db5d8b5f28e
Parents: 92ba576
Author: Zh Kostev <zh...@gmail.com>
Authored: Mon Jun 5 13:39:58 2017 -0700
Committer: Donald Szeto <ds...@salesforce.com>
Committed: Mon Jun 5 13:39:58 2017 -0700
----------------------------------------------------------------------
.gitignore | 2 ++
Gemfile | 4 ++-
README.md | 13 ++++++++
lib/predictionio/event_client.rb | 14 ++++++---
lib/predictionio/version.rb | 2 +-
predictionio.gemspec | 2 +-
spec/predictionio_spec.rb | 56 +++++++++++++++++++++++++++++++++--
spec/spec_helper.rb | 10 +++++++
8 files changed, 93 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index dca2977..29abd6e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
/coverage
/doc
+.rvmrc
+/.idea
/pkg
*.gem
Gemfile.lock
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/Gemfile
----------------------------------------------------------------------
diff --git a/Gemfile b/Gemfile
index fcf42f9..9bb1435 100644
--- a/Gemfile
+++ b/Gemfile
@@ -8,4 +8,6 @@ end
group :doc do
gem 'rdoc', '~> 4.0.0'
-end
\ No newline at end of file
+end
+
+gem 'activesupport', '~> 4.2'
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 77655dd..56828ef 100644
--- a/README.md
+++ b/README.md
@@ -71,6 +71,19 @@ client.create_event(
)
```
+### Create a $set` item event and send it to Event Server to specific channel
+
+*NOTE:* channels are supported in PIO version >= 0.9.2 only. Channel must be created first.
+
+```ruby
+client.create_event(
+ '$set',
+ 'item',
+ item_id,
+ { 'properties' => { 'categories' => ['Category 1', 'Category 2'], 'channel' => 'test-channel'} }
+)
+```
+
### Create a user 'rate' item event and send it to Event Server
```ruby
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/lib/predictionio/event_client.rb
----------------------------------------------------------------------
diff --git a/lib/predictionio/event_client.rb b/lib/predictionio/event_client.rb
index 2fda24e..75428e8 100644
--- a/lib/predictionio/event_client.rb
+++ b/lib/predictionio/event_client.rb
@@ -6,6 +6,7 @@
require 'date'
require "net/http"
+require 'active_support/core_ext/hash'
module PredictionIO
# This class contains methods that interface with the PredictionIO Event
@@ -110,14 +111,19 @@ module PredictionIO
#
# See also #create_event.
def acreate_event(event, entity_type, entity_id, optional = {})
- h = optional
+ h = optional.with_indifferent_access
h.key?('eventTime') || h['eventTime'] = DateTime.now.to_s
h['event'] = event
h['entityType'] = entity_type
h['entityId'] = entity_id
- @http.apost(PredictionIO::AsyncRequest.new(
- "/events.json?accessKey=#{@access_key}", h.to_json
- ))
+ @http.apost(PredictionIO::AsyncRequest.new(event_path_with_main_params(h), h.except(:channel).to_json))
+ end
+
+ def event_path_with_main_params(options)
+ result = "/events.json?accessKey=#{@access_key}"
+ result += "&channel=#{options[:channel]}" if options[:channel]
+
+ result
end
# :category: Asynchronous Methods
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/lib/predictionio/version.rb
----------------------------------------------------------------------
diff --git a/lib/predictionio/version.rb b/lib/predictionio/version.rb
index cb49861..f0d9c70 100644
--- a/lib/predictionio/version.rb
+++ b/lib/predictionio/version.rb
@@ -1,3 +1,3 @@
module PredictionIO
- VERSION = '0.9.6'
+ VERSION = '0.9.7'
end
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/predictionio.gemspec
----------------------------------------------------------------------
diff --git a/predictionio.gemspec b/predictionio.gemspec
index 13b651d..4a86f4e 100644
--- a/predictionio.gemspec
+++ b/predictionio.gemspec
@@ -16,7 +16,7 @@ EOF
s.email = 'support@prediction.io'
s.homepage = 'http://prediction.io/'
s.platform = Gem::Platform::RUBY
- s.required_ruby_version = '>= 1.9.3'
+ s.required_ruby_version = '>= 2.0'
s.files = Dir[File.join('lib', '**', '**')]
s.add_runtime_dependency 'json', '~> 1.8'
end
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/spec/predictionio_spec.rb
----------------------------------------------------------------------
diff --git a/spec/predictionio_spec.rb b/spec/predictionio_spec.rb
index d400898..91d8704 100644
--- a/spec/predictionio_spec.rb
+++ b/spec/predictionio_spec.rb
@@ -1,16 +1,66 @@
require 'predictionio'
require 'spec_helper'
-event_client = PredictionIO::EventClient.new(1, 'http://fakeapi.com:7070', 10)
-engine_client = PredictionIO::EngineClient.new('http://fakeapi.com:8000', 10)
-
describe PredictionIO do
+ let(:access_key) { 1 }
+ let(:event_client) { PredictionIO::EventClient.new(access_key, 'http://fakeapi.com:7070', 10) }
+ let(:engine_client) { PredictionIO::EngineClient.new('http://fakeapi.com:8000', 10) }
+
describe 'Events API' do
it 'create_event should create an event' do
response = event_client.create_event('register', 'user', 'foobar')
expect(response.code).to eq('201')
end
+ context 'with http stub (for channel test)' do
+ before(:each) do
+ success_response = Net::HTTPCreated.new('HTTP/1.1', '201', 'Created')
+ success_response.body = JSON.generate(eventId: 'deadbeef00')
+ expect_any_instance_of(PredictionIO::Connection).to receive(:apost).and_return(double(get: success_response))
+ end
+
+ it 'create_event should have channel option (symbol)' do
+ expect(PredictionIO::AsyncRequest).
+ to receive(:new).with("/events.json?accessKey=#{access_key}&channel=test-channel",
+ {
+ "eventTime" => "2017-03-22T12:26:35+03:00", "event" => "$set",
+ "entityType" => "Session", "entityId" => "42"
+ }.to_json)
+ event_client.create_event('$set', 'Session', '42',
+ {channel: 'test-channel', 'eventTime' => "2017-03-22T12:26:35+03:00"})
+ end
+
+ it 'create_event should process channel option (string)' do
+ expect(PredictionIO::AsyncRequest).
+ to receive(:new).with("/events.json?accessKey=#{access_key}&channel=test-channel",
+ {
+ "eventTime" => "2017-03-22T12:26:35+03:00", "event" => "$set",
+ "entityType" => "Session", "entityId" => "42"
+ }.to_json)
+ response = event_client.create_event('$set', 'Session', '42',
+ { 'channel' => 'test-channel', 'eventTime' => "2017-03-22T12:26:35+03:00" })
+ expect(response.code).to eq('201')
+ end
+
+ it 'create_event should work without channel option' do
+ expect(PredictionIO::AsyncRequest).
+ to receive(:new).with("/events.json?accessKey=#{access_key}",
+ {
+ "eventTime" => "2017-03-22T12:26:35+03:00", "event" => "$set",
+ "entityType" => "Session", "entityId" => "42"
+ }.to_json)
+ response = event_client.create_event('$set', 'Session', '42',
+ { 'eventTime' => "2017-03-22T12:26:35+03:00" })
+ expect(response.code).to eq('201')
+ end
+ end
+
+ it 'create_event should post real request with channel option' do
+ response = event_client.create_event('$set', 'Session', '42',
+ { 'channel' => 'test-channel', 'eventTime' => "2017-03-22T12:26:35+03:00" })
+ expect(response.code).to eq('201')
+ end
+
it 'create_event should not raise an error' do
response = event_client.create_event('register', 'user', 'foobar')
expect{ response }.to_not raise_error
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-sdk-ruby/blob/f71cf0e6/spec/spec_helper.rb
----------------------------------------------------------------------
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index dd2a85b..11ab902 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -8,6 +8,16 @@ WebMock.disable_net_connect!(allow_localhost: true)
RSpec.configure do |config|
config.before(:each) do
# Events API
+ %w(
+ http://fakeapi.com:7070/events.json?accessKey=1&channel=test-channel
+ ).each do |url|
+ stub_request(:post, url)
+ .with(body: hash_including(event: '$set',
+ entityType: 'Session',
+ entityId: '42'))
+ .to_return(status: 201, body: JSON.generate(eventId: 'deadbeef00'))
+ end
+
stub_request(:post, 'http://fakeapi.com:7070/events.json?accessKey=1')
.with(body: hash_including(event: 'register',
entityType: 'user',