localytics-ruby 0.0.1 → 0.0.7

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 CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- OWI0YTMzNDBiYjA1NTUyZDc2Y2ZkMWE3NjJiNGNjN2IxYzJjOGMyZA==
5
- data.tar.gz: !binary |-
6
- Y2FkMWNjYWU1NGRlOGVmYjVhNDI1NGZiNDJkZDIzOTI1YmY3NWQ1Ng==
2
+ SHA256:
3
+ metadata.gz: a6b2f7023c1d5e981f2bc4bb4ff10f9970cbc453cfe160102baa554a60a1c945
4
+ data.tar.gz: 65bbf127d9306b033efef67660e438365c5c1b6b2fda1e78fd3336745e520203
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- Y2ZlMDU0ZmUxNTI3NzVhMmMyNzBhZGMwNWRiYWQzYjk2ZTgyYzEwNzE2ZDhj
10
- MzM3YjZhODNhOTE4NWEzOWFjMDk1ZmQwNmVkYzI5OGIwNWEzNDhmY2FlMDA5
11
- NDc4NWIyMWIzYWJkNjgxYTMwNzU4OWYxN2ViNTQ2MWY5MjhmZTE=
12
- data.tar.gz: !binary |-
13
- ODYzODI2MGZjNDZiZTM3YjY5ZDY2NjJmMjlkNDMwMWQ5MDgwNGY5N2U4NzMw
14
- ZTA0ZTY0MGY4OTBlMmY5NTU4ZGFiNWRkNDdjYTVmM2JkZWVjNTFmNjE0MWU3
15
- ZTgzZmVlOGJjYWRkNjk2MDMxODA5NWJmODUwYTViMDZjZWRkNzk=
6
+ metadata.gz: 80bd19ff995e8f0b9379ea6afbc9975ef6987f3a760c3b50858b71acb096dae0e17519fd0bd40ad8d596dea6553961252b830dccd7387eaa82cd8a8a03db8ca6
7
+ data.tar.gz: 65b7fc28afaa3545b508f7b7e4480a06c67309f0b4fa5c01ebaa69c300ab07d589183f73c63299cee501c62bde21df701f6cc4675dfd69d15ce4b684fa30613d
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tobuy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,38 @@
1
+ Tobuy
2
+ =====
3
+
4
+ Ruby wrapper for Localytics API
5
+
6
+ A lot of inspiration (and code) for how this gem was built come from https://github.com/Mango/mango-ruby
7
+
8
+
9
+ ## Description
10
+
11
+ API to interact with Localytics
12
+ https://localytics.com/
13
+
14
+
15
+ ## Installation
16
+
17
+ As usual, you can install it using rubygems.
18
+
19
+ ```
20
+ $ gem install localytics-ruby
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ require 'localytics-ruby'
27
+
28
+ Localytics.api_key = ENV['LOCALYTICS_API_KEY']
29
+ Localytics.api_secret = ENV['LOCALYTICS_API_SECRET']
30
+
31
+ customerId = 1
32
+
33
+ begin
34
+ profile = Localytics::Profile.show 1
35
+ rescue Localytics::Error => e
36
+ e.each {|code, message| ... }
37
+ end
38
+ ```
@@ -3,13 +3,15 @@ require 'json'
3
3
  require 'base64'
4
4
  require_relative 'localytics/profile'
5
5
  require_relative 'localytics/push'
6
+ require_relative 'localytics/app'
7
+ require_relative 'localytics/event'
6
8
 
7
9
  module Localytics
8
10
  class Error < StandardError
9
11
  include Enumerable
10
12
  attr_accessor :errors
11
13
 
12
- def initialize message, errors={}
14
+ def initialize(message, errors={})
13
15
  super message
14
16
  @errors = errors
15
17
  end
@@ -37,30 +39,39 @@ module Localytics
37
39
  raise Error.new('No API secret provided')
38
40
  end
39
41
 
40
- payload = JSON.generate(params) if method == :post || method == :patch
41
- params = nil unless method == :get
42
+ unless method == :get
43
+ payload = JSON.generate(params)
44
+ params = nil
45
+ end
42
46
 
43
47
  auth = 'Basic ' + Base64.strict_encode64("#{api_key}:#{api_secret}")
44
48
 
45
49
  headers = {
46
- :params => params,
47
- :content_type => 'application/json',
48
- :accept => 'application/json',
50
+ :params => params,
51
+ :content_type => 'application/json',
52
+ :accept => 'application/json',
49
53
  :authorization => auth
50
54
 
51
55
  }.merge(headers)
