totangorb 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f039355a75921528c7c46461541f1397835675cb
4
+ data.tar.gz: fc2eb1910e192b193d3f86b65135659ccc8e8733
5
+ SHA512:
6
+ metadata.gz: 62d22eea99ae1fd2a777e5bf930ef233069a67967af279ea6aaa3e4161bc40b44a02cda9ceff698a570fbf19972c3c9ac85c3fbb3fefa0b409ad74c911b39342
7
+ data.tar.gz: 7a6439741a8f4f2e9ba843394894bcb22328f6d838add2f1f50f2b652f1add2d91beaa38987e67337c861749c7b38ae351b6a3ecca27177c838647865eeafbcb
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in totangorb.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michał Darda
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # Totangorb
2
+
3
+ Lightweight Ruby wrapper for [Totango](http:/http://www.totango.com/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'totangorb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install totangorb
20
+
21
+ ## Usage
22
+
23
+ Type:
24
+
25
+ $ rails g totangorb:install
26
+
27
+ It will generate an initializer for you
28
+
29
+ ```ruby
30
+ unless defined?($totango)
31
+ $totango = if Rails.env.production?
32
+ Totangorb::Tracker.new('1234xxxx')
33
+ else
34
+ Totangorb::Tracker.new('1234xxxx', debug: true, logger: Rails.logger)
35
+ end
36
+ end
37
+ ```
38
+
39
+ `debug: true` - do not make real HTTP requests - useful in development environment
40
+
41
+ You can also set your custom `logger`, such as `Rails.logger` to log every request made to Totango.
42
+
43
+ These parameters are totally optional.
44
+
45
+ Replace `1234xxxx` with your Totango API service id. From now you can make requests to Totango within your application:
46
+
47
+ ```ruby
48
+ $totango.track do |t|
49
+ t.username "Username"
50
+ t.account_id "1234"
51
+ t.account_name "Account name"
52
+ t.activity "Sample event"
53
+ t.module "Event module within application"
54
+ t.attributes {}
55
+ end
56
+ ```
57
+
58
+ Account attributes is optional - its a hash of custom attributes, you can really put whatever you want there, but remember to also set them in your Totango account.
59
+
60
+ For more informations, please visit [Totango Quick Start: HTTP API (Server side integration)](http://help.totango.com/installing-totango/quick-start-http-api-server-side-integration/)
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
69
+
70
+ ## Author
71
+ Michał Darda &copy; 2013 <michaldarda@gmail.com>
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ desc "Open an irb session preloaded with this library"
9
+ task :console do
10
+ sh "irb -rubygems -I lib -I extra -r ./lib/totangorb"
11
+ end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def to_h
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def to_h
3
+ {}
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class OpenStruct
2
+ def to_h
3
+ @table.dup
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ unless defined?($totango)
2
+ $totango = if Rails.env.production?
3
+ Totangorb::Tracker.new('1234xxxx')
4
+ else
5
+ Totangorb::Tracker.new('1234xxxx', debug: true, logger: Rails.logger)
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Totangorb
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates a Totangorb initializer."
7
+ def copy_initializer
8
+ template "totangorb.rb", "config/initializers/totangorb.rb"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../ext/nil_class'
2
+ require_relative '../ext/open_struct'
3
+ require_relative '../ext/hash'
4
+
5
+ require "totangorb/version"
6
+ require 'totangorb/tracker'
7
+ require 'totangorb/errors'
@@ -0,0 +1,3 @@
1
+ module Totangorb
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'ostruct'
2
+ require 'net/http'
3
+ require 'logger'
4
+
5
+ module Totangorb
6
+ class NullLogger
7
+ def info(msg) end
8
+ end
9
+
10
+ class Tracker
11
+ BASE_URL = 'http://sdr.totango.com/pixel.gif/?'
12
+
13
+ def initialize(service_id = nil, options = {})
14
+ raise(::Error, 'You must provide your private API key.') unless service_id
15
+ @service_id = service_id
16
+ @debug = options.fetch(:debug, false)
17
+ @logger = options.fetch(:logger) { NullLogger.new }
18
+
19
+ yield(self) if block_given?
20
+ end
21
+
22
+ def track(params = nil)
23
+ params ||= OpenStruct.new
24
+
25
+ yield(params) if block_given?
26
+
27
+ perform_request(params.to_h)
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :service_id, :debug, :logger
33
+
34
+ def perform_request(params = {})
35
+ uri = URI(BASE_URL)
36
+ uri.query = URI.encode_www_form(translate_params(params))
37
+
38
+ logger.info "Performing request to #{uri}"
39
+ Net::HTTP.get(uri) unless debug
40
+ end
41
+
42
+ # Translate params to names that totango understands
43
+ def translate_params(params = {})
44
+ {
45
+ sdr_s: service_id,
46
+ sdr_u: params[:username],
47
+ sdr_o: params[:account_id],
48
+ sdr_odn: params[:account_name],
49
+ sdr_a: params[:activity],
50
+ sdr_m: params[:module]
51
+ }.merge(parse_attributes(params.fetch(:attributes, {}))).delete_if { |k, v| v.nil? }
52
+ end
53
+
54
+ # Prepends every optional account attribute hash key with 'sdr_u.<key>'
55
+ def parse_attributes(attributes = {})
56
+ attributes.each.reduce({}) do |r, (k, v)|
57
+ r.merge("sdr_o.#{k}" => v)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Totangorb
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require_relative '../lib/totangorb'
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe Totangorb::Tracker do
4
+ describe '#initialize' do
5
+ it 'fails when service_id not provided' do
6
+ expect { Totangorb::Tracker.new }.to raise_error
7
+ end
8
+ end
9
+
10
+ describe '#track' do
11
+ before { @totango = Totangorb::Tracker.new("fake1234") }
12
+
13
+ describe 'invocation with block and hash' do
14
+ before do
15
+ @totango.should_receive(:perform_request).with({
16
+ username: 'Sample User',
17
+ account_id: 1,
18
+ account_name: 'Sample Account',
19
+ activity: 'Sample Activity',
20
+ module: 'Sample Module',
21
+ })
22
+ end
23
+
24
+ it 'takes a hash of params and passes it to perform_request' do
25
+ @totango.track({
26
+ username: 'Sample User',
27
+ account_id: 1,
28
+ account_name: 'Sample Account',
29
+ activity: 'Sample Activity',
30
+ module: 'Sample Module'
31
+ })
32
+ end
33
+
34
+ it 'takes a block, converts it to hash, and passes to perform_request' do
35
+ @totango.track do |t|
36
+ t.username = 'Sample User'
37
+ t.account_id = 1
38
+ t.account_name = 'Sample Account'
39
+ t.activity = 'Sample Activity'
40
+ t.module = 'Sample Module'
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'performing request' do
46
+ it 'decodes basic attributes' do
47
+ Net::HTTP.should_receive(:get).with(URI('http://sdr.totango.com/pixel.gif/?sdr_s=fake1234&sdr_u=Sample+User&sdr_o=1&sdr_odn=Sample+Account&sdr_a=Sample+Activity&sdr_m=Sample+Module'))
48
+
49
+ @totango.track do |t|
50
+ t.username = 'Sample User'
51
+ t.account_id = 1
52
+ t.account_name = 'Sample Account'
53
+ t.activity = 'Sample Activity'
54
+ t.module = 'Sample Module'
55
+ end
56
+ end
57
+
58
+ it 'decodes extra optional attributes' do
59
+ Net::HTTP.should_receive(:get).with(URI('http://sdr.totango.com/pixel.gif/?sdr_s=fake1234&sdr_u=Sample+User&sdr_o=1&sdr_odn=Sample+Account&sdr_a=Sample+Activity&sdr_m=Sample+Module&sdr_o.Sample+Attribute+1=Sample+Value+1&sdr_o.Sample+Attribute+2=Sample+Attribute+2'))
60
+
61
+ @totango.track do |t|
62
+ t.username = 'Sample User'
63
+ t.account_id = 1
64
+ t.account_name = 'Sample Account'
65
+ t.activity = 'Sample Activity'
66
+ t.module = 'Sample Module'
67
+ t.attributes = {
68
+ 'Sample Attribute 1' => 'Sample Value 1',
69
+ 'Sample Attribute 2' => 'Sample Attribute 2'
70
+ }
71
+ end
72
+ end
73
+ end
74
+
75
+ describe 'logging and debugging' do
76
+ it 'should log the output url and never make real http request' do
77
+ logger = double()
78
+ logger.should_receive(:info)
79
+ Net::HTTP.should_receive(:get).never
80
+ @totango = Totangorb::Tracker.new("fake1234", debug: true, logger: logger)
81
+
82
+ @totango.track do |t|
83
+ t.username = 'Sample User'
84
+ t.account_id = 1
85
+ t.account_name = 'Sample Account'
86
+ t.activity = 'Sample Activity'
87
+ t.module = 'Sample Module'
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'totangorb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "totangorb"
8
+ spec.version = Totangorb::VERSION
9
+ spec.authors = ["Michał Darda"]
10
+ spec.email = ["michaldarda@gmail.com"]
11
+ spec.description = %q{Lightweight Ruby wrapper for Totango.}
12
+ spec.summary = %q{Lightweight Ruby wrapper for Totango.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: totangorb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michał Darda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Lightweight Ruby wrapper for Totango.
56
+ email:
57
+ - michaldarda@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - ext/hash.rb
69
+ - ext/nil_class.rb
70
+ - ext/open_struct.rb
71
+ - lib/generators/templates/totangorb.rb
72
+ - lib/generators/totangorb/install_generator.rb
73
+ - lib/totangorb.rb
74
+ - lib/totangorb/errors.rb
75
+ - lib/totangorb/tracker.rb
76
+ - lib/totangorb/version.rb
77
+ - spec/spec_helper.rb
78
+ - spec/tracker_spec.rb
79
+ - totangorb.gemspec
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.14
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Lightweight Ruby wrapper for Totango.
104
+ test_files:
105
+ - spec/spec_helper.rb
106
+ - spec/tracker_spec.rb