Skip to content

Commit

Permalink
Change partner ruby client to return Sift::Response (#11)
Browse files Browse the repository at this point in the history
Change partner ruby client to return response
  • Loading branch information
nickhurlburt authored Jul 19, 2016
1 parent 8125c9c commit 460ba24
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
"[email protected]", # an email belonging to the merchant
"[email protected]", # 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

Expand Down
30 changes: 12 additions & 18 deletions lib/sift-partner/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -135,15 +129,15 @@ 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)
header = {"Content-Type" => "application/json",
"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)
Expand Down
2 changes: 1 addition & 1 deletion lib/sift-partner/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SiftPartner
VERSION = "0.0.4"
VERSION = "0.1.0"
API_VERSION = "3"
end
25 changes: 12 additions & 13 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand All @@ -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


Expand Down Expand Up @@ -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

0 comments on commit 460ba24

Please sign in to comment.