52
56
 
53
57
  options = {
54
58
  :headers => headers,
55
- :method => method,
56
- :url => url,
59
+ :method => method,
60
+ :url => url,
57
61
  :payload => payload
58
62
  }
59
63
 
60
64
  begin
61
- response = execute_request(options)
62
- return {} if response.code == 204 and method == :delete
63
- JSON.parse(response.body, :symbolize_names => true)
65
+ return_hash = {}
66
+ response = execute_request(options)
67
+ return return_hash if response.code == 204 && method == :delete
68
+
69
+ if response.body && !response.body.empty?
70
+ # Don't try to call JSON.parse on empty response body.
71
+ return_hash = JSON.parse(response.body, :symbolize_names => true)
72
+ end
73
+ return return_hash
74
+
64
75
  rescue RestClient::Exception => e
65
76
  handle_errors e
66
77
  end
@@ -72,9 +83,9 @@ module Localytics
72
83
  RestClient::Request.execute(options)
73
84
  end
74
85
 
75
- def self.handle_errors exception
76
- body = JSON.parse exception.http_body
86
+ def self.handle_errors(exception)
87
+ body = JSON.parse(exception.http_body)
77
88
  raise Error.new(exception.to_s, body['errors'])
78
89
  end
79
90
 
80
- end
91
+ end
@@ -0,0 +1,39 @@
1
+ module Localytics
2
+ class App
3
+
4
+ class << self
5
+ attr_accessor :app_id
6
+ end
7
+
8
+ def self.show_app(app_id=nil, api_key=nil, api_secret=nil)
9
+ Localytics.request api_base, :get, url(app_id), api_key, api_secret
10
+ end
11
+
12
+ def self.app_attributes(app_id=nil, api_key=nil, api_secret=nil)
13
+ Localytics.request api_base, :get, url(app_id, true), api_key, api_secret
14
+ end
15
+
16
+ def self.show_apps(app_id=nil, api_key=nil, api_secret=nil)
17
+ Localytics.request api_base, :get, '', api_key, api_secret
18
+ end
19
+
20
+ private
21
+ def self.api_base
22
+ "https://api.localytics.com/v1/apps"
23
+ end
24
+
25
+ def self.url(app_id=nil, attributes=nil)
26
+ unless app_id ||= self.app_id
27
+ raise Error.new('No APP id provided')
28
+ end
29
+
30
+ url = "/#{app_id}"
31
+
32
+ if attributes
33
+ url = url + "/attributes"
34
+ end
35
+
36
+ return url
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ module Localytics
2
+
3
+ class Event
4
+ class << self
5
+ attr_accessor :app_id
6
+ end
7
+
8
+ # @param event_attributes = Optional hash of up to 50 key/value attribute pairs.
9
+ # Values must be formatted as strings.
10
+ # @param ltv_change = Optional int representing incremental change in user's lifetime value.
11
+ # Must be integer, e.g. use 299 for $2.99.
12
+ def self.send(app_id, customer_id, event_name, event_attributes=nil, ltv_change=nil, api_key=nil, api_secret=nil)
13
+ raise Error.new('No APP id provided') unless app_id ||= self.app_id
14
+ raise Error.new('No customer_id provided') if customer_id.nil?
15
+ raise Error.new('No event_name provided') if event_name.nil? || event_name.empty?
16
+
17
+ params = {
18
+ schema_url: "https://localytics-files.s3.amazonaws.com/schemas/eventsApi/v1.json",
19
+ app_uuid: app_id,
20
+ customer_id: customer_id.to_s,
21
+ event_name: event_name,
22
+ event_time: (Time.now.to_f * 1000).to_i,
23
+ uuid: SecureRandom.uuid
24
+ }
25
+ params[:attributes] = event_attributes if event_attributes && !event_attributes.empty?
26
+ params[:ltv_change] = ltv_change if ltv_change
27
+
28
+ Localytics.request api_base, :post, url, api_key, api_secret, params
29
+ end
30
+
31
+ private
32
+
33
+ def self.api_base
34
+ "https://analytics.localytics.com/events/v1"
35
+ end
36
+
37
+ def self.url
38
+ "/uploads"
39
+ end
40
+ end
41
+
42
+ end
@@ -22,7 +22,7 @@ module Localytics
22
22
  #
23
23
  # More information on how messages can be built can be found on
24
24
  # http://docs.localytics.com/#Dev/getting-started-trans-push.html
