class VaccineSlotFinder::VaccineFinder

Public Class Methods

new(pincode) click to toggle source
# File lib/vaccine_slot_finder.rb, line 12
def initialize(pincode)
  @pincode = pincode
  @user_pincodes = init_bengaluru_pincodes
end

Public Instance Methods

by_pincode(query_params) click to toggle source
# File lib/vaccine_slot_finder.rb, line 38
def by_pincode(query_params)
  data = self.class.get("/calendarByPin", query_params)
  parsed_data = JSON.parse(data.body)
  relevant_data = extract_relevant_data(parsed_data)
  puts 'No vaccines available for age group 18-45! Stay at HOME please!' if relevant_data.empty?
  puts relevant_data
  send_to_teams_channel unless relevant_data.empty?
  # send_sms_to_me unless relevant_data.empty?
end
extract_relevant_data(parsed_data) click to toggle source
# File lib/vaccine_slot_finder.rb, line 58
def extract_relevant_data(parsed_data)
  useful_data = []
  parsed_data['centers'].each do |row|
    vaccine_available = row['sessions'].select{ |hsh| hsh['min_age_limit'] != 45 && hsh['available_capacity'] != 0 }
    next if vaccine_available.empty?
    useful_data << '==' * 20
    useful_hash = {
        'Center Name' => row['name'],
         'Available Slots' => extract_slots(row)
    }
    useful_data << useful_hash
  end
  useful_data
end
extract_slots(row) click to toggle source
# File lib/vaccine_slot_finder.rb, line 73
def extract_slots(row)
  sessions = []
  row['sessions'].each do |session|
    useful_hash = {
        "Date" => session['date'],
        "Available Capacity" => session['available_capacity'],
        "Age limit" => session['min_age_limit'],
        "Vaccine Given" => session['vaccine'],
        "Total Slots Available" => session['slots']
    }

    sessions << useful_hash
  end
  sessions
end
init_bengaluru_pincodes() click to toggle source
# File lib/vaccine_slot_finder.rb, line 26
def init_bengaluru_pincodes
  yml = YAML.load_file(File.expand_path('~/Desktop/bengaluru_pincodes.yaml'))
  # yml = YAML.load_file('lib/vaccine_slot_finder/bengaluru_pincodes.yaml')
  yml = yml['pincodes'].split(" ")
  yml = yml.map(&:to_i).sort.uniq!
  pincode_index = yml.find_index(@pincode.to_i)
  last_2_ele_index = [yml.length, (yml.length - 1)]
  return yml[0..5] if [0, 1].include?(pincode_index)
  return yml[-1..-5] if last_2_ele_index.include?(pincode_index)
  yml[pincode_index - 2..pincode_index + 2]
end
run() click to toggle source
# File lib/vaccine_slot_finder.rb, line 17
def run
  date = Time.now.strftime('%d/%m/%Y')
  @user_pincodes.each do |pincode|
    puts "Searching for pincode:- #{pincode}"
    query_params = { query: { pincode: pincode, date: date } }
    by_pincode(query_params)
  end
end
send_sms_to_me() click to toggle source
# File lib/vaccine_slot_finder.rb, line 48
def send_sms_to_me
  @client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
  message = @client.messages.create(
      body: "Pincode:- #{@pincode} seems to have have some vaccine slots available for you!",
      to: ENV['TWILIO_TO'],    # Replace with your phone number
      from: ENV['TWILIO_FROM'])  # Use this Magic Number for creating SMS

  puts "Message sent with message using ID:- #{message.sid} and for pincode:- #{@pincode}"
end
send_to_teams_channel() click to toggle source

Push to Microsoft Teams channel. Needs webhook url, where the data will be posted.

# File lib/vaccine_slot_finder.rb, line 90
def send_to_teams_channel
  response = self.class.post(ENV['TEAMS_WEBHOOK'],
                             :body => {"text" => "Pincode:- #{@pincode} has some vaccine for 18-45 age group. Try booking now!"}.to_json,
                             :headers => { 'Content-Type' => 'application/json' } )
  puts response.code
end