class Turbovax::DataFetcher

This class, given a portal and a twitter handler:

1) executes request for data
2) passes structured appointment data to twitter handler
3) returns appointment data

Public Class Methods

new(portal, twitter_handler: nil, date: nil, extra_params: {}) click to toggle source

@param [Turbovax::Portal] portal @param [TurboVax::Twitter::Handler] twitter_handler a class handles if appointments are found @param [DateTime] date sets request to specific date when specified @param [Hash] extra_params other info that can be provided to portal when executing blocks

# File lib/turbovax/data_fetcher.rb, line 16
def initialize(portal, twitter_handler: nil, date: nil, extra_params: {})
  portal.data_fetcher_params = { date: date || DateTime.now }.merge(extra_params)
  @portal = portal
  @conn = create_request_connection
  @twitter_handler = twitter_handler
end

Public Instance Methods

execute!() click to toggle source

@return [Array<Turbovax::Location>] List of locations and appointments

# File lib/turbovax/data_fetcher.rb, line 24
def execute!
  response = make_request
  log("make request [DONE]")
  locations = @portal.parse_response_with_portal(response.body)
  log("parse response [DONE]")

  send_to_twitter_handler(locations)

  locations
end

Private Instance Methods

create_request_connection() click to toggle source
# File lib/turbovax/data_fetcher.rb, line 48
def create_request_connection
  Faraday.new(
    url: @portal.api_base_url,
    headers: @portal.request_headers,
    ssl: { verify: false }
  ) do |faraday|
    faraday.response :logger, Turbovax.logger,
                     Turbovax.faraday_logging_config
    faraday.adapter Faraday.default_adapter
  end
end
log(message) click to toggle source
# File lib/turbovax/data_fetcher.rb, line 92
def log(message)
  Turbovax.logger.info("[#{self.class}] #{message}")
end
make_get_request(path, query_params) click to toggle source
# File lib/turbovax/data_fetcher.rb, line 75
def make_get_request(path, query_params)
  @conn.get(path) do |req|
    # only set params if they are present, otherwise this will overwrite any string query
    # param values that are existing in the url path
    req.params = query_params if query_params.nil? || query_params != {}
  end
end
make_post_request(path, query_params) click to toggle source
# File lib/turbovax/data_fetcher.rb, line 83
def make_post_request(path, query_params)
  @conn.post(path) do |req|
    # only set params if they are present, otherwise this will overwrite any string query
    # param values that are existing in the url path
    req.params = query_params if query_params.nil? || query_params != {}
    req.body = @portal.request_body
  end
end
make_request() click to toggle source
# File lib/turbovax/data_fetcher.rb, line 60
def make_request
  request_type = @portal.request_http_method
  path = @portal.api_path
  query_params = @portal.api_query_params

  case request_type
  when Turbovax::Constants::GET_REQUEST_METHOD
    make_get_request(path, query_params)
  when Turbovax::Constants::POST_REQUEST_METHOD
    make_post_request(path, query_params)
  else
    raise Turbovax::InvalidRequestTypeError
  end
end
send_to_twitter_handler(locations) click to toggle source
# File lib/turbovax/data_fetcher.rb, line 37
def send_to_twitter_handler(locations)
  if !Turbovax.twitter_enabled
    log("twitter handler [SKIP] not enabled")
  elsif !locations.size.positive?
    log("twitter handler [SKIP]: no location data")
  else
    @twitter_handler&.new(locations)&.execute!
    log("twitter handler [DONE]")
  end
end