├── Gemfile ├── lib ├── attribution │ ├── version.rb │ └── client.rb └── attribution.rb ├── spec ├── helper.rb └── lib │ └── client_spec.rb ├── README.md ├── attribution.gemspec └── Gemfile.lock /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | gemspec 3 | -------------------------------------------------------------------------------- /lib/attribution/version.rb: -------------------------------------------------------------------------------- 1 | module Attribution 2 | VERSION = '0.0.2' 3 | end 4 | -------------------------------------------------------------------------------- /lib/attribution.rb: -------------------------------------------------------------------------------- 1 | require "attribution/version" 2 | require "attribution/client" 3 | require "json" 4 | 5 | module Attribution 6 | end 7 | -------------------------------------------------------------------------------- /spec/helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RACK_ENV'] ||= 'test' 2 | 3 | require 'rspec' 4 | require 'webmock/rspec' 5 | 6 | WebMock.disable_net_connect!(allow_localhost: true) 7 | 8 | require './lib/attribution' 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # attribution-ruby 2 | 3 | attribution = Attribution::Client.new(ENV['PROJECT_ID']) 4 | attribution.track( 5 | event: "Charge Credit Card", 6 | user_id: USER_ID, 7 | properties: { 8 | revenue: 14.99 9 | } 10 | ) 11 | 12 | attribution.alias( 13 | user_id: USER_ID, 14 | previous_id: PREVIOUS_ID 15 | ) 16 | 17 | attribution.identify( 18 | user_id: USER_ID, 19 | traits: { 20 | email: 'test@example.com' 21 | } 22 | ) 23 | -------------------------------------------------------------------------------- /attribution.gemspec: -------------------------------------------------------------------------------- 1 | require File.expand_path('../lib/attribution/version', __FILE__) 2 | 3 | Gem::Specification.new do |spec| 4 | spec.name = 'attribution' 5 | spec.version = Attribution::VERSION 6 | spec.files = Dir.glob('**/*') 7 | spec.require_paths = ['lib'] 8 | spec.summary = 'Attribution analytics library' 9 | spec.description = 'The attributionapp.com ruby analytics library' 10 | spec.authors = ['attributionapp.com'] 11 | spec.email = 'support@attributionapp.com' 12 | spec.homepage = 'https://github.com/attribution/attribution-ruby' 13 | spec.license = 'MIT' 14 | 15 | # Ruby 1.8 requires json 16 | spec.add_dependency 'json', ['~> 1.7'] if RUBY_VERSION < "1.9" 17 | spec.add_dependency 'faraday', ['~> 0.9'] 18 | 19 | spec.add_development_dependency 'rspec', '~> 2.0' 20 | spec.add_development_dependency 'webmock', '~> 1.13' 21 | end 22 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | attribution (0.0.1) 5 | faraday (~> 0.9) 6 | 7 | GEM 8 | remote: http://rubygems.org/ 9 | specs: 10 | addressable (2.4.0) 11 | crack (0.4.3) 12 | safe_yaml (~> 1.0.0) 13 | diff-lcs (1.2.5) 14 | faraday (0.9.2) 15 | multipart-post (>= 1.2, < 3) 16 | hashdiff (0.2.3) 17 | multipart-post (2.0.0) 18 | rspec (2.99.0) 19 | rspec-core (~> 2.99.0) 20 | rspec-expectations (~> 2.99.0) 21 | rspec-mocks (~> 2.99.0) 22 | rspec-core (2.99.2) 23 | rspec-expectations (2.99.2) 24 | diff-lcs (>= 1.1.3, < 2.0) 25 | rspec-mocks (2.99.4) 26 | safe_yaml (1.0.4) 27 | webmock (1.22.6) 28 | addressable (>= 2.3.6) 29 | crack (>= 0.3.2) 30 | hashdiff 31 | 32 | PLATFORMS 33 | ruby 34 | 35 | DEPENDENCIES 36 | attribution! 37 | rspec (~> 2.0) 38 | webmock (~> 1.13) 39 | -------------------------------------------------------------------------------- /lib/attribution/client.rb: -------------------------------------------------------------------------------- 1 | require 'faraday' 2 | module Attribution 3 | class Client 4 | def base_url 5 | 'https://track.attributionapp.com' 6 | end 7 | 8 | def initialize(project_id) 9 | @project_id = project_id 10 | end 11 | 12 | def track(data) 13 | post('/track', data) 14 | end 15 | 16 | def alias(previous_id:, user_id:) 17 | post('/alias', { user_id: user_id, previous_id: previous_id }) 18 | end 19 | 20 | def identify(data) 21 | post('/identify', data) 22 | end 23 | 24 | def post(path, payload_hash) 25 | conn = Faraday.new(:url => base_url) do |faraday| 26 | faraday.adapter Faraday.default_adapter # make requests with Net::HTTP 27 | faraday.basic_auth @project_id, "" 28 | end 29 | response = conn.post do |req| 30 | req.url path 31 | req.headers['Content-Type'] = 'application/json' 32 | req.headers['User-Agent'] = 'Attribution-Ruby/0.0.1' 33 | req.body = payload_hash.to_json 34 | end 35 | raise "#{response.inspect}" unless response.success? 36 | response 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /spec/lib/client_spec.rb: -------------------------------------------------------------------------------- 1 | require './spec/helper' 2 | 3 | describe Attribution::Client do 4 | describe "#track" do 5 | let(:client) { Attribution::Client.new('12345') } 6 | let(:status) { 200 } 7 | let(:body) { "" } 8 | 9 | describe "#track" do 10 | before do 11 | stub_request(:post, "https://12345:@track.attributionapp.com/track"). 12 | with(:body => {"event"=>"Ate a Pizza", "properties"=>{"revenue"=>14.99}, "user_id"=>"user_123"}, 13 | :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Attribution-Ruby/0.0.1'}). 14 | to_return(:status => status, :body => body, :headers => {}) 15 | end 16 | 17 | it "makes track call" do 18 | response = client.track(event: "Ate a Pizza", user_id: 'user_123', properties: { revenue: 14.99 }) 19 | expect(response).to be_success 20 | end 21 | 22 | context "when there is an error" do 23 | let(:status) { 401 } 24 | it "shows error" do 25 | expect { 26 | client.track(event: "Ate a Pizza", user_id: 'user_123', properties: { revenue: 14.99 }) 27 | }.to raise_error 28 | end 29 | end 30 | end 31 | 32 | describe "#alias" do 33 | before do 34 | stub_request(:post, "https://12345:@track.attributionapp.com/alias"). 35 | with(:body => {"user_id"=>"12345", "previous_id"=> "12"}, 36 | :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Attribution-Ruby/0.0.1'}). 37 | to_return(:status => status, :body => body, :headers => {}) 38 | end 39 | 40 | it "makes alias call" do 41 | response = client.alias(user_id: "12345", previous_id: "12") 42 | expect(response).to be_success 43 | end 44 | 45 | context "when there is an error" do 46 | let(:status) { 401 } 47 | it "shows error" do 48 | expect { 49 | client.alias(previous_id: "12345", user_id: "12") 50 | }.to raise_error 51 | end 52 | end 53 | end 54 | 55 | describe "#identify" do 56 | before do 57 | stub_request(:post, "https://12345:@track.attributionapp.com/identify"). 58 | with(:body => {"user_id"=>"12345", "traits"=> { "email" => "test@example.com" }}, 59 | :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Attribution-Ruby/0.0.1'}). 60 | to_return(:status => status, :body => body, :headers => {}) 61 | end 62 | 63 | it "makes alias call" do 64 | response = client.identify(user_id: "12345", traits: { email: "test@example.com" }) 65 | expect(response).to be_success 66 | end 67 | 68 | context "when there is an error" do 69 | let(:status) { 401 } 70 | it "shows error" do 71 | expect { 72 | client.identify(user_id: "12345", traits: { email: "test@example.com" }) 73 | }.to raise_error 74 | end 75 | end 76 | end 77 | 78 | end 79 | end 80 | --------------------------------------------------------------------------------