Sha256: 5dcfecc026b2f06b3eb14350c1d282f764802d46f21ea1fe9b2e01e8120e77ee

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'vfrmap/version'
require 'vfrmap/location'
require 'uri'
require 'addressable/uri'
require 'padrino-helpers'
require 'open-uri'
require 'base64'

class Vfrmap
  include Padrino::Helpers::TagHelpers

  DEFAULT_OPTIONS = {
    type: 'vfrc',
    zoom: 10,
    width: 350,
    height: 350,
    class: nil,
    alt: nil
  }

  attr_accessor :options

  def initialize(location_string,options={})
    @location = Location.factory(location_string)
    raise 'Invalid location' unless @location
    @options = DEFAULT_OPTIONS.merge(options)
  end

  def uri
    URI::HTTP
      .build(host: 'vfrmap.com', path: '/api', query: query_params)
      .to_s
  end
  alias_method :src, :uri

  def to_html
    tag(:img, src: src, class: @options[:class], alt: @options[:alt])
  end

  def to_jpg
    open(uri).read
  end

  def to_base64_jpg
    Base64.encode64(to_jpg)
  end

  private

  def location
    @location
  end

  def latitude
    case
    when @location.is_a?(Geo::Coord)
      (@location.to_h[:lat]*10000000).round / 10000000.0
    when @location.is_a?(Airports::Airport)
      @location.latitude.to_f
    end
  end

  def longitude
    case
    when @location.is_a?(Geo::Coord)
      (@location.to_h[:lng]*10000000).round / 10000000.0
    when @location.is_a?(Airports::Airport)
      @location.longitude.to_f
    end
  end

  def query_params
    uri = Addressable::URI.new
    uri.query_values = {
      req:    'map',
      type:   @options[:type],
      lat:    latitude,
      lon:    longitude,
      zoom:   @options[:zoom],
      width:  @options[:width],
      height: @options[:height]
    }
    uri.query
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vfrmap-1.1.0 lib/vfrmap.rb
vfrmap-1.0.1 lib/vfrmap.rb
vfrmap-1.0.0 lib/vfrmap.rb