ncua 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 398980c10ce254d16210333409e04c87bb466da2
4
- data.tar.gz: a1bd3146b069403d2e26bcbc06d21b7b980727a4
3
+ metadata.gz: f7e2e496175b1a98a96270345f3685e360cab0cb
4
+ data.tar.gz: 8b84ad589f325b8077cb74233db773eac5bdce08
5
5
  SHA512:
6
- metadata.gz: 7dc57569862938332daca4e6e6357aa69e42aeb4e6291c0735c33b38ee6fb3ff0ccffb35624c15a888769a17100d6c0c607cac8293dbfc10f78c9b41557f1510
7
- data.tar.gz: b824aece7492cccc508b30233a4003be96482d33bfbe41d6eebad135966c6d9cf71391e04f92ad1e1325771d896b6f7da5a9f92a63443e396861aea0523808b3
6
+ metadata.gz: ec03e928a657aaa4bcc8bf765b1ac1f10d135489e308e1a0e015ea74d5a376359021d063a3dd17e0e695adf974490d0afc9ff5fb5b6dda6075f2785c163af18c
7
+ data.tar.gz: 2b47405597a38b5599d451be70bcc4e5fa470ae19b5fe26d74203329675c3d71e7a402d24d7b24c8ba9c1d256de3bbb50bed77eea55319d99220480d180c9333
data/README.md CHANGED
@@ -116,6 +116,26 @@ If for some reason the NCUA returns a 500 error when directly scraping for credi
116
116
 
117
117
  In fact, we only really expect response codes in the 200 range. If the NCUA returns another response, the gem will raise `NCUA::CreditUnion::ServerError` with the response code in the description.
118
118
 
119
+ ## Validating this Gem's Schema Assumptions
120
+
121
+ This gem relies upon both the schema of an unpublished public JSON API and the particular markup of a page on the NCUA's website remaining constant.
122
+
123
+ Obviously, this is a bad practice. Until the NCUA publishes a versioned, public API of their data, we will continue to rely upon these unorthodox methods of data retrieval.
124
+
125
+ To help current users of this gem, we have provided an API for validating the assumed schema of the NCUA's data.
126
+
127
+ Calling `NCUA.schema_valid?` will return `true` if the gem is safe to use. Note that this makes an actual call to the NCUA's endpoints, so be mindful of its use.
128
+
129
+ Calling `NCUA.validate_schema!` will similarly return `true` if the gem is safe to use, but will raise a more detailed exception if the schema is invalid.
130
+
131
+ The gem provides an internal rake task, `rake ncua:validate_schema!`, which calls `NCUA.validate_schema!`. For the convenience of Rails users, we have used a Railtie to automatically provide this rake task in your application.
132
+
133
+ If you do not use Rails, you can add the following to your `Rakefile` to expose this task.
134
+ ```ruby
135
+ require 'ncua'
136
+ NCUA::NCUATasks.new.install_tasks
137
+ ```
138
+
119
139
  ## Contributing
120
140
 
121
141
  Bug reports and pull requests are welcome on GitHub at https://github.com/ContinuityControl/ncua.
data/Rakefile CHANGED
@@ -1,6 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require './lib/tasks/ncua_tasks'
4
+
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
8
  task :default => :spec
9
+ Bundler::GemHelper.install_tasks
10
+ NCUA::NCUATasks.new.install_tasks
@@ -2,13 +2,18 @@ require 'httparty'
2
2
  require 'nokogiri'
3
3
  require 'ncua/version'
4
4
  require 'ncua/client'
5
+ require 'ncua/client_validator'
5
6
  require 'ncua/credit_union/record'
6
7
  require 'ncua/credit_union/office'
7
8
  require 'ncua/credit_union/details'
8
9
  require 'ncua/credit_union/scraper'
10
+ require 'ncua/credit_union/scraper_validator'
9
11
  require 'ncua/credit_union/details_client'
12
+ require 'tasks/ncua_tasks'
10
13
 
11
14
  module NCUA
15
+ require "ncua/railtie" if defined?(Rails)
16
+
12
17
  def self.find_office_by_address(address, opts={radius: 100})
13
18
  resp = Client.new.find_credit_union_by_address(address, opts[:radius])
14
19
  resp["list"].map { |result| CreditUnion::Office.new(result) }
@@ -27,4 +32,20 @@ module NCUA
27
32
  def self.find_credit_union(charter_number)
28
33
  CreditUnion::Details.new(CreditUnion::Scraper.new(charter_number).scrape!)
29
34
  end
35
+
36
+ def self.validate_schema!
37
+ unless NCUA::CreditUnion::ScraperValidator.new.schema_valid?
38
+ raise "NCUA CreditUnion Scraper Schema is invalid. Please contact Gem Maintainer"
39
+ end
40
+
41
+ unless NCUA::ClientValidator.schema_valid?
42
+ raise "NCUA Client Schema is invalid. Please contact Gem Maintainer"
43
+ end
44
+
45
+ true
46
+ end
47
+
48
+ def self.schema_valid?
49
+ NCUA::CreditUnion::Scraper.new(42).schema_valid? && NCUA::ClientValidator.schema_valid?
50
+ end
30
51
  end
@@ -6,7 +6,7 @@ module NCUA
6
6
  format :json
7
7
  #debug_output $stderr
8
8
 
