leapfrog-customer_scoring 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.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/leapfrog-customer_scoring.gemspec +27 -0
- data/lib/leapfrog/customer_scoring/client.rb +57 -0
- data/lib/leapfrog/customer_scoring/exceptions.rb +27 -0
- data/lib/leapfrog/customer_scoring/version.rb +5 -0
- data/lib/leapfrog/customer_scoring.rb +10 -0
- data/spec/leapfrog/customer_scoring/client_spec.rb +104 -0
- data/spec/spec_helper.rb +8 -0
- metadata +165 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Paul Sorensen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Leapfrog::CustomerScoring
|
2
|
+
|
3
|
+
[](https://travis-ci.org/paulnsorensen/leapfrog-customer_scoring)
|
4
|
+
[](https://gemnasium.com/paulnsorensen/leapfrog-customer_scoring)
|
5
|
+
[](https://codeclimate.com/github/paulnsorensen/leapfrog-customer_scoring)
|
6
|
+
|
7
|
+
|
8
|
+
Ruby API client for Leapfrog Online customer scoring
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'leapfrog-customer_scoring'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install leapfrog-customer_scoring
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Clients are instantiated with `http://internal.leapfrogonline/customer_scoring` as the default URL.
|
27
|
+
|
28
|
+
client = Leapfrog::CustomerScoring::Client.new
|
29
|
+
|
30
|
+
You may optionally pass a different URL to use.
|
31
|
+
|
32
|
+
client = Leapfrog::CustomerScoring::Client.new("http://example.com")
|
33
|
+
|
34
|
+
To retrieve the scoring advice for a customer, you will call the `get_score(income, zipcode, age)` method. The response will be a `Hash` with two symbolized keys containing the `:propensity` and `:ranking` for that customer.
|
35
|
+
|
36
|
+
advice = client.get_score("50000", "60621", "35")
|
37
|
+
advice.inspect
|
38
|
+
=> "{:propensity=>0.26532, :ranking=>\"C\"}"
|
39
|
+
|
40
|
+
## Testing
|
41
|
+
|
42
|
+
To test this gem, you can checkout the master branch and run the `rake`
|
43
|
+
in your console.
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'leapfrog/customer_scoring/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "leapfrog-customer_scoring"
|
8
|
+
spec.version = Leapfrog::CustomerScoring::VERSION
|
9
|
+
spec.authors = ["Paul Sorensen"]
|
10
|
+
spec.email = ["paulnsorensen@gmail.com"]
|
11
|
+
spec.description = %q{Ruby API client for Leapfrog Online customer scoring}
|
12
|
+
spec.summary = %q{Ruby API client for Leapfrog Online customer scoring}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "webmock", "~> 1.14.0"
|
25
|
+
spec.add_runtime_dependency "rest-client", "~> 1.6.7"
|
26
|
+
spec.add_runtime_dependency "json", "~> 1.8.0"
|
27
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Leapfrog
|
2
|
+
module CustomerScoring
|
3
|
+
class Client
|
4
|
+
DEFAULT_URL = "http://internal.leapfrogonline.com/customer_scoring"
|
5
|
+
|
6
|
+
# You may initialize the Client with any url or just use the default
|
7
|
+
# url of "http://internal.leapfrogonline.com/customer_scoring".
|
8
|
+
#
|
9
|
+
# client = Leapfrog::CustomerScoring::Client.new
|
10
|
+
#
|
11
|
+
# -- OR--
|
12
|
+
#
|
13
|
+
# other_url = "http://example.com/customer_scoring"
|
14
|
+
# client = Leapfrog::CustomerScoring::Client.new(other_url)
|
15
|
+
#
|
16
|
+
# This is the url to which the client will make requests
|
17
|
+
def initialize(url="")
|
18
|
+
@url = url.empty? ? DEFAULT_URL : url
|
19
|
+
end
|
20
|
+
|
21
|
+
# Makes request to the initialized endpoint to retrieve customer
|
22
|
+
# scoring advice. The <tt>income</tt>, <tt>zipcode</tt> and
|
23
|
+
# <tt>age</tt> parameters are required. The return value is a
|
24
|
+
# <tt>Hash</tt> with keys containing the <tt>:propensity</tt>
|
25
|
+
# and <tt>:ranking </tt> for customer with the passed parameters
|
26
|
+
#
|
27
|
+
# advice = client.get_score("50000", "60621", "35")
|
28
|
+
# advice.inspect
|
29
|
+
# => "{:propensity=>0.26532, :ranking=>\"C\"}"
|
30
|
+
#
|
31
|
+
def get_score(income, zipcode, age)
|
32
|
+
params = {income: income, zipcode: zipcode, age: age}
|
33
|
+
begin
|
34
|
+
RestClient.get(url, params: params) do |response, request, result|
|
35
|
+
case response.code
|
36
|
+
when 200
|
37
|
+
return ::JSON.parse(response, symbolize_names: true)
|
38
|
+
when 422
|
39
|
+
raise Leapfrog::CustomerScoring::InvalidInput
|
40
|
+
when 404
|
41
|
+
raise Leapfrog::CustomerScoring::ResourceNotFound
|
42
|
+
when 500
|
43
|
+
raise Leapfrog::CustomerScoring::ServerError
|
44
|
+
else
|
45
|
+
response.return!(request, result)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
rescue RestClient::RequestTimeout
|
49
|
+
raise Leapfrog::CustomerScoring::ServerTimeout
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
attr_reader :url
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Leapfrog
|
2
|
+
module CustomerScoring
|
3
|
+
class InvalidInput < Exception
|
4
|
+
def initialize(data={})
|
5
|
+
@data = data
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class ResourceNotFound < Exception
|
10
|
+
def initialize(data={})
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ServerError < Exception
|
16
|
+
def initialize(data={})
|
17
|
+
@data = data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class ServerTimeout < Exception
|
22
|
+
def initialize(data={})
|
23
|
+
@data = data
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Leapfrog::CustomerScoring do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "should have the correct default url" do
|
6
|
+
client = Leapfrog::CustomerScoring::Client.new
|
7
|
+
expect(client.send(:url)).to eql(Leapfrog::CustomerScoring::Client::DEFAULT_URL)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should allow another url to be specified" do
|
11
|
+
client = Leapfrog::CustomerScoring::Client.new("http://paulnsorensen.com")
|
12
|
+
expect(client.send(:url)).to eql("http://paulnsorensen.com")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#get_score" do
|
17
|
+
context "when successful" do
|
18
|
+
before(:all) do
|
19
|
+
test_url = Leapfrog::CustomerScoring::Client::DEFAULT_URL
|
20
|
+
test_url += "?age=35&income=50000&zipcode=60621"
|
21
|
+
stub_request(:get, test_url).
|
22
|
+
to_return(status: 200, body: '{"propensity": 0.26532,"ranking": "C"}')
|
23
|
+
client = Leapfrog::CustomerScoring::Client.new
|
24
|
+
@result = client.get_score("50000", "60621", "35")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return a Hash" do
|
28
|
+
expect(@result).to be_kind_of(Hash)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should only include 'propensity' and 'ranking" do
|
32
|
+
expect(@result.keys.sort).to eql([:propensity, :ranking])
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have the correct value for 'propensity'" do
|
36
|
+
expect(@result[:propensity]).to eql(0.26532)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have the correct value for 'ranking'" do
|
40
|
+
expect(@result[:ranking]).to eql("C")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when invalid url" do
|
45
|
+
before(:all) do
|
46
|
+
test_url = Leapfrog::CustomerScoring::Client::DEFAULT_URL
|
47
|
+
test_url += "?age=35&income=50000&zipcode=60621"
|
48
|
+
stub_request(:get, test_url).
|
49
|
+
to_return(status: [404, "Invalid URL"])
|
50
|
+
end
|
51
|
+
let(:client) { Leapfrog::CustomerScoring::Client.new }
|
52
|
+
|
53
|
+
it "should raise an InvalidUrl exception" do
|
54
|
+
expect { client.get_score("50000", "60621", "35") }.
|
55
|
+
to raise_error(Leapfrog::CustomerScoring::ResourceNotFound)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when invalid input" do
|
60
|
+
before(:all) do
|
61
|
+
test_url = Leapfrog::CustomerScoring::Client::DEFAULT_URL
|
62
|
+
test_url += "?age=35&income=50000&zipcode=no"
|
63
|
+
stub_request(:get, test_url).
|
64
|
+
to_return(status: [422, "Invalid Input"])
|
65
|
+
end
|
66
|
+
let(:client) { Leapfrog::CustomerScoring::Client.new }
|
67
|
+
|
68
|
+
it "should raise an InvalidInput exception" do
|
69
|
+
expect { client.get_score("50000", "no", "35") }.
|
70
|
+
to raise_error(Leapfrog::CustomerScoring::InvalidInput)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when server error" do
|
75
|
+
before(:all) do
|
76
|
+
test_url = Leapfrog::CustomerScoring::Client::DEFAULT_URL
|
77
|
+
test_url += "?age=35&income=50000&zipcode=60621"
|
78
|
+
stub_request(:get, test_url).
|
79
|
+
to_return(status: [500, "Internal Server Error"])
|
80
|
+
end
|
81
|
+
let(:client) { Leapfrog::CustomerScoring::Client.new }
|
82
|
+
|
83
|
+
it "should raise a ServerError exception" do
|
84
|
+
expect { client.get_score("50000", "60621", "35") }.
|
85
|
+
to raise_error(Leapfrog::CustomerScoring::ServerError)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when timed out" do
|
90
|
+
before(:all) do
|
91
|
+
test_url = Leapfrog::CustomerScoring::Client::DEFAULT_URL
|
92
|
+
test_url += "?age=35&income=50000&zipcode=60621"
|
93
|
+
stub_request(:get, test_url).
|
94
|
+
to_timeout
|
95
|
+
end
|
96
|
+
let(:client) { Leapfrog::CustomerScoring::Client.new }
|
97
|
+
|
98
|
+
it "should raise a Timeout exception" do
|
99
|
+
expect { client.get_score("50000", "60621", "35") }.
|
100
|
+
to raise_error(Leapfrog::CustomerScoring::ServerTimeout)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leapfrog-customer_scoring
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paul Sorensen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: webmock
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.14.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.14.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rest-client
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.6.7
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.6.7
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: json
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.8.0
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.8.0
|
110
|
+
description: Ruby API client for Leapfrog Online customer scoring
|
111
|
+
email:
|
112
|
+
- paulnsorensen@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- .rspec
|
119
|
+
- .travis.yml
|
120
|
+
- CHANGELOG.md
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- leapfrog-customer_scoring.gemspec
|
126
|
+
- lib/leapfrog/customer_scoring.rb
|
127
|
+
- lib/leapfrog/customer_scoring/client.rb
|
128
|
+
- lib/leapfrog/customer_scoring/exceptions.rb
|
129
|
+
- lib/leapfrog/customer_scoring/version.rb
|
130
|
+
- spec/leapfrog/customer_scoring/client_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
homepage: ''
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
none: false
|
141
|
+
requirements:
|
142
|
+
- - ! '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
segments:
|
146
|
+
- 0
|
147
|
+
hash: 3260785113119909660
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
none: false
|
150
|
+
requirements:
|
151
|
+
- - ! '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
segments:
|
155
|
+
- 0
|
156
|
+
hash: 3260785113119909660
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 1.8.25
|
160
|
+
signing_key:
|
161
|
+
specification_version: 3
|
162
|
+
summary: Ruby API client for Leapfrog Online customer scoring
|
163
|
+
test_files:
|
164
|
+
- spec/leapfrog/customer_scoring/client_spec.rb
|
165
|
+
- spec/spec_helper.rb
|