kilometer-ruby 0.1.1 → 0.1.2

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.
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.iml
11
+ .idea
12
+ .idea/*
13
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kilometer-ruby-api-ruby-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Events::Api::Ruby::Client
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/events/api/ruby/client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'events-api-ruby-client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install events-api-ruby-client
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/events-api-ruby-client.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/README.rst ADDED
@@ -0,0 +1 @@
1
+ Events API client for Ruby
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kilometer-ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kilometer-ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kilometer-ruby"
8
+ spec.version = Kilometer::VERSION
9
+ spec.authors = ["Yuri Kroz"]
10
+ spec.email = ["yuri@kilometer.io"]
11
+ spec.summary = "The official Kilometer.io Ruby library."
12
+ spec.description = "This is the official Kilometer.io Ruby library. This library allows for server-side integration of Kilometer."
13
+ spec.homepage = "http://kilometer.io"
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = '>= 2.0.0'
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12.a"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,3 @@
1
+ module Kilometer
2
+ VERSION = '0.1.2'
3
+ end
@@ -0,0 +1,182 @@
1
+ require_relative 'kilometer-ruby/version'
2
+ require 'date'
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'uri'
6
+
7
+ module Kilometer
8
+
9
+ class EventsAPIClient
10
+ # Use Kilometer::EventsAPIClient to track events and manage users properties.
11
+
12
+ HEADER_CUSTOMER_APP_ID = 'Customer-App-Id'
13
+ HEADER_TIMESTAMP = 'Timestamp'
14
+ HEADER_CLIENT_TYPE = 'Client-Type'
15
+ HEADER_CONTENT_TYPE = 'Content-Type'
16
+ CLIENT_TYPE = 'ruby'
17
+ CONTENT_TYPE = 'application/json'
18
+
19
+ def initialize(token, endpoint_url=nil)
20
+ @token = token
21
+ @endpoint_url = endpoint_url || 'http://events.kilometer.io'
22
+ end
23
+
24
+ public
25
+
26
+ # Creates a new identified user if he doesn't exist.
27
+ #
28
+ # user_id: identified user's ID
29
+ # headers: custom request headers (if isn't set default values are used)
30
+ # endpoint_url: where to send the request (if isn't set default value is used)
31
+ def add_user(user_id, headers=nil, endpoint_url=nil)
32
+
33
+ uri = URI("#{endpoint_url || @endpoint_url}/users")
34
+
35
+ Net::HTTP.start(uri.host, uri.port) do |http|
36
+
37
+ request = Net::HTTP::Post.new(uri.request_uri)
38
+
39
+ # Set headers
40
+ if headers
41
+ headers.each { |header, value| request.add_field(header, value) }
42
+ else
43
+ self.set_default_headers(request)
44
+ request.add_field(HEADER_CONTENT_TYPE, CONTENT_TYPE)
45
+ end
46
+
47
+ # Set payload
48
+ request.body = {user_id: user_id}.to_json
49
+
50
+ # Execute the request
51
+ http.request(request)
52
+ end
53
+ end
54
+
55
+ # Send an identified event. If a user doesn't exist it will create one.
56
+ #
57
+ # user_id: identified user's ID
58
+ # event_name: event name (e.g. "visit_website")
59
+ # event_properties: properties that describe the event's details
60
+ # headers: custom request headers (if isn't set default values are used)
61
+ # endpoint_url: where to send the request (if isn't set default value is used)
62
+ def add_event(user_id, event_name, event_properties=nil, headers=nil, endpoint_url=nil)
63
+
64
+ uri = URI("#{endpoint_url || @endpoint_url}/events")
65
+
66
+ Net::HTTP.start(uri.host, uri.port) do |http|
67
+
68
+ request = Net::HTTP::Post.new(uri.request_uri)
69
+
70
+ # Set headers
71
+ if headers
72
+ headers.each { |header, value| request.add_field(header, value) }
73
+ else
74
+ self.set_default_headers(request)
75
+ request.add_field(HEADER_CONTENT_TYPE, CONTENT_TYPE)
76
+ end
77
+
78
+ # Set payload
79
+ request.body = {
80
+ user_id: user_id,
81
+ event_name: event_name,
82
+ event_type: 'identified',
83
+ event_properties: event_properties || {}
84
+ }.to_json
85
+
86
+ # Execute the request
87
+ http.request(request)
88
+ end
89
+ end
90
+
91
+ # Increase a user's property by a value.
92
+ #
93
+ # user_id: identified user's ID
94
+ # property_name: user property name to increase
95
+ # value: amount by which to increase the property
96
+ # headers: custom request headers (if isn't set default values are used)
97
+ # endpoint_url: where to send the request (if isn't set default value is used)
98
+ def increase_user_property(user_id, property_name, value=0, headers=nil, endpoint_url=nil)
99
+ inc_or_dec_user_property('increase', user_id, property_name, value, headers, endpoint_url)
100
+ end
101
+
102
+ # Decrease a user's property by a value.
103
+ #
104
+ # user_id: identified user's ID
105
+ # property_name: user property name to decrease
106
+ # value: amount by which to decrease the property
107
+ # headers: custom request headers (if isn't set default values are used)
108
+ # endpoint_url: where to send the request (if isn't set default value is used)
109
+ def decrease_user_property(user_id, property_name, value=0, headers=nil, endpoint_url=nil)
110
+ inc_or_dec_user_property('decrease', user_id, property_name, value, headers, endpoint_url)
111
+ end
112
+
113
+ # Update a user's properties with values provided in "user_properties" dictionary
114
+ #
115
+ # user_id: identified user's ID
116
+ # user_properties: user properties to update with a new values
117
+ # headers: custom request headers (if isn't set default values are used)
118
+ # endpoint_url: where to send the request (if isn't set default value is used)
119
+ def update_user_properties(user_id, user_properties, headers=nil, endpoint_url=nil)
120
+
121
+ uri = URI("#{endpoint_url || @endpoint_url}/users/#{user_id}/properties")
122
+
123
+ Net::HTTP.start(uri.host, uri.port) do |http|
124
+
125
+ request = Net::HTTP::Put.new(uri.request_uri)
126
+
127
+ # Set headers
128
+ if headers
129
+ headers.each { |header, value| request.add_field(header, value) }
130
+ else
131
+ self.set_default_headers(request)
132
+ request.add_field(HEADER_CONTENT_TYPE, CONTENT_TYPE)
133
+ end
134
+
135
+ # Set payload
136
+ request.body = user_properties.to_json
137
+
138
+ # Execute the request
139
+ http.request(request)
140
+ end
141
+
142
+
143
+ end
144
+
145
+ # Gets Net::HTTP request instance and modifies it by adding default HTTP headers.
146
+ #
147
+ # WARNING: The method modifies the #request object!
148
+ def set_default_headers(request)
149
+ request.add_field(HEADER_CUSTOMER_APP_ID, @token)
150
+ request.add_field(HEADER_CLIENT_TYPE, CLIENT_TYPE)
151
+ request.add_field(HEADER_TIMESTAMP, Kilometer::now)
152
+ end
153
+
154
+ private
155
+
156
+ def inc_or_dec_user_property(action, user_id, property_name, value=0, headers=nil, endpoint_url=nil)
157
+
158
+ uri = URI("#{endpoint_url || @endpoint_url}/users/#{user_id}/properties/#{property_name}/#{action}/#{value}")
159
+
160
+ Net::HTTP.start(uri.host, uri.port) do |http|
161
+
162
+ request = Net::HTTP::Post.new(uri.request_uri)
163
+
164
+ # Set headers
165
+ if headers
166
+ headers.each { |header, value_| request.add_field(header, value_) }
167
+ else
168
+ self.set_default_headers(request)
169
+ end
170
+
171
+ # Execute the request
172
+ http.request(request)
173
+ end
174
+ end
175
+
176
+ end
177
+
178
+ # Get current timestamp in milliseconds
179
+ def self.now
180
+ DateTime.now.strftime('%Q')
181
+ end
182
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kilometer-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -50,7 +50,18 @@ email:
50
50
  executables: []
51
51
  extensions: []
52
52
  extra_rdoc_files: []
53
- files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - README.rst
59
+ - Rakefile
60
+ - bin/console
61
+ - bin/setup
62
+ - kilometer-ruby.gemspec
63
+ - lib/kilometer-ruby.rb
64
+ - lib/kilometer-ruby/version.rb
54
65
  homepage: http://kilometer.io
55
66
  licenses:
56
67
  - MIT
@@ -63,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
74
  requirements:
64
75
  - - ! '>='
65
76
  - !ruby/object:Gem::Version
66
- version: '0'
77
+ version: 2.0.0
67
78
  required_rubygems_version: !ruby/object:Gem::Requirement
68
79
  none: false
69
80
  requirements: