samwise 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8af5c83add66f563dd036246aae10b326d2dda50
4
- data.tar.gz: 8ff5bfb1a9ca8a6c56ea2cd027d0fe51123de03d
3
+ metadata.gz: 51829ef71b62ddf9e69b989b2c82a79a58cd3b44
4
+ data.tar.gz: 55b99795f3f3557c936afbad12aa0f6b8c45f196
5
5
  SHA512:
6
- metadata.gz: 17c7b87e130dfeb8125d957090314176aa2e6517ed0afb102b1386bee70d1e64558eb428c5aa922c569289a646c3471bd0e2f950fbedd77a51e686421450b878
7
- data.tar.gz: 4a85c96cfcc7cb0a67fc6cababdb26eda6f31913d91e8e372309ecda68537e1ac7565d2da79e528d9639f0c48ad4e50858f898a29e161d90b978f15a756e7f1a
6
+ metadata.gz: ded464ceac3d0780727bcebb709f5ede19fa79dd7c4aa399b50a233e51f4bed3ed0127026b785bfa7ed9148d8191f10e67de593789483de96beb1b472f4d62ef
7
+ data.tar.gz: 97951c2cd954eef771e5435111fc0717429b57dbbbb22d9ca9ba5b411d9f2d5e5282be961ece1e370e2a6d32d2811dc037b8485d96f533f3342ab237e59302ed
data/README.md CHANGED
@@ -29,10 +29,10 @@ client = Samwise::Client.new(api_key: 'my key ...')
29
29
  client = Samwise::Client.new
30
30
  ```
31
31
 
32
- ### Verify DUNS number
32
+ ### Verify DUNS number is in SAM.gov
33
33
 
34
34
  ```ruby
35
- client.duns_is_valid?(duns: '080037478')
35
+ client.duns_is_in_sam?(duns: '080037478')
36
36
  #=> true
37
37
  ```
38
38
 
@@ -68,7 +68,40 @@ client.get_duns_info(duns: '080037478')
68
68
  }
69
69
  ```
70
70
 
71
- `duns` can be an 8, 9, or 13 digit number:
71
+ ### Validate the format of a DUNS number
72
+
73
+ This does not need an API key and makes no network calls.
74
+
75
+ ```ruby
76
+ Samwise::Util.duns_is_properly_formatted?(duns: '88371771')
77
+ #=> true
78
+
79
+ Samwise::Util.duns_is_properly_formatted?(duns: '883717717')
80
+ #=> true
81
+
82
+ Samwise::Util.duns_is_properly_formatted?(duns: '0223841150000')
83
+ #=> true
84
+
85
+ Samwise::Util.duns_is_properly_formatted?(duns: '08-011-5718')
86
+ #=> true
87
+
88
+ Samwise::Util.duns_is_properly_formatted?(duns: 'abc1234567')
89
+ #=> false
90
+
91
+ Samwise::Util.duns_is_properly_formatted?(duns: '1234567891234567890')
92
+ #=> false
93
+ ```
94
+
95
+ ### Format a DUNS number
96
+
97
+ This removes any hyphens and appends `0`s where appropriate (does not need an API key and makes no network calls):
98
+
99
+ ```ruby
100
+ Samwise::Util.format_duns(duns: '08-011-5718')
101
+ #=> "0801157180000"
102
+ ```
103
+
104
+ `duns` can be an 8, 9, or 13 digit number (hyphens are removed):
72
105
 
73
106
  - If it is 8 digits, `0` is prepended, and `0000` is added to the end.
74
107
  - If it is 9 digits, `0000` is added to the end.
@@ -3,7 +3,7 @@ require 'json'
3
3
 
4
4
  module Samwise
5
5
  class Client
6
- def initialize(api_key: api_key)
6
+ def initialize(api_key: nil)
7
7
  @api_key = api_key || ENV['DATA_DOT_GOV_API_KEY']
8
8
  end
9
9
 
@@ -12,14 +12,14 @@ module Samwise
12
12
  JSON.parse(response.body)
13
13
  end
14
14
 
15
- def duns_is_valid?(duns: nil)
15
+ def duns_is_in_sam?(duns: nil)
16
16
  response = lookup_duns(duns: duns)
17
17
  response.status == 200
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def lookup_duns(duns: duns)
22
+ def lookup_duns(duns: nil)
23
23
  duns = Samwise::Util.format_duns(duns: duns)
24
24
  response = Faraday.get(Samwise::Protocol.duns_url(duns: duns, api_key: @api_key))
25
25
  end
@@ -0,0 +1,9 @@
1
+ module Samwise
2
+ module Error
3
+ class InvalidFormat < StandardError
4
+ def initialize(message: 'The format of the provided DUNS number is invalid')
5
+ super(message)
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/samwise/util.rb CHANGED
@@ -2,14 +2,58 @@ require 'pry'
2
2
 
