Class: Turbovax::Portal
- Inherits:
-
Object
- Object
- Turbovax::Portal
- Defined in:
- lib/turbovax/portal.rb
Overview
Captures configuration required to fetch and process data from a specific vaccine website
Class Attribute Summary collapse
-
.data_fetcher_params ⇒ Hash
Extra params that are set by [Turbovax::DataFetcher].
Class Method Summary collapse
-
.api_base_url ⇒ Object
Returns base API URL (used when creating Faraday connection).
-
.api_path ⇒ Object
Returns API URL path (used when making Faraday requests).
-
.api_query_params(*args, &block) ⇒ Hash
Block that will be executed and then appended to API url path.
-
.api_url ⇒ String
Full API URL.
-
.api_url_variables ⇒ Hash
Hash or block that is interpolated.
- .define_parameter(attribute, _doc_return_type, _explanation = nil, _explanation = nil, _example = nil) ⇒ Object
-
.key ⇒ String
Unique identifier for portal.
-
.name ⇒ String
Full name of portal.
-
.notes ⇒ String
Extra info for users.
-
.parse_response(*args, &block) ⇒ Array<Turbovax::Location>
Block that will called after raw data is fetched from API.
-
.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.
-
.public_url ⇒ String
Link to public facing website.
- .request_body(*args, &block) ⇒ Object
-
.request_headers ⇒ Hash
Key:value mapping of HTTP request headers.
-
.request_http_method ⇒ Symbol
Turbovax::Constants::GET_REQUEST_METHOD or.
Class Attribute Details
.data_fetcher_params ⇒ Hash
Extra params that are set by [Turbovax::DataFetcher]
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_url ⇒ Object
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_path ⇒ Object
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
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_url ⇒ String
Full API URL
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_variables ⇒ Hash
Hash or block that is interpolated
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 |
.key ⇒ String
Unique identifier for portal
44 |
# File 'lib/turbovax/portal.rb', line 44 define_parameter :key, String, "Unique identifier for portal", "Key", "'nyc_vax'" |
.name ⇒ String
Full name of portal
42 43 |
# File 'lib/turbovax/portal.rb', line 42 define_parameter :name, String, "Full name of portal", "Name", "'New York City Vaccine Website'" |
.notes ⇒ String
Extra info for users
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
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_url ⇒ String
Link to public facing website
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_headers ⇒ Hash
Key:value mapping of HTTP request headers
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_method ⇒ Symbol
Turbovax::Constants::GET_REQUEST_METHOD or
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" |