sift-partner 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTU3NTgwNzFlMGY0ZTQyM2ExZDFkYTJmYjZmMmFlM2IxNzExOTZhYw==
5
+ data.tar.gz: !binary |-
6
+ YTA5MDY4M2MwOGIzZjAzODY3YmQzNjk1N2VhNGU3ODg0MjZmOGRlZg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MTU4NTBmY2MzOTNjN2JjMTYyZTFhMjUyNjNkYjgwMTZjMDdlMDdhZDMwYWRk
10
+ ZTIzZjJkZjViNTkxNDAxZjBjYTQ1Yjg2NmVlNWMzNmY4MjUyZGM5OTFkYTFj
11
+ ZWM0Yjg1OGIyN2ZlNDkwZjY2YTQ5ODllMjYxNWNkYTA1OGQ5ODU=
12
+ data.tar.gz: !binary |-
13
+ MTdkZmYyMDUyMDA4MmExMzEyOGUwYzJmNWU2OTdkZTJmMjA2YzY1MzRjOGE4
14
+ ZDdlMDg1MTM1YzYzNDQ4Njk3MGZmMmVkZDcxZTZlZWVmNWZkYTg1MzMxYmVk
15
+ MzRlMmJjN2YyODE3MzRjMDdmOGUxOTQyMWM1ZjE4Yjk0YTYxYTY=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sift.gemspec
4
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sift-partner (0.0.1)
5
+ httparty (>= 0.11.0)
6
+ multi_json (>= 1.0)
7
+ sift (>= 1.1.6.2)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ addressable (2.3.6)
13
+ crack (0.4.2)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.2.5)
16
+ httparty (0.13.1)
17
+ json (~> 1.8)
18
+ multi_xml (>= 0.5.2)
19
+ json (1.8.1)
20
+ multi_json (1.10.1)
21
+ multi_xml (0.5.5)
22
+ rake (10.3.2)
23
+ rspec (3.1.0)
24
+ rspec-core (~> 3.1.0)
25
+ rspec-expectations (~> 3.1.0)
26
+ rspec-mocks (~> 3.1.0)
27
+ rspec-core (3.1.4)
28
+ rspec-support (~> 3.1.0)
29
+ rspec-expectations (3.1.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.1.0)
32
+ rspec-mocks (3.1.1)
33
+ rspec-support (~> 3.1.0)
34
+ rspec-support (3.1.0)
35
+ safe_yaml (1.0.3)
36
+ sift (1.1.6.2)
37
+ httparty (>= 0.11.0)
38
+ multi_json (>= 1.0)
39
+ webmock (1.18.0)
40
+ addressable (>= 2.3.6)
41
+ crack (>= 0.3.2)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ rake
48
+ rspec (>= 2.14.1)
49
+ sift-partner!
50
+ webmock (>= 1.16.0)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Sift Science
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ = Sift Science Partner Ruby bindings
2
+
3
+ == Requirements
4
+
5
+ * Ruby 1.8.7 or above. (Ruby 1.8.6 might work if you load ActiveSupport.)
6
+ * HTTParty, 0.11.0 or greater
7
+ * Multi Json, 1.0 or greater
8
+ * sift, 1.1.6.2 or greater
9
+
10
+ For development only:
11
+ * bundler
12
+ * rspec, 2.14.1 or greater
13
+ * webmock, 1.16 or greater
14
+ * rake, any version
15
+
16
+ == Installation
17
+
18
+ If you want to build the gem from source:
19
+
20
+ $ gem build sift-partner.gemspec
21
+
22
+ Alternatively, you can install the gem from Rubyforge:
23
+
24
+ $ gem install sift-partner
25
+
26
+ == Usage
27
+
28
+ require "sift-partner"
29
+
30
+ # you can get these values from the console
31
+ partner_client = SiftPartner::Client.new(partner_api_key, partner_acct_id)
32
+
33
+ # create a new account for a given merchant
34
+ merchant_account = partner_client.new_account(
35
+ "merchantsite.com", # the url for the merchant's site
36
+ "shopowner@merchantsite.com", # an email belonging to the merchant
37
+ "johndoe@merchantsite.com", # an email used to log in to Sift
38
+ "s0m3l0ngp455w0rd" # password associated with that log in
39
+ )
40
+
41
+ # get a listing of all your accounts
42
+ all_accounts = partner_client.get_accounts
43
+
44
+ # config http notifications
45
+ cfg = {
46
+ :http_notification_threshold => 0.15,
47
+ :http_notification_url => "http://api.partner.com/notify?id=%s"
48
+ }
49
+ updated = partner_client.update_notification_config(cfg)
50
+
51
+
52
+ == Building
53
+
54
+ Building and publishing the gem is captured by the following steps:
55
+
56
+ $ gem build sift-partner.gemspec
57
+ $ gem push sift-partner<current version>.gem
58
+
59
+ $ bundle
60
+ $ rake -T
61
+ $ rake build
62
+ $ rake install
63
+ $ rake release
64
+
65
+ == Testing
66
+
67
+ To run the various tests use the rake command as follows:
68
+
69
+ $ rake spec
@@ -0,0 +1,14 @@
1
+ begin
2
+ require "bundler/gem_tasks"
3
+ Bundler::GemHelper.install_tasks
4
+ rescue LoadError => e
5
+ warn "It is recommended that you use bundler during development: gem install bundler"
6
+ end
7
+
8
+ require "rspec/core/rake_task"
9
+ task :default => :spec
10
+
11
+ desc "Run tests"
12
+ RSpec::Core::RakeTask.new(:spec) do |t|
13
+ t.pattern = "./spec/**/*_spec.rb"
14
+ end
@@ -0,0 +1,2 @@
1
+ require "sift-partner/client"
2
+ require "sift-partner/version"
@@ -0,0 +1,122 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'httparty'
4
+ require 'sift' # we use Response
5
+
6
+ module SiftPartner
7
+
8
+ # Ruby bindings for Sift Science's Partner API.
9
+ # For background and examples on how to use the Partner API with this client
10
+ # please refer to https://siftscience.com/resources/references/partner-ruby.html
11
+ class Client
12
+ API_ENDPOINT = "https://partner.siftscience.com/v3"
13
+ API_TIMEOUT = 2
14
+
15
+ #
16
+ # Constructor
17
+ # == Parameters:
18
+ # api_key
19
+ # The api_key of the partner
20
+ # (which may be found in the api_keys section of the console)
21
+ # id
22
+ # The account id of the partner
23
+ # (which may be found in the settings page of the console)
24
+ def initialize(api_key = Sift.api_key, id = Sift.account_id)
25
+ @api_key = api_key
26
+ @id = id
27
+ end
28
+
29
+ # Creates a new merchant account under the given partner.
30
+ # == Parameters:
31
+ # site_url
32
+ # the url of the merchant site
33
+ # site_email
34
+ # an email address for the merchant
35
+ # analyst_email
36
+ # an email address which will be used to log in at the Sift Console
37
+ # password
38
+ # password (at least 10 chars) to be used to sign into the Console
39
+ #
40
+ # When successful, returns a including the new account id and credentials.
41
+ # When an error occurs, returns nil.
42
+ def new_account(site_url, site_email, analyst_email, password)
43
+ reqBody = {:site_url => site_url, :site_email => site_email,
44
+ :analyst_email => analyst_email, :password => password}
45
+ begin
46
+ http_post(accounts_url(), reqBody)
47
+ rescue Exception => e
48
+ puts e.message
49
+ puts e.backtrace.inspect
50
+ end
51
+
52
+ end
53
+
54
+ # Gets a listing of the ids and keys for all merchant accounts that have
55
+ # been created by this partner.
56
+ #
57
+ # When successful, returns a hash including the key :data, which is an
58
+ # array of account descriptions. (Each element has the same structure as a
59
+ # single response from new_account).
60
+ def get_accounts()
61
+ http_get(accounts_url)
62
+ end
63
+
64
+ # Updates the configuration which controls http notifications for all merchant
65
+ # accounts under this partner.
66
+ #
67
+ # == Parameters
68
+ # cfg
69
+ # A Hash, with keys :http_notification_url and :http_notification_threshold
70
+ # The value of the notification_url will be a url containing the string '%s' exactly once.
71
+ # This allows the url to be used as a template, into which a merchant account id can be substituted.
72
+ # The notification threshold should be a floating point number between 0.0 and 1.0
73
+ def update_notification_config(cfg)
74
+ http_put(notification_config_url(), cfg)
75
+ end
76
+
77
+ private
78
+ def accounts_url
79
+ URI("#{API_ENDPOINT}/partners/#{@id}/accounts")
80
+ end
81
+
82
+ def notification_config_url
83
+ URI("#{API_ENDPOINT}/accounts/#{@id}/config")
84
+ end
85
+
86
+ def safe_json(http_response)
87
+ response = Sift::Response.new(http_response.body, http_response.code)
88
+ if !response.nil? and response.ok?
89
+ response.json
90
+ else
91
+ puts "bad value in safeJson :"
92
+ PP.pp(response)
93
+ end
94
+ end
95
+
96
+ def prep_https(uri)
97
+ https = Net::HTTP.new(uri.host, uri.port)
98
+ https.use_ssl = true
99
+ https
100
+ end
101
+
102
+ def http_get(uri)
103
+ header = {"Authorization" => "Basic #{@api_key}"}
104
+ http_response = HTTParty.get(uri, :headers =>header)
105
+ safe_json(http_response)
106
+ end
107
+
108
+ def http_put(uri, bodyObj)
109
+ header = {"Content-Type" => "application/json",
110
+ "Authorization" => "Basic #{@api_key}"}
111
+ http_response = HTTParty.put(uri, :body => bodyObj.to_json, :headers => header)
112
+ safe_json(http_response)
113
+ end
114
+
115
+ def http_post(uri, bodyObj)
116
+ header = {"Content-Type" => "application/json",
117
+ "Authorization" => "Basic #{@api_key}"}
118
+ http_response = HTTParty.post(uri, :body => bodyObj.to_json, :headers => header)
119
+ safe_json(http_response)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,3 @@
1
+ module SiftPartner
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sift-partner/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sift-partner"
7
+ s.version = SiftPartner::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Aaron Beppu"]
10
+ s.email = ["support@siftscience.com"]
11
+ s.homepage = "http://siftscience.com"
12
+ s.summary = %q{Sift Science Ruby Partner API Gem}
13
+ s.description = %q{Sift Science Ruby Partner API. Please see http://siftscience.com for more details.}
14
+
15
+ s.rubyforge_project = "sift-partner"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # Gems that must be intalled for sift to compile and build
23
+ s.add_development_dependency "rspec", ">=2.14.1"
24
+ s.add_development_dependency "webmock", ">= 1.16.0"
25
+
26
+ # Gems that must be intalled for sift to work
27
+ s.add_dependency "httparty", ">= 0.11.0"
28
+ s.add_dependency "multi_json", ">= 1.0"
29
+ s.add_dependency "sift", ">= 1.1.6.2"
30
+
31
+ s.add_development_dependency("rake")
32
+ end
@@ -0,0 +1,16 @@
1
+
2
+ require "bundler/setup"
3
+ require "sift-partner"
4
+ require "webmock/rspec"
5
+
6
+ # Setup Fakeweb
7
+ WebMock.disable_net_connect!
8
+
9
+ RSpec.configure do |config|
10
+ config.expect_with :rspec do |c|
11
+ c.syntax = :should
12
+ end
13
+ config.mock_with :rspec do |c|
14
+ c.syntax = :should
15
+ end
16
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ describe SiftPartner::Client do
4
+ partner_id = "65653548"
5
+ partner_api_key = "98463454389754"
6
+ expected_account_body = {
7
+ "account_id" => "1234567890abcdef",
8
+ "production" => {
9
+ "api_keys" => [
10
+ {
11
+ "id" => "54321abcdef",
12
+ "state" => "ACTIVE",
13
+ "key" => "fedcba0987654321"
14
+ }
15
+ ],
16
+ "beacon_keys" => [
17
+ {
18
+ "id" => "4321abcdef5",
19
+ "state" => "ACTIVE",
20
+ "key" => "edcba0987654321f"
21
+ }
22
+ ]
23
+ },
24
+ "sandbox" => {
25
+ "api_keys" => [
26
+ {
27
+ "id" => "321abcdef54",
28
+ "state" => "ACTIVE",
29
+ "key" => "dcba0987654321fe"
30
+ }
31
+ ],
32
+ "beacon_keys" => [
33
+ {
34
+ "id" => "21abcdef543",
35
+ "state" => "ACTIVE",
36
+ "key" => "cba0987654321fed"
37
+ }
38
+ ]
39
+ }
40
+ }
41
+
42
+ it "should march through create acct flow" do
43
+ site_url = "merchant123.com"
44
+ site_email = "owner@merchant123.com"
45
+ analyst_email = "analyst+merchant123@partner.com"
46
+ password = "s0m3l0ngp455w0rd"
47
+ # when we receive the mocked url, it will include the basic auth header encoded
48
+ # and regurgitated before the host name
49
+ stub_request(:post, /https:\/\/.*\@partner\.siftscience\.com\/v3\/partners\/#{partner_id}\/accounts/).
50
+ with { |request|
51
+ parsed_body = JSON.parse(request.body)
52
+ parsed_body.should include("site_url" => site_url)
53
+ parsed_body.should include("site_email" => site_email)
54
+ parsed_body.should include("analyst_email" => analyst_email)
55
+ parsed_body.should include("password" => password)
56
+ }.to_return({
57
+ :status => 200,
58
+ :headers => {},
59
+ :body => expected_account_body.to_json})
60
+ partner_client = SiftPartner::Client.new(partner_api_key, partner_id)
61
+
62
+ response = partner_client.new_account(site_url, site_email, analyst_email,
63
+ password)
64
+ response.should_not be_nil
65
+ response["production"]["api_keys"][0]["state"].should eq("ACTIVE")
66
+ end
67
+
68
+ it "should march through account listing flow" do
69
+ stub_request(:get, /https:\/\/.*partner\.siftscience\.com\/v3\/partners\/#{partner_id}\/accounts/).
70
+ to_return(
71
+ {:body =>
72
+ { "type" => "partner_account", "data" => [expected_account_body.to_json],
73
+ "hasMore" => false,
74
+ "nextRef" => nil,
75
+ "totalResults" => 1}.to_json,
76
+ :status => 200,
77
+ :headers => {}}
78
+ )
79
+ partner_client = SiftPartner::Client.new(partner_api_key, partner_id)
80
+ response = partner_client.get_accounts()
81
+ response.should_not be_nil
82
+ response["totalResults"].should eq(1)
83
+ end
84
+
85
+ it "should work through config update flow" do
86
+ stub_request(:put, /https:\/\/.*\@partner\.siftscience\.com\/v3\/accounts\/#{partner_id}\/config/).
87
+ with { |request|
88
+ parsed_body = JSON.parse(request.body)
89
+ parsed_body.should include("http_notification_threshold" => 0.1)
90
+ parsed_body.should include("http_notification_url" => "https://api.partners.com/notify?account=%s")
91
+ }.to_return({:status => 200, :headers => {},
92
+ :body => {
93
+ "email_notifiction_threshold" => 0.899,
94
+ "http_notification_url" => "https://api.partners.com/notify?account=%s",
95
+ "http_notification_threshold" => 0.1,
96
+ "is_production" => true,
97
+ "enable_sor_by_expected_loss" => false
98
+ }.to_json
99
+ })
100
+ partner_client = SiftPartner::Client.new(partner_api_key, partner_id)
101
+ cfg = {
102
+ "http_notification_url" => "https://api.partners.com/notify?account=%s",
103
+ "http_notification_threshold" => 0.1
104
+ }
105
+ response = partner_client.update_notification_config(cfg)
106
+ response.should_not be_nil
107
+ epsilon = 1e-6
108
+ response["http_notification_url"].should eq(cfg["http_notification_url"])
109
+ response["http_notification_threshold"].should < cfg["http_notification_threshold"] + epsilon
110
+ response["http_notification_threshold"].should > cfg["http_notification_threshold"] - epsilon
111
+
112
+ end
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sift-partner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Beppu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.14.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.16.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.16.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.11.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.11.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sift
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.6.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.6.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Sift Science Ruby Partner API. Please see http://siftscience.com for
98
+ more details.
99
+ email:
100
+ - support@siftscience.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE
108
+ - README.rdoc
109
+ - Rakefile
110
+ - lib/sift-partner.rb
111
+ - lib/sift-partner/client.rb
112
+ - lib/sift-partner/version.rb
113
+ - sift-partner.gemspec
114
+ - spec/spec_helper.rb
115
+ - spec/unit/client_spec.rb
116
+ homepage: http://siftscience.com
117
+ licenses: []
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project: sift-partner
135
+ rubygems_version: 2.1.11
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Sift Science Ruby Partner API Gem
139
+ test_files:
140
+ - spec/spec_helper.rb
141
+ - spec/unit/client_spec.rb