vero 0.3.2 → 0.4

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.
@@ -0,0 +1,25 @@
1
+ module Vero
2
+ module API
3
+ module Users
4
+ class TrackAPI < BaseAPI
5
+ def url
6
+ "#{@domain}/api/v2/users/track.json"
7
+ end
8
+
9
+ def request
10
+ RestClient.post(url, @options)
11
+ end
12
+
13
+ def validate!
14
+ result = true
15
+ result &&= options[:email].to_s.blank? == false
16
+ result &&= (options[:data].nil? || options[:data].is_a?(Hash))
17
+
18
+ unless result
19
+ raise ArgumentError.new(:email => options[:email], :data => options[:data])
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module Vero
2
+ module API
3
+ module Users
4
+ class UnsubscribeAPI < BaseAPI
5
+ def url
6
+ "#{@domain}/api/v2/users/unsubscribe.json"
7
+ end
8
+
9
+ def request
10
+ RestClient.post(url, @options)
11
+ end
12
+
13
+ def validate!
14
+ result = true
15
+ result &&= options[:email].to_s.blank? == false
16
+
17
+ unless result
18
+ raise ArgumentError.new(:email => options[:email])
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
data/lib/vero/config.rb CHANGED
@@ -27,7 +27,11 @@ module Vero
27
27
  end
28
28
 
29
29
  def domain
30
- @domain || 'www.getvero.com'
30
+ if @domain.blank?
31
+ 'https://www.getvero.com'
32
+ else
33
+ @domain =~ /http[s]?\:\/\/.+/ ? @domain : "http://#{@domain}"
34
+ end
31
35
  end
32
36
 
33
37
  def auth_token
data/lib/vero/context.rb CHANGED
@@ -1,7 +1,3 @@
1
- require 'json'
2
- require 'delayed_job'
3
- require 'vero/jobs/rest_post_job'
4
-
5
1
  module Vero
6
2
  class Context
7
3
  attr_accessor :config, :subject
@@ -46,74 +42,73 @@ module Vero
46
42
  @config.configured?
47
43
  end
48
44
 
49
- def track(event_name, event_data = {})
50
- validate_configured!
51
- validate_track_params!(event_name, event_data)
45
+ ### API methods
52
46
 
53
- request_params = @config.request_params
54
- request_params.merge!({:event_name => event_name, :identity => subject.to_vero, :data => event_data})
47
+ def track!(event_name, event_data)
48
+ validate_configured!
55
49
 
56
- method = if @config.async
57
- :post_later
58
- else
59
- :post_now
50
+ identity = subject.to_vero
51
+ options = @config.request_params
52
+ options.merge!(:data => event_data, :event_name => event_name, :identity => identity)
53
+
54
+ unless @config.disabled
55
+ Vero::Sender.send Vero::API::Events::TrackAPI, @config.async, @config.domain, options
60
56
  end
61
- self.send(method, "http://#{@config.domain}/api/v1/track.json", request_params, 'track')
62
57
  end
63
58
 
64
59
  def identify!
65
60
  validate_configured!
66
-
61
+
67
62
  data = subject.to_vero
68
- request_params = @config.request_params
69
- request_params.merge!({:email => data[:email], :data => data})
63
+ options = @config.request_params
64
+ options.merge!(:email => data[:email], :data => data)
70
65
 
71
- method = if @config.async
72
- :post_later
73
- else
74
- :post_now
66
+ unless @config.disabled
67
+ Vero::Sender.send Vero::API::Users::TrackAPI, @config.async, @config.domain, options
75
68
  end
76
- self.send(method, "http://#{@config.domain}/api/v1/user.json", request_params, 'identify!')
77
69
  end
78
70
 
79
- private
80
- def post_now(url, params, method_name)
71
+ def update_user!(email = nil)
72
+ validate_configured!
73
+
74
+ changes = subject.to_vero
75
+ options = @config.request_params
76
+ options.merge!(:email => (email || changes[:email]), :changes => changes)
77
+
81
78
  unless @config.disabled
82
- Vero::Jobs::RestPostJob.new(url, params).perform
83
- Vero::App.log(self, "method: #{method_name}, params: #{params.to_json}, response: job performed")
79
+ Vero::Sender.send Vero::API::Users::EditAPI, @config.async, @config.domain, options
84
80
  end
