class Turbovax::Portal
Captures configuration required to fetch and process data from a specific vaccine website
Attributes
Public Class Methods
Returns base API URL (used when creating Faraday connection)
# File lib/turbovax/portal.rb, line 150 def api_base_url "#{api_uri_object.scheme}://#{api_uri_object.hostname}" end
Returns API URL path (used when making Faraday requests)
# File lib/turbovax/portal.rb, line 155 def api_path "#{api_uri_object.path}?#{api_uri_object.query}" end
Block that will be executed and then appended to API url path. The extra_params variable is provided by Turbovax::DataFetcher
. When specified, this will overwrite any query string that is already present in api_url
@param [Array] args passed from [Turbovax::DataFetcher] @param [Block] block stored as a class instance variable @return [Hash] @example Append date and noCache to URL
# result: /path?date=2021-08-08&noCache=0.123 api_query_params do |extra_params| { date: extra_params[:date].strftime("%F"), noCache: rand, } end
# File lib/turbovax/portal.rb, line 129 def api_query_params(*args, &block) if args.size.positive? && !@api_query_params.nil? @api_query_params.call(*args) elsif !block.nil? @api_query_params = block else {} end end
@!macro [attach] define_parameter
@method $1 $3 @return [$2] @example $4 $5
# File lib/turbovax/portal.rb, line 15 def self.define_parameter( attribute, _doc_return_type, _explanation = nil, _explanation = nil, _example = nil ) # metaprogramming magic so that # 1) attributes can be defined via DSL # 2) attributes can be fetched when method is called without any parameters # 3) attributes can saved static variables or blocks that can be called (for dynamic) # might be better to refactor in the future define_method attribute do |argument = nil, &block| variable = nil block_exists = begin variable = instance_variable_get("@#{attribute}") variable.is_a?(Proc) rescue StandardError false end if !variable.nil? block_exists ? variable.call(argument) : variable else instance_variable_set("@#{attribute}", argument || block) end end end
Block that will called after raw data is fetched from API. Must return list of Location
instances @param [Array] args passed from [Turbovax::DataFetcher] @param [Block] block stored as a class instance variable @return [Array<Turbovax::Location>] @example Parse API responses from turbovax.info
parse_response do |response| response_json = JSON.parse(response) response_json["locations"].map do |location| Turbovax::Location.new( name: location["name"], time_zone: "America/New_York", data: { is_available: location["is_available"], appointment_count: location["appointments"]["count"], appointments: [{ time: "2021-04-19T17:21:15-04:00", }] } ) end end
# File lib/turbovax/portal.rb, line 104 def parse_response(*args, &block) if args.size.positive? && !@parse_response.nil? @parse_response.call(*args) elsif !block.nil? @parse_response = block else {} end end
Calls parse_response
and assigns portal to each location so user doesn't need to do this by themselves
# File lib/turbovax/portal.rb, line 161 def parse_response_with_portal(response) parse_response(response).map do |location| location.portal ||= self location end end
# File lib/turbovax/portal.rb, line 139 def request_body(*args, &block) if args.size.positive? && !@request_body.nil? @request_body.call(*args) elsif !block.nil? @request_body = block else {}.to_json end end
Private Class Methods
# File lib/turbovax/portal.rb, line 170 def api_uri_object @api_uri_object ||= URI(api_url % api_url_variables) end