25
- def self.push(messages, target_type, app_id=nil, api_key=nil, api_secret=nil, headers={})
25
+ def self.push(messages, target_type, app_id, campaing_key = nil, api_key=nil, api_secret=nil, headers={})
26
26
  Localytics.request(
27
27
  api_base(app_id),
28
28
  :post,
@@ -32,7 +32,7 @@ module Localytics
32
32
  {
33
33
  messages: messages,
34
34
  target_type: target_type,
35
- campaign_key: nil,
35
+ campaign_key: campaing_key,
36
36
  request_id: SecureRandom.uuid
37
37
  },
38
38
  headers
@@ -40,22 +40,22 @@ module Localytics
40
40
  end
41
41
 
42
42
  # For :messages options please check the :push method
43
- def self.push_to_customers(messages, app_id, api_key=nil, api_secret=nil)
44
- push messages, 'customer_id', app_id, api_key, api_secret
43
+ def self.push_to_customers(messages, app_id, campaing_key = nil, api_key=nil, api_secret=nil)
44
+ push messages, 'customer_id', app_id, campaing_key, api_key, api_secret
45
45
  end
46
46
 
47
47
  # For :messages options please check the :push method
48
- def self.push_to_all_customers(messages, app_id, api_key=nil, api_secret=nil)
49
- push messages, 'broadcast', app_id, api_key, api_secret
48
+ def self.push_to_all_customers(messages, app_id, campaing_key = nil, api_key=nil, api_secret=nil)
49
+ push messages, 'broadcast', app_id, campaing_key, api_key, api_secret
50
50
  end
51
51
 
52
52
  # For :messages options please check the :push method
53
- def self.push_to_profiles(messages, app_id, api_key=nil, api_secret=nil)
54
- push messages, 'profile', app_id, api_key, api_secret
53
+ def self.push_to_profiles(messages, app_id, campaing_key = nil, api_key=nil, api_secret=nil)
54
+ push messages, 'profile', app_id, campaing_key, api_key, api_secret
55
55
  end
56
56
 
57
- def self.push_to_audiences(messages, app_id, api_key=nil, api_secret=nil)
58
- push messages, 'audience_id', app_id, api_key, api_secret
57
+ def self.push_to_audiences(messages, app_id, campaing_key = nil, api_key=nil, api_secret=nil)
58
+ push messages, 'audience_id', app_id, campaing_key, api_key, api_secret
59
59
  end
60
60
 
61
61
  private
@@ -71,3 +71,26 @@ test 'send push' do |mock|
71
71
  Localytics::Push.push_to_customers [{alert: 'message', target: 1}], 'app_id'
72
72
  end
73
73
 
74
+ test 'show apps' do |mock|
75
+ mock.expects(:get).once.with('https://api.localytics.com/v1/apps', {}).returns(test_response(test_show_apps, 200))
76
+ apps = Localytics::App.show_apps
77
+ assert_equal 'App Name', apps[:_embedded][:apps][0][:name]
78
+ end
79
+
80
+ test 'show app' do |mock|
81
+ mock.expects(:get).once.with('https://api.localytics.com/v1/apps/umdois', {}).returns(test_response(test_show_app, 200))
82
+ app = Localytics::App.show_app('umdois')
83
+ assert_equal 'App Name', app[:name]
84
+ end
85
+
86
+ test 'show app attributes' do |mock|
87
+ mock.expects(:get).once.with('https://api.localytics.com/v1/apps/umdois/attributes', {}).returns(test_response(test_apps_attributes, 200))
88
+ events = Localytics::App.app_attributes('umdois')
89
+ assert_equal 'Clicked Link', events[:events][0][:event_name]
90
+ end
91
+
92
+ test 'send event' do |mock|
93
+ mock.expects(:post).once.with('https://analytics.localytics.com/events/v1/uploads', anything).returns(test_response({}, 202))
94
+ response = Localytics::Event.send('app_id', 111111, 'event_name')
95
+ assert(response.empty?)
96
+ end
@@ -83,6 +83,33 @@ def test_profiles(params={})
83
83
  }.merge(params)
84
84
  end
85
85
 
86
+ def test_show_apps(params={})
87
+ {
88
+ "_embedded": {
89
+ "apps": [
90
+ {
91
+ "name": "App Name",
92
+ "app_id": "umdois",
93
+ "created_at": "2012-04-10T04:07:13Z",
94
+ "_links": {
95
+ "self": { "href": "/v1/apps/umdois" },
96
+ "apps": { "href": "/v1/apps" },
97
+ "query": {
98
+ "templated": true,
99
+ "href": "/v1/apps/umdois/query{?app_id,metrics,dimensions,conditions,limit,order,days,comment,translate}"
100
+ },
101
+ "root": { "href": "/v1" }
102
+ }
103
+ }
104
+ ]
105
+ },
106
+ "_links": {
107
+ "self": { "href": "/v1/apps" },
108
+ "root": { "href": "/v1" }
109
+ }
110
+ }
111
+ end
112
+
86
113
  def profile_params
