From 1c1f8e37d20694715fe2d0ea7186e12afef3d384 Mon Sep 17 00:00:00 2001 From: Herine Chin Date: Tue, 7 Jul 2015 17:35:54 -0700 Subject: [PATCH 1/4] add score tests --- spec/trakio/score_spec.rb | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 spec/trakio/score_spec.rb diff --git a/spec/trakio/score_spec.rb b/spec/trakio/score_spec.rb new file mode 100644 index 0000000..2c3e93e --- /dev/null +++ b/spec/trakio/score_spec.rb @@ -0,0 +1,75 @@ +require 'spec_helper' + +describe Trakio do + + subject { Trakio } + + after { + Trakio.default_instance = nil + } + + describe '#score' do + + 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"). + with(:body => { + key: 'my_api_secret_key', + distinct_id: 'a_person' + }).to_return(:body => { + status: 'success' + }.to_json) + + trakio = Trakio.new 'my_api_token', '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"). + with(:body => { + key: 'my_api_secret_key', + company_id: 'a_company' + }).to_return(:body => { + status: 'success' + }.to_json) + + trakio = Trakio.new 'my_api_token', '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', 'my_api_secret_key' + expect(trakio.score distinct_id: 'a_person', company_id: 'a_company').to have_been_requested + end + + end + + context "when neither distinct_id or company_id is provided" do + + it "raises an error" do + trakio = Trakio.new 'my_api_token', 'my_api_secret_key' + expect { trakio.score }.to raise_error Trakio::Exceptions::MissingParameter + end + + end + + end + +end From 678f7422f25ac5cdecab2d8fdff253f7f26b09a0 Mon Sep 17 00:00:00 2001 From: Herine Chin Date: Fri, 10 Jul 2015 12:08:06 -0700 Subject: [PATCH 2/4] add invalid key exception and tests --- lib/trakio_client.rb | 17 ++++++++++++++++- lib/trakio_client/end_point.rb | 14 +++++++++++--- lib/trakio_client/exceptions.rb | 1 + lib/trakio_client/score.rb | 25 +++++++++++++++++++++++++ spec/trakio/initialize_spec.rb | 13 +++++++++++++ spec/trakio/score_spec.rb | 31 ++++++++++++++++++------------- 6 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 lib/trakio_client/score.rb diff --git a/lib/trakio_client.rb b/lib/trakio_client.rb index 99c7116..8a1b978 100644 --- a/lib/trakio_client.rb +++ b/lib/trakio_client.rb @@ -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" @@ -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 @@ -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 @@ -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) diff --git a/lib/trakio_client/end_point.rb b/lib/trakio_client/end_point.rb index c85ba66..189371c 100644 --- a/lib/trakio_client/end_point.rb +++ b/lib/trakio_client/end_point.rb @@ -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 @@ -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' diff --git a/lib/trakio_client/exceptions.rb b/lib/trakio_client/exceptions.rb index 820fbc4..4630472 100644 --- a/lib/trakio_client/exceptions.rb +++ b/lib/trakio_client/exceptions.rb @@ -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 diff --git a/lib/trakio_client/score.rb b/lib/trakio_client/score.rb new file mode 100644 index 0000000..d0f5598 --- /dev/null +++ b/lib/trakio_client/score.rb @@ -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 \ No newline at end of file diff --git a/spec/trakio/initialize_spec.rb b/spec/trakio/initialize_spec.rb index f8bc723..ec08199 100644 --- a/spec/trakio/initialize_spec.rb +++ b/spec/trakio/initialize_spec.rb @@ -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 diff --git a/spec/trakio/score_spec.rb b/spec/trakio/score_spec.rb index 2c3e93e..38d6c69 100644 --- a/spec/trakio/score_spec.rb +++ b/spec/trakio/score_spec.rb @@ -10,18 +10,24 @@ 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"). - with(:body => { - key: 'my_api_secret_key', - distinct_id: 'a_person' + 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', 'my_api_secret_key' + 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' @@ -34,15 +40,14 @@ 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"). - with(:body => { - key: 'my_api_secret_key', - company_id: 'a_company' + 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', 'my_api_secret_key' + 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' @@ -55,8 +60,8 @@ 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', 'my_api_secret_key' - expect(trakio.score distinct_id: 'a_person', company_id: 'a_company').to have_been_requested + 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 @@ -64,7 +69,7 @@ context "when neither distinct_id or company_id is provided" do it "raises an error" do - trakio = Trakio.new 'my_api_token', 'my_api_secret_key' + trakio = Trakio.new 'my_api_token', api_secret_key: 'my_api_secret_key' expect { trakio.score }.to raise_error Trakio::Exceptions::MissingParameter end From 02a3748e7cd782af9573bb237e3217c35e53d8d5 Mon Sep 17 00:00:00 2001 From: Herine Chin Date: Fri, 10 Jul 2015 12:31:43 -0700 Subject: [PATCH 3/4] add score example to readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 8191777..49282a7 100644 --- a/README.md +++ b/README.md @@ -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' } + + # 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' } +``` + ## 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. From 2eba93d2d148c3c84c9cf9671d949c2ed85ceea1 Mon Sep 17 00:00:00 2001 From: Herine Chin Date: Fri, 10 Jul 2015 12:33:41 -0700 Subject: [PATCH 4/4] update score response example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 49282a7..97db3e9 100644 --- a/README.md +++ b/README.md @@ -91,13 +91,13 @@ For more indepth documentation see: http://docs.trak.io/ruby.html 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' } + # 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' } + # 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