Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/http'
- require 'json'
- require 'pp'
- class StiftungAPI
- attr_reader :params, :languages, :specialties, :special_requests, :special_services,
- :base_uri, :payment_services
- def initialize
- @base_uri = URI(ENV['stiftung_base_url'])
- @params = {
- akey: ENV['stiftung_key'],
- }
- # Fine for now, only from English. Should have multilingual support
- # Move to .json files
- @languages = {
- 'arabic': '256',
- 'spanish': '102',
- 'french': '86',
- 'english': '36',
- 'chinese': '1449',
- 'turkish': '285',
- 'portuguese': '779',
- 'polish': '37',
- 'italian': '153',
- 'dutch': '1316',
- 'greek': '1030',
- 'japanese': '1103',
- 'norwegian': '1529',
- 'romanian': '471',
- 'russian': '38',
- 'indonesian': '1684',
- 'finnish': '419',
- 'hindi': '1767',
- 'korean': '1332'
- }
- @special_requests = {
- 'evening_hours': '120',
- 'weekend_hours': '46',
- 'house_visit': '29'
- }
- @specialties = {
- 'dentist': 'zahnmedizin',
- 'doctor': 'allgemeinmedizin',
- 'general_practicioner': 'allgemeinmedizin',
- 'oftalmologist': 'augenarzt',
- 'gynecologist': 'frauenarzt'
- }
- @special_services = {
- 'Abendsprechstunden': :evening_hours,
- 'Wochenendsprechstunden': :weekend_hours,
- 'Termine nur nach Vereinbarung': :appointment_only,
- 'weitere Sprechzeiten nach Vereinbarung': :extra_hours_with_appointment,
- 'mit Bus und Bahn erreichbar': :bus_and_metro,
- 'Parkplätze nahe der Praxis': :parking,
- 'Erinnerung an Vorsorge': :appointment_periodical_reminder,
- 'Hausbesuche': :house_visit,
- 'Homepage angegeben': :homepage,
- 'Services im Wartezimmer': :waiting_room_services
- }
- @payment_services = {
- 'Kasse, Privat und Selbstzahler': :all,
- 'gesetzlich Versicherte (nur bestimmte Therapien)': :publicly_insured,
- 'nur Privatpatienten/Selbstzahler': :only_private,
- 'Kasse mit Einschränkungen': :copayment,
- 'Privatpatienten/ Kostenerstattungsverfahren': :partially_refunded
- }
- end
- def self.get_doctor_details(query)
- @api ||= StiftungAPI.new
- @api.get_doctor_details(query)
- end
- # Returns the details on a doctor
- def get_doctor_details(query)
- # Checks whether doctor is available in database with all data loaded
- doctor = Doctor.find_by(doctor_id: query[:doctor_id])
- if doctor.nil? || doctor.patient_services.nil?
- doctor = request({
- a: 'DD',
- e_id: query[:doctor_id],
- userip: query[:user_ip] || ENV['stiftung_default_user_ip']
- })
- end
- # TODO: remove this line
- {doctor: doctor.attributes.except("id").merge(id: doctor.doctor_id)}
- end
- def self.get_api_details(query)
- @api ||= StiftungAPI.new
- @api.get_api_details(query)
- end
- # Returns the details on a doctor
- def get_api_details(query)
- request({
- a: 'FS1',
- userip: query[:user_ip] || ENV['stiftung_default_user_ip']
- })
- end
- def self.get_doctors_list(query)
- @api ||= StiftungAPI.new
- @api.get_doctors_list(query)
- end
- def get_doctors_list(query)
- adapted_query = {
- a: 'DL',
- Lng: languages[query.dig(:language).to_sym],
- Ft: specialties[query.dig(:specialty)&.to_sym]|| 'Allgemeinmedizin',
- Ftg: query.dig(:location),
- userip: query[:user_ip] || ENV['stiftung_default_user_ip']
- }
- request(adapted_query)
- end
- private
- # Sends formatted request to API
- def request(query)
- p "MAKING QUERY"
- pp query
- base_uri.query = URI.encode_www_form(params.merge(query))
- res = Net::HTTP.get_response(base_uri)
- format_result(res.body)
- end
- # Cleans up the result
- def format_result(body)
- result = JSON.parse(body)
- pp result
- if result.has_key?('entry')
- format_doctor_details(result)
- elsif result.has_key?('entry_List')
- format_doctors_list(result)
- else
- {}
- end
- end
- # Formats details of a doctor
- def format_doctor_details(result)
- entry = result['entry']
- get_doctor(entry)
- end
- def format_doctors_list(result)
- entries = result['entry_List']
- entries.each_with_object([]) do |entry, list|
- # TODO: change this ASAP, when controllers are refactored
- # list.push(get_doctor(entry))
- doctor = get_doctor(entry)
- list.push(doctor.attributes.except("id").merge(id: doctor.doctor_id))
- end
- end
- # Given an entry from API extract information
- # and return a doctor, or find a new one
- def get_doctor(entry)
- doctor = Doctor.find_or_create_by(doctor_id: entry["e_id"]) do |doctor|
- doctor.doctor_id = entry["e_id"]
- doctor.name = "#{entry['title']} #{entry['firstname']} #{entry['lastname']}"
- doctor.clinic = entry["business"]
- doctor.clinic_title = entry["nameExtension"]
- doctor.latitude = entry["geoLat"]
- doctor.longitude = entry["geoLong"]
- doctor.postal_code = entry["zipCode"]
- doctor.city = entry["place"]
- doctor.address = entry["street"]
- doctor.phone = entry["phone1"]
- doctor.languages = unpack_languages(entry) if entry.key?("orientation_List")
- doctor.patient_services = unpack_patient_services(entry) if entry.key?("indicator_List")
- doctor.payment_services = unpack_payment_services(entry) if entry.key?("indicator_List")
- end
- # If record was loaded from list request and we perform a detail
- # request update doctor information with detail information
- doctor.tap do |selected_doctor|
- if (selected_doctor.patient_services.nil? && entry.key?("indicator_List"))
- selected_doctor.update_attributes({
- languages: unpack_languages(entry),
- patient_services: unpack_patient_services(entry),
- payment_services: unpack_payment_services(entry)
- })
- end
- end
- end
- # These methods deal with the strange and convoluted API format
- # If you think you can NOT codegolf your way around it, good luck
- # And Fuck you also, smartass.
- def unpack_languages(entry)
- entry["orientation_List"].select { |i| i["type"] == '6' }.first["lbl_List"].map(&:values).flatten
- end
- # Nice, uh?
- def unpack_patient_services(entry)
- services = entry["indicator_List"].select { |i| i["name"] == 'Ic3' }.first["lbl_List"]
- services.map(&:values).map do |service|
- [special_services[service[0].to_sym], service[1] == '1']
- end.to_h
- end
- def unpack_payment_services(entry)
- services = entry["indicator_List"].select { |i| i["name"] == 'Ic1' }.first["lbl_List"]
- services.map(&:values).map do |service|
- [payment_services[service[0].to_sym], service[1] == '1']
- end.to_h
- end
- end
- # pp StiftungAPI.get_doctor_details(doctor_id: '617549')
- # 617549
- # pp StiftungAPI.get_doctors_list(language: 'italian',specialty: 'doctor',location: '04179')
- # TODO:
- # - Abstract unpacking for Icn
- # - Maybe this stuff should be moved to the database?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement