Skip to content

Commit

Permalink
Add proof-of-concept for appoint wait times fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
johncarney committed Jul 18, 2019
1 parent a7bb709 commit f0dad1a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ gem "jbuilder", "~> 2.5"
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", ">= 1.1.0", require: false

gem "nokogiri"

group :development, :test do
# Call "byebug" anywhere in the code to stop execution and get a debugger console
gem "byebug", platforms: [:mri, :mingw, :x64_mingw]
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ DEPENDENCIES
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
nokogiri
pg (>= 0.18, < 2.0)
pry-byebug
puma (~> 3.11)
Expand Down
11 changes: 11 additions & 0 deletions app/services/concerns/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Concerns
module Service
extend ActiveSupport::Concern

class_methods do
def call(*args, &block)
new(*args).call(&block)
end
end
end
end
29 changes: 29 additions & 0 deletions app/services/fetch_wait_times.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "openssl"
require "open-uri"

FetchWaitTimes = Struct.new(:consulate) do
include Concerns::Service

URI_HOST = "travel.state.gov"
URI_PATH = "/content/travel/resources/database/database.getVisaWaitTimes.html"

CATEGORIES = %i[ visitor student_exchange_visitor other_non_immigrant ]

delegate :code, to: :consulate, prefix: true

def uri
URI::HTTPS.build(host: URI_HOST, path: URI_PATH, query: URI.encode_www_form(cid: consulate_code))
end

def doc
@doc ||= Nokogiri::HTML(open(uri, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE))
end

def wait_times
%i[ visitor student_exchange_visitor other_non_immigrant ].zip(doc.text.split(/\s*,\s*/).take(3).map(&:to_i)).to_h
end

def call
CATEGORIES.zip(doc.text.split(/\s*,\s*/).take(CATEGORIES.size).map(&:to_i)).to_h
end
end

0 comments on commit f0dad1a

Please sign in to comment.