Sha256: 5ec3b155b1c22e682445dc47354ca2644948e91acd1ee8f77056223b16a27cd8

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

module ShackKit
  module Data
    class HamQTH
      attr_reader :session_key
      QUERY_URL = 'https://www.hamqth.com/xml.php'

      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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shack_kit-0.2.2 lib/shack_kit/data/ham_qth.rb
shack_kit-0.2.1 lib/shack_kit/data/ham_qth.rb
shack_kit-0.2.0 lib/shack_kit/data/ham_qth.rb