3
3
  module Samwise
4
4
  module Util
5
- def self.format_duns(duns: duns)
6
- if duns.length == 9
5
+ def self.duns_is_properly_formatted?(duns: nil)
6
+ duns.gsub!('-', '')
7
+
8
+ return true if duns_contains_forbidden_characters?(duns: duns)
9
+
10
+ return true if duns_is_eight_digits?(duns: duns)
11
+ return true if duns_is_nine_digits?(duns: duns)
12
+ return true if duns_is_thirteen_digits?(duns: duns)
13
+
14
+ return false
15
+ end
16
+
17
+ def self.format_duns(duns: nil)
18
+ raise Samwise::Error::InvalidFormat unless duns_is_properly_formatted?(duns: duns)
19
+
20
+ duns.gsub!('-', '')
21
+
22
+ if duns_is_nine_digits?(duns: duns)
7
23
  duns = "#{duns}0000"
8
- elsif duns.length == 8
24
+ elsif duns_is_eight_digits?(duns: duns)
9
25
  duns = "0#{duns}0000"
10
26
  end
11
27
 
12
28
  duns
13
29
  end
30
+
31
+ def self.duns_contains_forbidden_characters?(duns: nil)
32
+ duns.split('').each do |character|
33
+ return false if self.string_is_numeric?(string: character)
34
+ end
35
+
36
+ false
37
+ end
38
+
39
+ def self.string_is_numeric?(string: nil)
40
+ string.split('').each do |character|
41
+ return false unless character.to_i.to_s == character
42
+ end
43
+
44
+ return true
45
+ end
46
+
47
+ def self.duns_is_eight_digits?(duns: nil)
48
+ duns.length == 8
49
+ end
50
+
51
+ def self.duns_is_nine_digits?(duns: nil)
52
+ duns.length == 9
53
+ end
54
+
55
+ def self.duns_is_thirteen_digits?(duns: nil)
56
+ duns.length == 13
57
+ end
14
58
  end
15
59
  end
@@ -1,4 +1,3 @@
1
1
  module Samwise
2
- # samwise version
3
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
4
3
  end
data/lib/samwise.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'samwise/version'
2
+ require 'samwise/error'
2
3
  require 'samwise/util'
3
4
  require 'samwise/protocol'
4
5
  require 'samwise/client'