85
- 'success'
86
- rescue => e
87
- Vero::App.log(self, "method: #{method_name}, params: #{params.to_json} error: #{e.message}")
88
81
  end
89
82
 
90
- def post_later(url, params, method_name)
83
+ def update_user_tags!(add = [], remove = [])
84
+ validate_configured!
85
+
86
+ identity = subject.to_vero
87
+ options = @config.request_params
88
+ options.merge!(:email => identity[:email], :add => add, :remove => remove)
89
+
91
90
  unless @config.disabled
92
- ::Delayed::Job.enqueue Vero::Jobs::RestPostJob.new(url, params)
93
- Vero::App.log(self, "method: #{method_name}, params: #{params.to_json}, response: delayed job queued")
91
+ Vero::Sender.send Vero::API::Users::EditTagsAPI, @config.async, @config.domain, options
94
92
  end
95
- 'success'
96
- rescue => e
97
- if e.message == "Could not find table 'delayed_jobs'"
98
- raise "To send ratings asynchronously, you must configure delayed_job. Run `rails generate delayed_job:active_record` then `rake db:migrate`."
99
- else
100
- raise e
93
+ end
94
+
95
+ def unsubscribe!
96
+ validate_configured!
97
+
98
+ identity = subject.to_vero
99
+ options = @config.request_params
100
+ options.merge!(:email => identity[:email])
101
+
102
+ unless @config.disabled
103
+ Vero::Sender.send Vero::API::Users::UnsubscribeAPI, @config.async, @config.domain, options
101
104
  end
102
105
  end
103
106
 
107
+ private
104
108
  def validate_configured!
105
109
  unless @config.configured?
106
110
  raise "You must configure the 'vero' gem. Visit https://github.com/semblancesystems/vero for more details."
107
111
  end
108
112
  end
109
-
110
- def validate_track_params!(event_name, event_data)
111
- result = true
112
-
113
- result &&= event_name.kind_of?(String) && !event_name.blank?
114
- result &&= event_data.nil? || event_data.kind_of?(Hash)
115
-
116
- raise ArgumentError.new({:event_name => event_name, :event_data => event_data}) unless result
117
- end
118
113
  end
119
114
  end
@@ -0,0 +1,22 @@
1
+ module Vero
2
+ class Sender
3
+ def self.senders
4
+ @senders ||= {
5
+ true => Vero::Senders::Thread,
6
+ false => Vero::Senders::Base,
7
+ :none => Vero::Senders::Base,
8
+ :thread => Vero::Senders::Thread,
9
+ :delayed_job => Vero::Senders::DelayedJob
10
+ }
11
+ end
12
+
13
+ def self.send(api_class, sender_strategy, domain, options)
14
+ sender_class = self.senders.fetch(sender_strategy) { self.senders[false] }
15
+ (sender_class.new).call(api_class, domain, options)
16
+ rescue => e
17
+ Vero::App.log(self.new, "method: #{api_class.name}, options: #{options.to_json}, error: #{e.message}")
18
+ raise e
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module Vero
2
+ module Senders
3
+ class Base
4
+ def call(api_class, domain, options)
5
+ response = api_class.perform(domain, options)
6
+ Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: job performed")
7
+ response
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Vero
2
+ module Senders
3
+ class DelayedJob
4
+ def call(api_class, domain, options)
5
+ response = ::Delayed::Job.enqueue api_class.new(domain, options)
6
+ Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: delayed job queued")
7
+ response
8
+ rescue => e
9
+ if e.message == "Could not find table 'delayed_jobs'"
10
+ raise "To send ratings asynchronously, you must configure delayed_job. Run `rails generate delayed_job:active_record` then `rake db:migrate`."
11
+ else
12
+ raise e
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'girl_friday'
2
+
3
+ module Vero
4
+ module Senders
5
+ class Thread
6
+ VERO_SENDER_QUEUE ||= ::GirlFriday::WorkQueue.new(:vero_queue, :size => 1) do |msg|
7
+ api_class = msg[:api_class]
8
+ domain = msg[:domain]
9
+ options = msg[:options]
10
+
11
+ begin
12
+ api_class.perform(domain, options)
13
+ Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: job performed")
14
+ rescue => e
15
+ Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: #{e.message}")
16
+ end
17
+ end
18
+
19
+ def call(api_class, domain, options)
20
+ !!VERO_SENDER_QUEUE.push(:api_class => api_class, :domain => domain, :options => options)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -27,7 +27,8 @@ module Vero
27
27
  def to_vero