9
- def find_credit_union_by_address(address, radius)
9
+ def find_credit_union_by_address(address, radius = 100)
10
10
  self.class.get(query_endpoint, query: {
11
11
  address: address,
12
12
  type: 'address',
@@ -30,6 +30,5 @@ module NCUA
30
30
  def query_endpoint
31
31
  '/findCUByRadius.aspx'
32
32
  end
33
-
34
33
  end
35
34
  end
@@ -0,0 +1,30 @@
1
+ module NCUA
2
+ class ClientValidator
3
+ def self.schema_valid?
4
+ base_fields_valid? && list_fields_valid?
5
+ end
6
+
7
+ private
8
+
9
+ def self.base_fields_valid?
10
+ methods = [:find_credit_union_by_name, :find_credit_union_by_charter_number, :find_credit_union_by_address]
11
+ expected_keys = ['list', 'latitude', 'longitude'].sort
12
+
13
+ methods.all? { |method_name|
14
+ Client.new.send(method_name, "Something Fake").keys.sort == expected_keys
15
+ }
16
+ end
17
+
18
+ def self.list_fields_valid?
19
+ expected_keys = ['CU_NAME', 'AddressLongitude', 'AddressLatitude',
20
+ 'CU_SITENAME', 'CU_NUMBER', 'City', 'CityPhysical',
21
+ 'Country', 'index', 'IsMainOffice', 'Phone',
22
+ 'SiteFunctions', 'SiteId', 'SiteName', 'State', 'URL',
23
+ 'Zipcode', 'distance', 'Street'].sort
24
+ found_keys = Client.new.find_credit_union_by_charter_number(42)["list"].first.keys.sort
25
+ # return expected_fields is a subset of found_keys
26
+ found_keys == expected_keys
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ module NCUA
2
+ module CreditUnion
3
+ class ScraperValidator
4
+ def schema_valid?
5
+ has_a_table? &&
6
+ has_correctly_formatted_key_value_rows? &&
7
+ has_correct_keys?
8
+ end
9
+
10
+ private
11
+
12
+ def has_a_table?
13
+ !html_doc.at_css('table#MainContent_newDetails').nil?
14
+ end
15
+
16
+ def has_correctly_formatted_key_value_rows?
17
+ key_value_rows.all? { |tr|
18
+ key_value_row?(tr.css("td")) || sub_header_row?(tr.css("td"))
19
+ }
20
+ end
21
+
22
+ def has_correct_keys?
23
+ found_keys = key_value_rows.map { |tr| tr.at_css('td.dvHeader') }.compact.map(&:text)
24
+ expected_keys = ["Credit Union Name:", "Charter Number:", "Credit Union Type:",
25
+ "Credit Union Status:", "Corporate Credit Union:",
26
+ "Credit Union Charter Year:", "Current Charter Issue Date:",
27
+ "Date Insured:", "Charter State:", "Region:",
28
+ "Field of Membership Type:", "Low Income Designation:",
29
+ "Member of FHLB:", "Assets:", "Peer Group:", "Number of Members:",
30
+ "Address:", "City, State Zip code:", "Country:", "County:", "Phone:",
31
+ "Website:", "CEO/Manager:"]
32
+ found_keys == expected_keys
33
+ end
34
+
35
+ def key_value_row?(tr_cells)
36
+ tr_cells.count == 2 && tr_cells.first[:class] == 'dvHeader'
37
+ end
38
+
39
+ def sub_header_row?(tr_cells)
40
+ tr_cells.count == 1 && tr_cells.first[:class] == 'subheader'
41
+ end
42
+
43
+ def request
44
+ @request ||= DetailsClient.new.get_details(42)
45
+ end
46
+
47
+ def html_doc
48
+ @html_doc ||= Nokogiri::HTML(request.body)
49
+ end
50
+
51
+ def key_value_rows
52
+ @key_value_rows ||= html_doc.at_css("table#MainContent_newDetails").css("tr")
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ require 'ncua'
2
+ require 'rails'
3
+ module NCUA
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ NCUA::NCUATasks.new.install_tasks
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module NCUA
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
@@ -0,0 +1,13 @@
1
+ module NCUA
2
+ class NCUATasks
3
+ include Rake::DSL if defined? Rake::DSL
4
+ def install_tasks
5
+ namespace :ncua do
6
+ desc "Verify Schema"
7
+ task(:validate_schema!) do
8
+ ::NCUA.validate_schema!
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncua
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Reznick
@@ -118,12 +118,16 @@ files:
118
118
  - bin/setup
119
119
  - lib/ncua.rb
120
120
  - lib/ncua/client.rb
121
+ - lib/ncua/client_validator.rb
121
122
  - lib/ncua/credit_union/details.rb
122
123
  - lib/ncua/credit_union/details_client.rb
123
124
  - lib/ncua/credit_union/office.rb
124
125
  - lib/ncua/credit_union/record.rb
125
126
  - lib/ncua/credit_union/scraper.rb
127
+ - lib/ncua/credit_union/scraper_validator.rb
128
+ - lib/ncua/railtie.rb
126
129
  - lib/ncua/version.rb
130
+ - lib/tasks/ncua_tasks.rb
127
131
  - ncua.gemspec
128
132
  homepage: https://github.com/ContinuityControl/ncua
129
133
  licenses: