class ShackKit::Data::HamQTH

Constants

QUERY_URL

Attributes

session_key[R]

Public Class Methods

new(params = {}) click to toggle source
# File lib/shack_kit/data/ham_qth.rb, line 7
def initialize(params = {})
  return false unless params[:login] && params[:password] || CONFIG && CONFIG.has_key?('ham_qth')
  login = params[:login] || CONFIG['ham_qth']['login']
  password = params[:password] || CONFIG['ham_qth']['password']
  response = HTTP.get("#{QUERY_URL}?u=#{login}&p=#{password}")
  parsed_response = Oga.parse_xml(response.to_s)
  @session_key = parsed_response.xpath("HamQTH/session/session_id").text
  puts parsed_response.xpath("HamQTH/session/error").text if @session_key.empty?
end

Public Instance Methods

lookup(callsign) click to toggle source
# File lib/shack_kit/data/ham_qth.rb, line 17
def lookup(callsign)
  return { error: "Can't query HamQTH.com without submitting valid login credentials first" } if @session_key.nil? || @session_key.empty?
  response = HTTP.get("#{QUERY_URL}?id=#{@session_key}&callsign=#{callsign}&prg=#{USER_AGENT}")
  parsed_response = Oga.parse_xml(response.to_s)
  return { error: parsed_response.xpath("HamQTH/session/error").text } if
    parsed_response.xpath("HamQTH/search").text.empty?
  attributes = parsed_response.xpath("HamQTH/search").first.children.select{ |c| c.class == Oga::XML::Element }.map(&:name)
  {}.tap do |output|
    attributes.each do |attribute|
      output[attribute.to_sym] = parsed_response.xpath("HamQTH/search/#{attribute}").text
    end
  end
end