Class: Turbovax::Portal

Inherits:
Object
  • Object
show all
Defined in:
lib/turbovax/portal.rb

Overview

Captures configuration required to fetch and process data from a specific vaccine website

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.data_fetcher_paramsHash

Extra params that are set by [Turbovax::DataFetcher]

Examples:

{ date: DateTime.now }

Returns:

  • (Hash)

74
75
76
# File 'lib/turbovax/portal.rb', line 74

define_parameter :data_fetcher_params, Hash,
"Extra params that are set by [Turbovax::DataFetcher]",
"{ date: DateTime.now }"

Class Method Details

.api_base_urlObject

Returns base API URL (used when creating Faraday connection)


150
151
152
# File 'lib/turbovax/portal.rb', line 150

def api_base_url
  "#{api_uri_object.scheme}://#{api_uri_object.hostname}"
end

.api_pathObject

Returns API URL path (used when making Faraday requests)


155
156
157
# File 'lib/turbovax/portal.rb', line 155

def api_path
  "#{api_uri_object.path}?#{api_uri_object.query}"
end

.api_query_params(*args, &block) ⇒ Hash

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

Examples:

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

Parameters:

  • args (Array)

    passed from [Turbovax::DataFetcher]

  • block (Block)

    stored as a class instance variable

Returns:

  • (Hash)

129
130
131
132
133
134
135
136
137
# 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

.api_urlString

Full API URL

Examples:

Example Turbovax endpoint

'https://api.turbovax.info/v1/dashboard'

Returns:

  • (String)

56
57
# File 'lib/turbovax/portal.rb', line 56

define_parameter :api_url, String, "Full API URL", "Example Turbovax endpoint",
"'https://api.turbovax.info/v1/dashboard'"

.api_url_variablesHash

Hash or block that is interpolated

Examples:

api_url_variables do |extra_params|
  {
    site_id: NAME_TO_ID_MAPPING[extra_params[:name]],
    date: extra_params.strftime("%F"),
  }
end

#  before api_url_variables interpolation
api_url "https://api.turbovax.info/v1/sites/%{site_id}/%{date}"

#  after api_url_variables interpolation
api_url "https://api.turbovax.info/v1/sites/8888/2021-08-08"

Returns:

  • (Hash)

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/turbovax/portal.rb', line 58

define_parameter :api_url_variables, Hash,
               "Hash or block that is interpolated ",
               '
  api_url_variables do |extra_params|
    {
      site_id: NAME_TO_ID_MAPPING[extra_params[:name]],
      date: extra_params.strftime("%F"),
    }
  end

  #  before api_url_variables interpolation
  api_url "https://api.turbovax.info/v1/sites/%{site_id}/%{date}"

  #  after api_url_variables interpolation
  api_url "https://api.turbovax.info/v1/sites/8888/2021-08-08"
'

.define_parameter(attribute, _doc_return_type, _explanation = nil, _explanation = nil, _example = nil) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

.keyString

Unique identifier for portal

Examples:

Key

'nyc_vax'

Returns:

  • (String)

44
# File 'lib/turbovax/portal.rb', line 44

define_parameter :key, String, "Unique identifier for portal", "Key", "'nyc_vax'"

.nameString

Full name of portal

Examples:

Name

'New York City Vaccine Website'

Returns:

  • (String)

42
43
# File 'lib/turbovax/portal.rb', line 42

define_parameter :name, String, "Full name of portal", "Name",
"'New York City Vaccine Website'"

.notesString

Extra info for users

Returns:

  • (String)

77
78
# File 'lib/turbovax/portal.rb', line 77

define_parameter :notes, String,
"Extra info for users"

.parse_response(*args, &block) ⇒ Array<Turbovax::Location>

Block that will called after raw data is fetched from API. Must return list of Location instances

Examples:

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

Parameters:

  • args (Array)

    passed from [Turbovax::DataFetcher]

  • block (Block)

    stored as a class instance variable

Returns:


104
105
106
107
108
109
110
111
112
# 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

.parse_response_with_portal(response) ⇒ Object

Calls parse_response and assigns portal to each location so user doesn't need to do this by themselves


161
162
163
164
165
166
# 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

.public_urlString

Link to public facing website

Examples:

Full URL

'https://www.turbovax.info/'

Returns:

  • (String)

45
46
47
48
# File 'lib/turbovax/portal.rb', line 45

define_parameter :public_url,
String,
"Link to public facing website", "Full URL",
"'https://www.turbovax.info/'"

.request_body(*args, &block) ⇒ Object


139
140
141
142
143
144
145
146
147
# 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

.request_headersHash

Key:value mapping of HTTP request headers

Examples:

Specify user agent and cookies

{ 'user-agent': 'Mozilla/5.0', 

Returns:

  • (Hash)

50
51
52
# File 'lib/turbovax/portal.rb', line 50

define_parameter :request_headers, Hash, "Key:value mapping of HTTP request headers",
                       "Specify user agent and cookies", "{ 'user-agent': 'Mozilla/5.0', " \
"'cookies': 'ABC' }"

.request_http_methodSymbol

Turbovax::Constants::GET_REQUEST_METHOD or

Returns:

  • (Symbol)

53
54
55
# File 'lib/turbovax/portal.rb', line 53

define_parameter :request_http_method, Symbol,
"Turbovax::Constants::GET_REQUEST_METHOD or " \
"Turbovax::Constants::POST_REQUEST_METHOD"