jirafe 0.2.0 → 0.3.0

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.
@@ -16,6 +16,8 @@ module Jirafe
16
16
  autoload :VERSION, "jirafe/version.rb"
17
17
  autoload :Configuration, "jirafe/configuration.rb"
18
18
  autoload :Error, "jirafe/error.rb"
19
+ autoload :Asset, "jirafe/asset.rb"
20
+ autoload :Tracker, "jirafe/tracker.rb"
19
21
  module Resource
20
22
  autoload :JirafeResource, "jirafe/resource/jirafe_resource.rb"
21
23
  autoload :Version, "jirafe/resource/version.rb"
@@ -23,6 +25,7 @@ module Jirafe
23
25
  autoload :Application, "jirafe/resource/application.rb"
24
26
  autoload :TimeSeries, "jirafe/resource/time_series.rb"
25
27
  autoload :Site, "jirafe/resource/site.rb"
28
+ autoload :Resources, "jirafe/resource/resources.rb"
26
29
  module Sites
27
30
  autoload :AbandonRate, "jirafe/resource/sites/abandon_rate.rb"
28
31
  autoload :AbandonedAverageOrderValue, "jirafe/resource/sites/abandoned_average_order_value.rb"
@@ -0,0 +1,19 @@
1
+ module Jirafe
2
+ class Asset
3
+ class << self
4
+ def js_url_for(component, platform)
5
+ [base_url, component, "js", "#{platform}_namespaced_ui.js"].join("/")
6
+ end
7
+
8
+ def css_url_for(component, platform)
9
+ [base_url, component, "css", "#{platform}_ui.css"].join("/")
10
+ end
11
+
12
+ private
13
+
14
+ def base_url
15
+ Jirafe.config.assets_url
16
+ end
17
+ end
18
+ end
19
+ end
@@ -5,14 +5,20 @@ module Jirafe
5
5
  include Singleton
6
6
  DEFAULT_URL = "https://api.jirafe.com/v1"
7
7
  DEFAULT_CALLBACK_URL = "https://data.jirafe.com"
8
+ DEFAULT_ASSETS_URL = "https://jirafe.com"
9
+ DEFAULT_TRACKING_URL = "https://c.jirafe.com"
8
10
  TEST_URL = "https://test-api.jirafe.com/v1"
9
11
  TEST_CALLBACK_URL = "https://test-data.jirafe.com"
12
+ TEST_ASSETS_URL = "https://test.jirafe.com"
13
+ TEST_TRACKING_URL = "https://test-c.jirafe.com"
10
14
  DEFAULT_CONTENT_TYPE = "application/json"
11
- attr_accessor :token, :url, :callback_url, :logger, :test, :content_type
15
+ attr_accessor :token, :url, :callback_url, :assets_url, :tracking_url, :logger, :test, :content_type
12
16
 
13
17
  def initialize
14
18
  @url = DEFAULT_URL
15
19
  @callback_url = DEFAULT_CALLBACK_URL
20
+ @assets_url = DEFAULT_ASSETS_URL
21
+ @tracking_url = DEFAULT_TRACKING_URL
16
22
  @content_type = DEFAULT_CONTENT_TYPE
17
23
  @token = nil
18
24
  @test = false
@@ -22,6 +28,8 @@ module Jirafe
22
28
  if value
23
29
  self.url = TEST_URL if url == DEFAULT_URL
24
30
  self.callback_url = TEST_CALLBACK_URL if callback_url == DEFAULT_CALLBACK_URL
31
+ self.assets_url = TEST_ASSETS_URL if assets_url == DEFAULT_ASSETS_URL
32
+ self.tracking_url = TEST_TRACKING_URL if tracking_url == DEFAULT_TRACKING_URL
25
33
  end
26
34
  @test = value == true
27
35
  end
@@ -34,9 +42,17 @@ module Jirafe
34
42
  def logger
