diff --git a/README.md b/README.md index 8191777..97db3e9 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", "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. 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 new file mode 100644 index 0000000..38d6c69 --- /dev/null +++ b/spec/trakio/score_spec.rb @@ -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