data/spec/client_spec.rb CHANGED
@@ -8,31 +8,31 @@ describe Samwise::Client, vcr: { cassette_name: "Samwise::Client", record: :new_
8
8
  let(:thirteen_duns) { '0223841150000' }
9
9
  let(:non_existent_duns) { '0000001000000' }
10
10
 
11
- context '#duns_is_valid?' do
12
- it "should verify that a 9 digit DUNS number exists" do
11
+ context '#duns_is_in_sam?' do
12
+ it "should verify that a 9 digit DUNS number exists in sam.gov" do
13
13
  client = Samwise::Client.new(api_key: api_key)
14
- response = client.duns_is_valid?(duns: nine_duns)
14
+ response = client.duns_is_in_sam?(duns: nine_duns)
15
15
 
16
16
  expect(response).to be(true)
17
17
  end
18
18
 
19
- it "should verify that an 8 digit DUNS number exists" do
19
+ it "should verify that an 8 digit DUNS number exists in sam.gov" do
20
20
  client = Samwise::Client.new(api_key: api_key)
21
- response = client.duns_is_valid?(duns: eight_duns)
21
+ response = client.duns_is_in_sam?(duns: eight_duns)
22
22
 
23
23
  expect(response).to be(true)
24
24
  end
25
25
 
26
- it "should verify that a 13 digit DUNS number exists" do
26
+ it "should verify that a 13 digit DUNS number exists in sam.gov" do
27
27
  client = Samwise::Client.new(api_key: api_key)
28
- response = client.duns_is_valid?(duns: thirteen_duns)
28
+ response = client.duns_is_in_sam?(duns: thirteen_duns)
29
29
 
30
30
  expect(response).to be(true)
31
31
  end
32
32
 
33
- it "should return false given a non-existent DUNS number" do
33
+ it "should return false given a DUNS number that is not in sam.gov" do
34
34
  client = Samwise::Client.new(api_key: api_key)
35
- response = client.duns_is_valid?(duns: non_existent_duns)
35
+ response = client.duns_is_in_sam?(duns: non_existent_duns)
36
36
 
37
37
  expect(response).to be(false)
38
38
  end
@@ -60,7 +60,7 @@ describe Samwise::Client, vcr: { cassette_name: "Samwise::Client", record: :new_
60
60
  expect(response).to be_a(Hash)
61
61
  end
62
62
 
63
- it "should return an error given a non-existent DUNS number" do
63
+ it "should return an error given a DUNS number that is not in sam.gov" do
64
64
  client = Samwise::Client.new(api_key: api_key)
65
65
  response = client.get_duns_info(duns: non_existent_duns)
66
66
 
data/spec/util_spec.rb CHANGED
@@ -2,18 +2,82 @@ require 'spec_helper'
2
2
  require 'samwise'
3
3
 
4
4
  describe Samwise::Util do
5
- let(:eight_duns) { '88371771' }
6
- let(:nine_duns) { '883717717' }
5
+ let(:eight_duns) { '88371771' }
6
+ let(:nine_duns) { '883717717' }
7
+ let(:thirteen_duns) { '0223841150000' }
8
+ let(:hyphen_duns) { '08-011-5718' }
9
+ let(:letters_duns) { 'abc12345678'}
10
+ let(:bad_dunses) do
11
+ [
12
+ '1234567890',
13
+ '12345678901',
14
+ '123456789011',
15
+ '1',
16
+ '',
17
+ '--',
18
+ '12345678901234567890'
19
+ ]
20
+ end
21
+
22
+ context '#duns_is_properly_formatted?' do
23
+ it 'should return true when the DUNS is 13 digits (not counting hyphens)' do
24
+ is_formatted = Samwise::Util.duns_is_properly_formatted?(duns: thirteen_duns)
25
+
26
+ expect(is_formatted).to be(true)
27
+ end
28
+
29
+ it 'should return true when the DUNS is 9 digits (not counting hyphens)' do
30
+ is_formatted = Samwise::Util.duns_is_properly_formatted?(duns: nine_duns)
31
+
32
+ expect(is_formatted).to be(true)
33
+ end
34
+
35
+ it 'should return true when the DUNS is 8 digits (not counting hyphens)' do
36
+ is_formatted = Samwise::Util.duns_is_properly_formatted?(duns: eight_duns)
37
+
38
+ expect(is_formatted).to be(true)
39
+ end
7
40
 
8
- it "should format an 8 digit DUNS into a 13 digit DUNS" do
9
- formatted_duns = Samwise::Util.format_duns(duns: eight_duns)
41
+ it 'should return false when the DUNS is not 8, 9, or 13 digits (not counting hyphens)' do
42
+ bad_dunses.each do |bad_duns|
43
+ is_formatted = Samwise::Util.duns_is_properly_formatted?(duns: bad_duns)
10
44
 
11
- expect(formatted_duns.length).to eq(13)
45
+ expect(is_formatted).to be(false)
46
+ end
47
+ end
48
+
49
+ it 'should return false when the DUNS contains letters' do
50
+ is_formatted = Samwise::Util.duns_is_properly_formatted?(duns: letters_duns)
51
+
52
+ expect(is_formatted).to be(false)
53
+ end
12
54
  end
13
55
 
14
- it "should format a 9 digit DUNS into a 13 digit DUNS" do
15
- formatted_duns = Samwise::Util.format_duns(duns: nine_duns)
56
+ context '#format_duns' do
57
+ it 'should format an 8 digit DUNS into a 13 digit DUNS' do
58
+ formatted_duns = Samwise::Util.format_duns(duns: eight_duns)
59
+
60
+ expect(formatted_duns.length).to eq(13)
61
+ end
62
+
63
+ it 'should format a 9 digit DUNS into a 13 digit DUNS' do
64
+ formatted_duns = Samwise::Util.format_duns(duns: nine_duns)
65
+
66
+ expect(formatted_duns.length).to eq(13)
67
+ end
68
+
69
+ it 'should remove hyphens from a DUNS number' do
70
+ formatted_duns = Samwise::Util.format_duns(duns: hyphen_duns)
71
+
72
+ expect(formatted_duns).to eq('0801157180000')
73
+ end
16
74
 
17
- expect(formatted_duns.length).to eq(13)
75
+ it 'should raise a Samwise::Error::InvalidFormat error if the DUNS is invalid' do
76
+ bad_dunses.each do |bad_duns|
77
+ expect do
78
+ Samwise::Util.format_duns(duns: bad_duns)
79
+ end.to raise_error(Samwise::Error::InvalidFormat)
80
+ end
81
+ end
18
82
  end
19
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samwise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan deLevie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-04 00:00:00.000000000 Z
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codeclimate-test-reporter
@@ -155,6 +155,7 @@ files:
155
155
  - Rakefile
156
156
  - lib/samwise.rb
157
157
  - lib/samwise/client.rb
158
+ - lib/samwise/error.rb
158
159
  - lib/samwise/protocol.rb
159
160
  - lib/samwise/util.rb
160
161
  - lib/samwise/version.rb