35
43
  @logger ||= (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
36
44
  end
45
+
46
+ def tracking_path
47
+ uri = URI.parse(tracking_url)
48
+ [uri.host, uri.path].join("")
49
+ end
50
+
37
51
  def reset!
38
52
  self.url = DEFAULT_URL
39
53
  self.callback_url = DEFAULT_CALLBACK_URL
54
+ self.assets_url = DEFAULT_ASSETS_URL
55
+ self.tracking_url = DEFAULT_TRACKING_URL
40
56
  self.content_type = DEFAULT_CONTENT_TYPE
41
57
  self.token = nil
42
58
  self.test = false
@@ -9,15 +9,19 @@ module Jirafe
9
9
  attr_reader :json_body
10
10
 
11
11
  def initialize(response)
12
- @message = JSON.parse(response.body)["error"]["message"] rescue response.body
12
+ parsed_error = JSON.parse(response.body) rescue response.body
13
+ parsed_message = parsed_error["error"]["message"] rescue parsed_error
14
+ parsed_message_data = parsed_error["error"]["data"].join(', ') rescue nil
15
+ @message_data = JSON.parse(response.body)["error"]["data"] rescue nil
13
16
  @http_status = response.code
14
17
  @http_body = response.body
15
18
  @json_body = JSON.parse(response.body) rescue {}
19
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
20
+ @message = "#{status_string}#{parsed_message} #{parsed_message_data}"
16
21
  end
17
22
 
18
23
  def to_s
19
- status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
20
- "#{status_string}#{@message}"
24
+ @message
21
25
  end
22
26
  end
23
27
 
@@ -46,12 +46,6 @@ module Jirafe
46
46
  end
47
47
 
48
48
  def resource_url(identifier = nil, *parent_identifiers)
49
- # puts "#{self} START"
50
- # p parent_path(*parent_identifiers)
51
- # p parent_identifiers
52
- # p path
53
- # puts "ID: #{identifier.inspect}"
54
- # puts "#{self} END"
55
49
  [parent_path(*parent_identifiers), path, identifier].compact.join("/")
56
50
  end
57
51
 
@@ -0,0 +1,31 @@
1
+ module Jirafe
2
+ module Resource
3
+ class Resources < JirafeResource
4
+ path "resources"
5
+ parent Application
6
+ attributes({
7
+ :sites => [:create, :update],
8
+ :users => [:create, :update]
9
+ })
10
+
11
+ class << self
12
+ def sync(app_id, sites, users)
13
+ resource = self.new(:sites => array_to_hash(sites), :users => array_to_hash(users))
14
+ resource.parent = Application.new(:identifier => app_id)
15
+ resource.create
16
+ end
17
+
18
+ private
19
+
20
+ def array_to_hash(array)
21
+ index = 0
22
+ array.inject({}) do |hash, entry|
23
+ hash[index] = entry
24
+ index += 1
25
+ hash
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,6 +1,3 @@
1
- # post_users_authenticate POST /v1/users/{email}/authenticate.{_format}
2
- # post_users_register POST /v1/users/{email}/register.{_format}
3
- # post_users POST /v1/users.{_format}
4
1
  module Jirafe
5
2
  module Resource
6
3
  class Site < JirafeResource
@@ -14,19 +11,3 @@ module Jirafe
14
11
  end
15
12
  end
16
13
 
17
-
18
- # CREATE TABLE users (
19
- # user_id SERIAL NOT NULL PRIMARY KEY,
20
- # email varchar(255) NOT NULL,
21
- # name varchar(64) NOT NULL,
22
- # surname varchar(64),
23
- # password varchar(88),
24
- # salt varchar(32),
25
- # confirmation_token varchar(64),
26
- # is_hosted_user boolean NOT NULL DEFAULT 'f',
27
- # is_confirmed boolean NOT NULL DEFAULT 'f',
28
- # created_t_stamp timestamptz NOT NULL DEFAULT NOW(),
29
- # updated_t_stamp timestamptz NOT NULL DEFAULT NOW(),
30
- # UNIQUE (email),
31
- # CONSTRAINT lowercase_emails CHECK (email !~ '[A-Z]')
32
- # );
@@ -0,0 +1,24 @@
1
+ module Jirafe
2
+ class Tracker
3
+ class << self
4
+
5
+ def js_path
6
+ [Jirafe.config.tracking_path, "jirafe.js"].join("/")
7
+ end
8
+
9
+ def js_tag(json_data = "{}", html_options = {})
10
+ tag_id = html_options[:id] || "jirafe_analytics"
11
+ <<-HTML
12
+ <script type='text/javascript' id='#{tag_id}'>
13
+ var jirafe = #{json_data};
14
+ (function(){
15
+ var d=document,g=d.createElement('script'),s=d.getElementsByTagName('script')[0];
16
+ g.type='text/javascript',g.defer=g.async=true;g.src=d.location.protocol+'//#{js_path}';
17
+ s.parentNode.insertBefore(g, s);
18
+ })();
19
+ </script>
20
+ HTML
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Jirafe
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Jirafe::Asset do
4
+ describe ".js_url_for" do
5
+ subject { Jirafe::Asset.js_url_for(component, platform) }
6
+
7
+ context "given a component and platform" do
8
+ let(:component) { "dashboard" }
9
+ let(:platform) { "spree" }
10
+
11
+ it { should == "https://test.jirafe.com/dashboard/js/spree_namespaced_ui.js" }
12
+ end
13
+ end
14
+
15
+ describe ".css_url_for" do
16
+ subject { Jirafe::Asset.css_url_for(component, platform) }
17
+
18
+ context "given a component and platform" do
19
+ let(:component) { "dashboard" }
20
+ let(:platform) { "spree" }
21
+
22
+ it { should == "https://test.jirafe.com/dashboard/css/spree_ui.css" }
23
+ end
24
+ end
25
+ end
@@ -27,6 +27,8 @@ describe Jirafe::Configuration do
27
27
  instance.test.should be_true
28
28
  instance.url.should == "https://test-api.jirafe.com/v1"
29
29
  instance.callback_url.should == "https://test-data.jirafe.com"
30
+ instance.assets_url.should == "https://test.jirafe.com"
31
+ instance.tracking_url.should == "https://test-c.jirafe.com"
30
32
  end
31
33
  end
32
34
 
@@ -40,6 +42,8 @@ describe Jirafe::Configuration do
40
42
  instance.test.should be_false
41
43
  instance.url.should == "https://api.jirafe.com/v1"
42
44
  instance.callback_url.should == "https://data.jirafe.com"
45
+ instance.assets_url.should == "https://jirafe.com"
46
+ instance.tracking_url.should == "https://c.jirafe.com"
43
47
  end
44
48
  end
45
49
  end
@@ -56,13 +60,13 @@ describe Jirafe::Configuration do
56
60
  subject { instance.url }
57
61
  before { instance.reset! }
58
62
 
59
- it { should == "https://api.jirafe.com/v1"}
63
+ it { should == "https://api.jirafe.com/v1" }
60
64
 
61
65
  context "overriding the url" do
62
66
  before { instance.url = "http://myurl" }
63
67
  after { instance.reset! }
64
68
 
65
- it { should == "http://myurl"}
69
+ it { should == "http://myurl" }
66
70
  end
67
71
  end
68
72
 
@@ -70,16 +74,51 @@ describe Jirafe::Configuration do
70
74
  subject { instance.callback_url }
71
75
  before { instance.reset! }
72
76
 
73
- it { should == "https://data.jirafe.com"}
77
+ it { should == "https://data.jirafe.com" }
74
78
 
75
79
  context "overriding the url" do
76
80
  before { instance.callback_url = "http://myurl" }
77
81
  after { instance.reset! }
78
82
 
79
- it { should == "http://myurl"}
83
+ it { should == "http://myurl" }
80
84
  end
81
85
  end
82
86
 
87
+ describe "#assets_url" do
88
+ subject { instance.assets_url }
89
+ before { instance.reset! }
90
+
91
+ it { should == "https://jirafe.com" }
92
+
93
+ context "overriding the url" do
94
+ before { instance.assets_url = "http://myurl" }
95
+ after { instance.reset! }
96
+
97
+ it { should == "http://myurl" }
98
+ end
99
+ end
100
+
101
+ describe "#tracking_url" do
102
+ subject { instance.tracking_url }
103
+ before { instance.reset! }
104
+
105
+ it { should == "https://c.jirafe.com" }
106
+
107
+ context "overriding the url" do
108
+ before { instance.tracking_url = "http://myurl" }
109
+ after { instance.reset! }
110
+
111
+ it { should == "http://myurl" }
112
+ end
113
+ end
114
+
115
+ describe "#tracking_path" do
116
+ subject { instance.tracking_path }
117
+ before { instance.tracking_url = "https://myurl.onsomedomain.com/withdir" }
118
+
119
+ it { should == "myurl.onsomedomain.com/withdir" }
120
+ end
121
+
83
122
  describe "#token" do
84
123
  subject { instance.token }
85
124
 
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ describe Jirafe::Resource::Resources do
4
+ let(:resource_class) { Jirafe::Resource::Resources }
5
+ let(:application_id) { 1 }
6
+ let(:sites) do
7
+ [{:description => "My Store",
8
+ :url => "http://mynewurl.com",
9
+ :currency => "EUR",
10
+ :timezone => "Europe/Paris",
11
+ :external_id => 1,
12
+ :site_id => 1
13
+ }]
14
+ end
15
+
16
+ let(:users) do
17
+ [{:email => "user@example.com",
18
+ :first_name => "Test",
19
+ :last_name => "User"
20
+ }]
21
+ end
22
+
23
+ before { use_app_token }
24
+
25
+ describe ".sync" do
26
+ let(:response) { make_request(cassette) { resource_class.sync(application_id, sites, users) } }
27
+ subject { response }
28
+
29
+ let(:cassette) { "sync_post_with_app_token" }
30
+
31
+ its(:class) { should == resource_class }
32
+
33
+ context "the sites hash" do
34
+ let(:site) { response.sites.first }
35
+
36
+ it "matches the updated values" do
37
+ site["site_id"].should == sites.first[:site_id]
38
+ site["description"].should == sites.first[:description]
39
+ site["url"].should == sites.first[:url]
40
+ site["store_api_url"].should be_nil
41
+ site["store_cart_url"].should be_nil
42
+ end
43
+ end
44
+
45
+ context "the users hash" do
46
+ let(:user) { response.users.first }
47
+
48
+ it "matches the updated values" do
49
+ user["email"].should == users.first[:email]
50
+ user["first_name"].should == users.first[:first_name]
51
+ user["last_name"].should == users.first[:last_name]
52
+ user["token"].should_not be_nil
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe Jirafe::Tracker do
4
+ describe ".js_path" do
5
+ subject { Jirafe::Tracker.js_path }
6
+
7
+ it { should == "test-c.jirafe.com/jirafe.js" }
8
+ end
9
+
10
+ describe ".js_tag" do
11
+ subject { Jirafe::Tracker.js_tag(json_data, {:id => 'myid'}) }
12
+
13
+ context "given some JSON data" do
14
+ let(:json_data) { { "foo" => "bar" }.to_json }
15
+
16
+ let(:expected_html) do
17
+ <<-HTML
18
+ <script type='text/javascript' id='myid'>
19
+ var jirafe = {"foo":"bar"};
20
+ (function(){
21
+ var d=document,g=d.createElement('script'),s=d.getElementsByTagName('script')[0];
22
+ g.type='text/javascript',g.defer=g.async=true;g.src=d.location.protocol+'//test-c.jirafe.com/jirafe.js';
23
+ s.parentNode.insertBefore(g, s);
24
+ })();
25
+ </script>
26
+ HTML
27
+ end
28
+
29
+ it { should == expected_html }
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jirafe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-07 00:00:00.000000000 Z
12
+ date: 2012-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -211,10 +211,12 @@ files:
211
211
  - fixtures/responses/site_visits_get_with_master_token.yml
212
212
  - fixtures/responses/status_get.yml
213
213
  - fixtures/responses/status_get_500.yml
214
+ - fixtures/responses/sync_post_with_app_token.yml
214
215
  - fixtures/responses/test.yml
215
216
  - fixtures/responses/version_get.yml
216
217
  - jirafe.gemspec
217
218
  - lib/jirafe.rb
219
+ - lib/jirafe/asset.rb
218
220
  - lib/jirafe/callback/event.rb
219
221
  - lib/jirafe/callback/events/order.rb
220
222
  - lib/jirafe/callback/events/refund.rb
@@ -223,6 +225,7 @@ files:
223
225
  - lib/jirafe/error.rb
224
226
  - lib/jirafe/resource/application.rb
225
227
  - lib/jirafe/resource/jirafe_resource.rb
228
+ - lib/jirafe/resource/resources.rb
226
229
  - lib/jirafe/resource/site.rb
227
230
  - lib/jirafe/resource/sites/abandon_rate.rb
228
231
  - lib/jirafe/resource/sites/abandoned_average_order_value.rb
@@ -250,16 +253,18 @@ files:
250
253
  - lib/jirafe/resource/sites/visitors_interest.rb
251
254
  - lib/jirafe/resource/sites/visits.rb
252
255
  - lib/jirafe/resource/status.rb
253
- - lib/jirafe/resource/sync_service.rb
254
256
  - lib/jirafe/resource/time_series.rb
255
257
  - lib/jirafe/resource/user.rb
256
258
  - lib/jirafe/resource/version.rb
259
+ - lib/jirafe/tracker.rb
257
260
  - lib/jirafe/version.rb
261
+ - spec/jirafe/asset_spec.rb
258
262
  - spec/jirafe/callback/event_spec.rb
259
263
  - spec/jirafe/configuration_spec.rb
260
264
  - spec/jirafe/jirafe_spec.rb
261
265
  - spec/jirafe/resource/application_spec.rb
262
266
  - spec/jirafe/resource/jirafe_resource_spec.rb
267
+ - spec/jirafe/resource/resources_spec.rb
263
268
  - spec/jirafe/resource/site_spec.rb
264
269
  - spec/jirafe/resource/sites/abandon_rate_spec.rb
265
270
  - spec/jirafe/resource/sites/abandoned_average_order_value_spec.rb
@@ -288,6 +293,7 @@ files:
288
293
  - spec/jirafe/resource/sites/visits_spec.rb
289
294
  - spec/jirafe/resource/status_spec.rb
290
295
  - spec/jirafe/resource/version_spec.rb
296
+ - spec/jirafe/tracker_spec.rb
291
297
  - spec/spec_helper.rb
292
298
  - spec/support/callback_server.rb
293
299
  - spec/support/response_helper.rb
@@ -319,11 +325,13 @@ signing_key:
319
325
  specification_version: 3
320
326
  summary: Client for Jirafe analytics web service
321
327
  test_files:
328
+ - spec/jirafe/asset_spec.rb
322
329
  - spec/jirafe/callback/event_spec.rb
323
330
  - spec/jirafe/configuration_spec.rb
324
331
  - spec/jirafe/jirafe_spec.rb
325
332
  - spec/jirafe/resource/application_spec.rb
326
333
  - spec/jirafe/resource/jirafe_resource_spec.rb
334
+ - spec/jirafe/resource/resources_spec.rb
327
335
  - spec/jirafe/resource/site_spec.rb
328
336
  - spec/jirafe/resource/sites/abandon_rate_spec.rb
329
337
  - spec/jirafe/resource/sites/abandoned_average_order_value_spec.rb
@@ -352,6 +360,7 @@ test_files:
352
360
  - spec/jirafe/resource/sites/visits_spec.rb
353
361
  - spec/jirafe/resource/status_spec.rb
354
362
  - spec/jirafe/resource/version_spec.rb
363
+ - spec/jirafe/tracker_spec.rb
355
364
  - spec/spec_helper.rb
356
365
  - spec/support/callback_server.rb
357
366
  - spec/support/response_helper.rb