28
28
  klass = self.class
29
29
  result = klass.trackable_map.inject({}) do |hash, symbol|
30
- hash[symbol] = self.send(symbol)
30
+ t = respond_to?(symbol) ? send(symbol) : nil
31
+ hash[symbol] = t unless t.nil?
31
32
  hash
32
33
  end
33
34
 
@@ -2,7 +2,11 @@ module Vero
2
2
  module Trackable
3
3
  module Interface
4
4
  def track(event_name, event_data = {})
5
- with_default_vero_context.track(event_name, event_data)
5
+ track!(event_name, event_data)
6
+ end
7
+
8
+ def track!(event_name, event_data = {})
9
+ with_default_vero_context.track!(event_name, event_data)
6
10
  end
7
11
 
8
12
  def identify!
data/lib/vero/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vero
2
- VERSION = '0.3.2'
2
+ VERSION = '0.4'
3
3
  end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vero::API::Events::TrackAPI do
4
+ subject { Vero::API::Events::TrackAPI.new('https://www.getvero.com', {}) }
5
+ it "should inherit from Vero::API::BaseCaller" do
6
+ subject.should be_a(Vero::API::BaseAPI)
7
+ end
8
+
9
+ it "should map to current version of Vero API" do
10
+ subject.send(:url).should == "https://www.getvero.com/api/v2/events/track.json"
11
+ end
12
+
13
+ subject { Vero::API::Events::TrackAPI.new('https://www.getvero.com', {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => 'test_event'}) }
14
+ describe :validate! do
15
+ it "should raise an error if test_event is a blank String" do
16
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => nil}
17
+ subject.options = options
18
+ expect { subject.send(:validate!) }.to raise_error(ArgumentError)
19
+
20
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => 'test_event'}
21
+ subject.options = options
22
+ expect { subject.send(:validate!) }.to_not raise_error(ArgumentError)
23
+ end
24
+
25
+ it "should raise an error if data is not either nil or a Hash" do
26
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => 'test_event', :data => []}
27
+ subject.options = options
28
+ expect { subject.send(:validate!) }.to raise_error(ArgumentError)
29
+
30
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => 'test_event', :data => nil}
31
+ subject.options = options
32
+ expect { subject.send(:validate!) }.to_not raise_error(ArgumentError)
33
+
34
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => 'test_event', :data => {}}
35
+ subject.options = options
36
+ expect { subject.send(:validate!) }.to_not raise_error(ArgumentError)
37
+ end
38
+ end
39
+
40
+ describe :request do
41
+ it "should send a request to the Vero API" do
42
+ RestClient.should_receive(:post).with("https://www.getvero.com/api/v2/events/track.json", {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :event_name => 'test_event'})
43
+ RestClient.stub(:post).and_return(200)
44
+ subject.send(:request)
45
+ end
46
+ end
47
+
48
+ describe "integration test" do
49
+ it "should not raise any errors" do
50
+ RestClient.stub(:post).and_return(200)
51
+ expect { subject.perform }.to_not raise_error
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vero::API::Users::EditAPI do
4
+ subject { Vero::API::Users::EditAPI.new('https://www.getvero.com', {}) }
5
+ it "should inherit from Vero::API::BaseCaller" do
6
+ subject.should be_a(Vero::API::BaseAPI)
7
+ end
8
+
9
+ it "should map to current version of Vero API" do
10
+ subject.send(:url).should == "https://www.getvero.com/api/v2/users/edit.json"
11
+ end
12
+
13
+ subject { Vero::API::Users::EditAPI.new('https://www.getvero.com', {:auth_token => 'abcd', :email => 'test@test.com', :changes => { :email => 'test@test.com' }}) }
14
+ describe :validate! do
15
+ end
16
+
17
+ describe :request do
18
+ it "should send a request to the Vero API" do
19
+ RestClient.should_receive(:put).with("https://www.getvero.com/api/v2/users/edit.json", {:auth_token => 'abcd', :email => 'test@test.com', :changes => { :email => 'test@test.com' }})
20
+ RestClient.stub(:put).and_return(200)
21
+ subject.send(:request)
22
+ end
23
+ end
24
+
25
+ describe "integration test" do
26
+ it "should not raise any errors" do
27
+ RestClient.stub(:put).and_return(200)
28
+ expect { subject.perform }.to_not raise_error
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vero::API::Users::EditTagsAPI do
4
+ subject { Vero::API::Users::EditTagsAPI.new('https://www.getvero.com', {}) }
5
+ it "should inherit from Vero::API::BaseCaller" do
6
+ subject.should be_a(Vero::API::BaseAPI)
7
+ end
8
+
9
+ it "should map to current version of Vero API" do
10
+ subject.send(:url).should == "https://www.getvero.com/api/v2/users/tags/edit.json"
11
+ end
12
+
13
+ subject { Vero::API::Users::EditTagsAPI.new('https://www.getvero.com', {:auth_token => 'abcd', :email => 'test@test.com', :add => ["test"]}) }
14
+ describe :validate! do
15
+ end
16
+
17
+ describe :request do
18
+ it "should send a request to the Vero API" do
19
+ RestClient.should_receive(:put).with("https://www.getvero.com/api/v2/users/tags/edit.json", {:auth_token => 'abcd', :email => 'test@test.com', :add => ["test"]})
20
+ RestClient.stub(:put).and_return(200)
21
+ subject.send(:request)
22
+ end
23
+ end
24
+
25
+ describe "integration test" do
26
+ it "should not raise any errors" do
27
+ RestClient.stub(:put).and_return(200)
28
+ expect { subject.perform }.to_not raise_error
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vero::API::Users::TrackAPI do
4
+ subject { Vero::API::Users::TrackAPI.new('https://www.getvero.com', {}) }
5
+ it "should inherit from Vero::API::BaseCaller" do
6
+ subject.should be_a(Vero::API::BaseAPI)
7
+ end
8
+
9
+ it "should map to current version of Vero API" do
10
+ subject.send(:url).should == "https://www.getvero.com/api/v2/users/track.json"
11
+ end
12
+
13
+ subject { Vero::API::Users::TrackAPI.new('https://www.getvero.com', {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => 'test@test.com'}) }
14
+ describe :validate! do
15
+ it "should raise an error if email is a blank String" do
16
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => nil}
17
+ subject.options = options
18
+ expect { subject.send(:validate!) }.to raise_error(ArgumentError)
19
+
20
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => 'test@test.com'}
21
+ subject.options = options
22
+ expect { subject.send(:validate!) }.to_not raise_error(ArgumentError)
23
+ end
24
+
25
+ it "should raise an error if data is not either nil or a Hash" do
26
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => 'test@test.com', :data => []}
27
+ subject.options = options
28
+ expect { subject.send(:validate!) }.to raise_error(ArgumentError)
29
+
30
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => 'test@test.com', :data => nil}
31
+ subject.options = options
32
+ expect { subject.send(:validate!) }.to_not raise_error(ArgumentError)
33
+
34
+ options = {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => 'test@test.com', :data => {}}
35
+ subject.options = options
36
+ expect { subject.send(:validate!) }.to_not raise_error(ArgumentError)
37
+ end
38
+ end
39
+
40
+ describe :request do
41
+ it "should send a request to the Vero API" do
42
+ RestClient.should_receive(:post).with("https://www.getvero.com/api/v2/users/track.json", {:auth_token => 'abcd', :identity => {:email => 'test@test.com'}, :email => 'test@test.com'})
43
+ RestClient.stub(:post).and_return(200)
44
+ subject.send(:request)
45
+ end
46
+ end
47
+
48
+ describe "integration test" do
49
+ it "should not raise any errors" do
50
+ RestClient.stub(:post).and_return(200)
51
+ expect { subject.perform }.to_not raise_error
52
+ end
53
+ end
54
+ end