From 460ba249ab5c555eeca0ac76ea80b159bfe5dd77 Mon Sep 17 00:00:00 2001 From: nickhurlburt Date: Tue, 19 Jul 2016 13:53:05 -0700 Subject: [PATCH] Change partner ruby client to return Sift::Response (#11) Change partner ruby client to return response --- Gemfile.lock | 2 +- LICENSE | 2 +- README.rdoc | 6 +++++- lib/sift-partner/client.rb | 30 ++++++++++++------------------ lib/sift-partner/version.rb | 2 +- spec/unit/client_spec.rb | 25 ++++++++++++------------- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d65f33c..88e5b2e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - sift-partner (0.0.3) + sift-partner (0.1.0) httparty (~> 0.13.1) multi_json (~> 1.0) sift (~> 1.1.7) diff --git a/LICENSE b/LICENSE index b41cc7f..20f97ac 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2014 Sift Science +Copyright (c) 2014-2016 Sift Science Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rdoc b/README.rdoc index d193e68..f9f7e01 100644 --- a/README.rdoc +++ b/README.rdoc @@ -31,13 +31,17 @@ Alternatively, you can install the gem from Rubyforge: partner_client = SiftPartner::Client.new(partner_api_key, partner_acct_id) # create a new account for a given merchant - merchant_account = partner_client.new_account( + merchant_account_response = partner_client.new_account( "merchantsite.com", # the url for the merchant's site "shopowner@merchantsite.com", # an email belonging to the merchant "johndoe@merchantsite.com", # an email used to log in to Sift "s0m3l0ngp455w0rd" # password associated with that log in ) + merchant_account_response.ok? # returns true or false + merchant_account_response.body # API response body + merchant_account_response.http_status_code # HTTP response code, 200 is ok. + # get a listing of all your accounts all_accounts = partner_client.get_accounts diff --git a/lib/sift-partner/client.rb b/lib/sift-partner/client.rb index de3890d..b55013d 100644 --- a/lib/sift-partner/client.rb +++ b/lib/sift-partner/client.rb @@ -39,8 +39,7 @@ def initialize(api_key = Sift.api_key, id = Sift.account_id) # password # password (at least 10 chars) to be used to sign into the Console # - # When successful, returns a including the new account id and credentials. - # When an error occurs, returns nil. + # Returns a Sift::Response object (see https://github.com/SiftScience/sift-ruby) def new_account(site_url, site_email, analyst_email, password) raise("site url must be a non-empty string") unless valid_string?(site_url) @@ -63,11 +62,12 @@ def new_account(site_url, site_email, analyst_email, password) # been created by this partner. This will return up to 100 results # at a time. # - # When successful, returns a hash including the key :data, which is an - # array of account descriptions. (Each element has the same structure as a - # single response from new_account). If the key :has_more is true, then - # pass the :next_ref value into this function again to get the next set - # of results. + # Returns a Sift::Response object (see https://github.com/SiftScience/sift-ruby) + # When successful, the response body is a hash including the key :data, + # which is an array of account descriptions. (Each element has the same + # structure as a single response from new_account.) If + # the key :has_more is true, then pass the :next_ref value into this + # function again to get the next set of results. def get_accounts(next_ref = nil) http_get(next_ref ? next_ref : accounts_url) end @@ -80,7 +80,7 @@ def get_accounts(next_ref = nil) # A Hash, with keys :http_notification_url and :http_notification_threshold # The value of the notification_url will be a url containing the string '%s' exactly once. # This allows the url to be used as a template, into which a merchant account id can be substituted. - # The notification threshold should be a floating point number between 0.0 and 1.0 + # The notification threshold should be a floating point number between 0.0 and 1.0 def update_notification_config(cfg = nil) raise("configuration must be a hash") unless cfg.is_a? Hash @@ -101,18 +101,12 @@ def notification_config_url URI("#{API_ENDPOINT}/accounts/#{@id}/config") end - def safe_json(http_response) + def sift_response(http_response) response = Sift::Response.new( http_response.body, http_response.code, http_response.response ) - if !response.nil? and response.ok? - response.json - else - puts "bad value in safeJson :" - PP.pp(response) - end end def prep_https(uri) @@ -126,7 +120,7 @@ def http_get(uri) "User-Agent" => user_agent} http_response = HTTParty.get(uri, :headers =>header) - safe_json(http_response) + sift_response(http_response) end def http_put(uri, bodyObj) @@ -135,7 +129,7 @@ def http_put(uri, bodyObj) "User-Agent" => user_agent} http_response = HTTParty.put(uri, :body => bodyObj.to_json, :headers => header) - safe_json(http_response) + sift_response(http_response) end def http_post(uri, bodyObj) @@ -143,7 +137,7 @@ def http_post(uri, bodyObj) "Authorization" => "Basic #{@api_key}", "User-Agent" => user_agent} http_response = HTTParty.post(uri, :body => bodyObj.to_json, :headers => header) - safe_json(http_response) + sift_response(http_response) end def valid_string?(s) diff --git a/lib/sift-partner/version.rb b/lib/sift-partner/version.rb index 03d6ca0..3642015 100644 --- a/lib/sift-partner/version.rb +++ b/lib/sift-partner/version.rb @@ -1,4 +1,4 @@ module SiftPartner - VERSION = "0.0.4" + VERSION = "0.1.0" API_VERSION = "3" end diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index a3ed7d0..6ae3ed4 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -119,8 +119,8 @@ response = partner_client.new_account(site_url, site_email, analyst_email, password) response.should_not be_nil - response["production"]["api_keys"][0]["state"].should eq("ACTIVE") - end + response.body["production"]["api_keys"][0]["state"].should eq("ACTIVE") + end it "should march through account listing flow" do stub_request(:get, /https:\/\/.*partner\.siftscience\.com\/v3\/partners\/#{partner_id}\/accounts\z/). @@ -147,16 +147,16 @@ partner_client = SiftPartner::Client.new(partner_api_key, partner_id) response = partner_client.get_accounts() response.should_not be_nil - response["nextRef"].should_not be_nil - response["hasMore"].should be_truthy - response["totalResults"].should eq(2) - next_ref = response["nextRef"] + response.body["nextRef"].should_not be_nil + response.body["hasMore"].should be_truthy + response.body["totalResults"].should eq(2) + next_ref = response.body["nextRef"] response = partner_client.get_accounts(next_ref) response.should_not be_nil - response["hasMore"].should be_falsey - response["nextRef"].should be_nil - response["totalResults"].should eq(2) + response.body["hasMore"].should be_falsey + response.body["nextRef"].should be_nil + response.body["totalResults"].should eq(2) end @@ -190,10 +190,9 @@ response = partner_client.update_notification_config(cfg) response.should_not be_nil epsilon = 1e-6 - response["http_notification_url"].should eq(cfg["http_notification_url"]) - response["http_notification_threshold"].should < cfg["http_notification_threshold"] + epsilon - response["http_notification_threshold"].should > cfg["http_notification_threshold"] - epsilon - + response.body["http_notification_url"].should eq(cfg["http_notification_url"]) + response.body["http_notification_threshold"].should < cfg["http_notification_threshold"] + epsilon + response.body["http_notification_threshold"].should > cfg["http_notification_threshold"] - epsilon end end