87
114
  {
88
115
  attributes: {
@@ -93,6 +120,53 @@ def profile_params
93
120
  }
94
121
  end
95
122
 
123
+
124
+ def test_apps_attributes()
125
+ {
126
+ "events": [
127
+ {
128
+ "event_name": "Clicked Link",
129
+ "attributes": [ "link_text", "link_target", "link_placement" ],
130
+ "high_cardinality_attributes": [ "link_text" ]
131
+ },
132
+ {
133
+ "event_name": "Added to Cart",
134
+ "attributes": [ "item_name", "item_id" ],
135
+ "high_cardinality_attributes": [ "item_name", "item_id" ]
136
+ }
137
+ ],
138
+ "_links": {
139
+ "app": { "href": "/v1/apps/umdois" },
140
+ "root": { "href": "/v1" }
141
+ }
142
+ }
143
+ end
144
+
145
+ def test_show_app()
146
+ app_info = test_show_apps.delete(:_embedded)[:apps][0]
147
+
148
+ more_info = {
149
+ "stats": {
150
+ "sessions": 52291,
151
+ "closes": 46357,
152
+ "users": 7008,
153
+ "events": 865290,
154
+ "data_points": 963938,
155
+ "platforms": ["HTML5"],
156
+ "client_libraries": ["html5_2.6", "html5_2.5", "html5_2.4"],
157
+ "begin_date": "2013-08-10",
158
+ "end_date": "2013-09-10"
159
+ },
160
+ "icon_url": "https://example.com/app-icon.png",
161
+ "custom_dimensions": {
162
+ "custom_0_dimension": "Blood Type",
163
+ "custom_1_dimension": "Moon Phase"
164
+ }
165
+ }
166
+
167
+ app_info.merge(more_info)
168
+ end
169
+
96
170
  def bad_request
97
171
  json = {
98
172
  'errors' => [{
@@ -100,4 +174,4 @@ def bad_request
100
174
  }]
101
175
  }
102
176
  RestClient::Exception.new(test_response(json, 404), 400)
103
- end
177
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localytics-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobuy development team
@@ -9,64 +9,68 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-02 00:00:00.000000000 Z
12
+ date: 2020-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '1.7'
20
+ version: '2.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '1.7'
27
+ version: '2.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: cutest
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ~>
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
34
  version: '1.2'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ~>
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: '1.2'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: mocha
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - '='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '1.1'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - '='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '1.1'
56
- description: API to interact with Localytics\nhttps://localytics.com/
56
+ description: API to interact with Localytics https://localytics.com/
57
57
  email:
58
58
  - support@tob.uy
59
59
  executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
+ - LICENSE
64
+ - README.md
63
65
  - lib/localytics-ruby.rb
64
66
  - lib/localytics.rb
67
+ - lib/localytics/app.rb
68
+ - lib/localytics/event.rb
65
69
  - lib/localytics/profile.rb
66
70
  - lib/localytics/push.rb
67
71
  - test/localytics_test.rb
68
72
  - test/test_helper.rb
69
- homepage: https://tob.uy/
73
+ homepage: https://github.com/tobuy/localytics-ruby
70
74
  licenses:
71
75
  - MIT
72
76
  metadata: {}
@@ -76,20 +80,19 @@ require_paths:
76
80
  - lib
77
81
  required_ruby_version: !ruby/object:Gem::Requirement
78
82
  requirements:
79
- - - ! '>='
83
+ - - ">="
80
84
  - !ruby/object:Gem::Version
81
85
  version: '0'
82
86
  required_rubygems_version: !ruby/object:Gem::Requirement
83
87
  requirements:
84
- - - ! '>='
88
+ - - ">="
85
89
  - !ruby/object:Gem::Version
86
90
  version: '0'
87
91
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.4.5
92
+ rubygems_version: 3.0.8
90
93
  signing_key:
91
94
  specification_version: 4
92
95
  summary: Ruby wrapper for Localytics API
93
96
  test_files:
94
- - test/localytics_test.rb
95
97
  - test/test_helper.rb
98
+ - test/localytics_test.rb