Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ For more indepth documentation see: http://docs.trak.io/ruby.html
# resp will look like { 'status': 'success' }
```

### Creating an instance and using score
```ruby
# set token and key on default instance
Trakio.init 'my_api_token', api_secret_key: 'my_api_secret_key'

resp = Trakio.score distinct_id: 'a_distinct_id'
# resp will look like { "status": "success", "score": 333, "score_history": { "2015-06-09": 333, "2015-06-08": 333, "2015-06-07": 333 } }

# create the instance
trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key'

resp = trakio.score distinct_id: 'a_distinct_id'
# resp will look like { "status": "success", "score": 333, "score_history": { "2015-06-09": 333, "2015-06-08": 333, "2015-06-07": 333 } }
```

## Creating and Running Tests
* Tests can be run by running the following commands `bundle exec rspec`
* Tests can be added by either adding into an existing spec file, or creating a new one.
Expand Down
17 changes: 16 additions & 1 deletion lib/trakio_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "trakio_client/exceptions"
require "trakio_client/identify"
require "trakio_client/track"
require "trakio_client/score"
require "trakio_client/version"
require "rest_client"
require "json"
Expand All @@ -16,6 +17,7 @@ module TrakioClient
def self.included base
base.extend ClassMethods
base.send :attr_accessor, :api_token
base.send :attr_accessor, :api_secret_key
base.send :attr_accessor, :https
base.send :attr_accessor, :host
base.send :attr_accessor, :channel
Expand Down Expand Up @@ -68,11 +70,12 @@ def initialize(*args)
@https = true
@host = 'api.trak.io/v1'

%w{https host channel distinct_id company_id}.each do |name|
%w{https host channel distinct_id company_id api_secret_key}.each do |name|
instance_variable_set("@#{name}", params[name.to_sym]) if params && params.has_key?(name.to_sym)
end
end

# write api endpoints
['Alias', 'Annotate', 'Company', 'Identify', 'Track'].each do |method_object|
TrakioClient.module_eval "
def #{method_object.downcase} *args
Expand All @@ -82,6 +85,18 @@ def #{method_object.downcase} *args
"
end

# read api endpoints
# checks for api_secret_key before running
['Score'].each do |method_object|
TrakioClient.module_eval "
def #{method_object.downcase} *args
raise Exceptions::InvalidKey.new('Missing API Secret Key') unless api_secret_key
@#{method_object.downcase} ||= TrakioClient::#{method_object}.new(self)
@#{method_object.downcase}.run(*args)
end
"
end

def page_view *args
@track ||= Track.new(self)
@track.page_view(*args)
Expand Down
14 changes: 11 additions & 3 deletions lib/trakio_client/end_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class EndPoint

attr_accessor :trakio
def_delegator :@trakio, :api_token
def_delegator :@trakio, :api_secret_key
def_delegator :@trakio, :https
def_delegator :@trakio, :host
def_delegator :@trakio, :channel
Expand All @@ -17,11 +18,18 @@ def initialize trakio

protected

def send_request endpoint, params
def send_request endpoint, params, use_key=false
protocol = https ? "https" : "http"
url = "#{protocol}://#{host}/#{endpoint}"
data = { token: api_token, data: params }.to_json
resp = RestClient.post url, data, :content_type => :json, :accept => :json

if use_key
params_url = "?#{params.keys.first}=#{params.values.first}"
resp = RestClient.get url + params_url, key: api_secret_key
else
data = { token: api_token, data: params }.to_json
resp = RestClient.post url, data, :content_type => :json, :accept => :json
end

result = JSON.parse(resp.body, :symbolize_names => true)
return result if result[:status] == 'success'

Expand Down
1 change: 1 addition & 0 deletions lib/trakio_client/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DataObjectInvalidJson < StandardError; end
class DataObjectInvalidBase64 < StandardError; end
class DataObjectInvalidType < StandardError; end
class InvalidToken < StandardError; end
class InvalidKey < StandardError; end
class MissingParameter < StandardError; end
class InvalidParameter < StandardError; end
class MissingProperty < StandardError; end
Expand Down
25 changes: 25 additions & 0 deletions lib/trakio_client/score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module TrakioClient
class Score < EndPoint

def run p = {}
distinct_id = p[:distinct_id]
company_id = p[:company_id]
check_parameters distinct_id, company_id

params = {}
params[:distinct_id] = distinct_id unless distinct_id.nil?
params[:company_id] = company_id unless company_id.nil?
send_request('score', params, true)
end

def check_parameters distinct_id, company_id
if distinct_id.nil? && company_id.nil?
raise Exceptions::MissingParameter.new("Either a `company_id` or `distinct_id` parameter must be provided.")
end
if distinct_id && company_id
raise Exceptions::InvalidParameter.new("Either a 'distinct_id' or 'company_id' parameter must be provided but not both")
end
end

end
end
13 changes: 13 additions & 0 deletions spec/trakio/initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
expect(trakio.api_token).to eql 'my_api_token'
end

context "when a API secret key is provided" do

it "creates a new Trakio instance" do
expect(Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key').to be_a Trakio
end

it "sets the API token for this instance" do
trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key'
expect(trakio.api_token).to eql 'my_api_token'
expect(trakio.api_secret_key).to eql 'my_api_secret_key'
end

end

context "when a channel is provided" do
it "sets channel for this instance" do
Expand Down
80 changes: 80 additions & 0 deletions spec/trakio/score_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'spec_helper'

describe Trakio do

subject { Trakio }

after {
Trakio.default_instance = nil
}

describe '#score' do

context "when no api secret key" do
it "raises an error" do
trakio = Trakio.new 'my_api_token'
expect { trakio.score distinct_id: 'a_person' }.to raise_error Trakio::Exceptions::InvalidKey
end
end

context "when a distinct_id is provided as an argument" do

it "sends a score request to api.trak.io" do
stub = stub_request(:get, "https://api.trak.io/v1/score?distinct_id=a_person").
with(:headers => {
key: 'my_api_secret_key'
}).to_return(:body => {
status: 'success'
}.to_json)

trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key'
resp = trakio.score distinct_id: 'a_person'

expect(resp[:status]).to eql 'success'

expect(stub).to have_been_requested
end

end

context "when a company_id is provided as an argument" do

it "sends a score request to api.trak.io" do
stub = stub_request(:get, "https://api.trak.io/v1/score?company_id=a_company").
with(:headers => {
key: 'my_api_secret_key'
}).to_return(:body => {
status: 'success'
}.to_json)

trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key'
resp = trakio.score company_id: 'a_company'

expect(resp[:status]).to eql 'success'

expect(stub).to have_been_requested
end

end

context "when both a company_id and distinct_id are provided as arguments" do

it "raises an error" do
trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key'
expect{ trakio.score distinct_id: 'a_person', company_id: 'a_company' }.to raise_error Trakio::Exceptions::InvalidParameter
end

end

context "when neither distinct_id or company_id is provided" do

it "raises an error" do
trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key'
expect { trakio.score }.to raise_error Trakio::Exceptions::MissingParameter
end

end

end

end