Skip to content
This repository
Browse code

Added CUSIP goodie

By request:
https://duckduckhack.uservoice.com/forums/5168-plugins/suggestions/2823880-cusip-check-digit-calculator

Note: Due to the proprietary nature of CUSIP identifiers,
corresponding sercurities cannot be freely looked up, only
the CUSIP validated. See
https://en.wikipedia.org/wiki/CUSIP#Antitrust_Review
  • Loading branch information...
commit 43e9183eebb337ac9321c5a263ee59fd36b965a5 1 parent 525d661
Dylan Lloyd authored July 12, 2012
1  dist.ini
@@ -8,6 +8,7 @@ copyright_holder = DuckDuckGo, Inc. L<http://duckduckgo.com/>
8 8
 copyright_year   = 2011
9 9
 
10 10
 [Prereqs]
  11
+Business::CUSIP = 1.03
11 12
 MIME::Base64 = 3.13
12 13
 Roman = 1.23
13 14
 Math::Int2Base = 1.00
31  lib/DDG/Goodie/Cusip.pm
... ...
@@ -0,0 +1,31 @@
  1
+package DDG::Goodie::Cusip;
  2
+
  3
+use DDG::Goodie;
  4
+use Business::CUSIP;
  5
+
  6
+triggers any => 'cusip';
  7
+
  8
+zci is_cached => 1;
  9
+zci answer_type => "cusip";
  10
+
  11
+handle query => sub { 
  12
+    return unless /(?:cusip\ (?:check\ (?:digit\ )?)?)?
  13
+                   (\d{3}[0-9A-Z]{3}\d{2,3})
  14
+                   (?:(?:cusip\ ?)?(?:check\ (:?digit)))?/ix;
  15
+    my $cusip_number = uc $1;
  16
+    my $cusip = Business::CUSIP->new($cusip_number);
  17
+    if ($cusip->is_valid) {
  18
+        return "Valid CUSIP: $1";
  19
+    } else {
  20
+        my $chk = $cusip->check_digit;
  21
+        if ($cusip->cusip($cusip->cusip . $chk)) {
  22
+            if ($cusip->is_valid) {
  23
+                return "CUSIP check digit: $chk, valid CUSIP: $1$chk";
  24
+            }
  25
+        }
  26
+        return "CUSIP check digit: $chk, invalid CUSIP: $1$chk";
  27
+    }
  28
+    return "Invalid CUSIP: $1";
  29
+};
  30
+
  31
+1;
22  t/Cusip.t
... ...
@@ -0,0 +1,22 @@
  1
+#!/usr/bin/env perl
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use Test::More;
  6
+use DDG::Test::Goodie;
  7
+
  8
+zci answer_type => 'cusip';
  9
+zci is_cached => 1;
  10
+
  11
+ddg_goodie_test(
  12
+	[qw(
  13
+		DDG::Goodie::Cusip
  14
+	)],
  15
+	'cusip 345370860' => test_zci('Valid CUSIP: 345370860'),
  16
+	'cusip check digit 34537086' => test_zci('CUSIP check digit: 0, valid CUSIP: 345370860'),
  17
+
  18
+	'037833100 cusip' => test_zci('Valid CUSIP: 037833100'),
  19
+	'03783310 cusip check digit' => test_zci('CUSIP check digit: 0, valid CUSIP: 037833100'),
  20
+);
  21
+
  22
+done_testing;

0 notes on commit 43e9183

Please sign in to comment.
Something went wrong with that request. Please try again.