Sha256: c9b6ae0924d0159bc16c8a3f8c41be782e27957e629d04e2c944aeac04fbd92f

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'open-uri'
require 'sora_geocoding/base'

module SoraGeocoding
  #
  # generate url
  #
  class Url < Base
    attr_accessor :site

    BASE_YAHOO_URL = 'https://map.yahooapis.jp/geocode/V1/geoCoder'.freeze
    BASE_GEOCODING_URL = 'https://www.geocoding.jp/api/'.freeze

    def initialize(query)
      @yahoo_appid = configuration.yahoo_appid
      @site = configuration.site
      @query = query
    end

    def get
      switch_site
      select
    end

    private
      def select
        if @yahoo_appid.nil? || @site == 'geocoding'
          geocoding
        elsif @site.nil? || @site == 'yahoo'
          yahoo
        end
      end

      def switch_site
        if @yahoo_appid.nil? || @site == 'geocoding'
          @site = 'geocoding'
        elsif @site.nil? || @site == 'yahoo'
          @site = 'yahoo'
        else
          raise 'Please specify the correct site.'
        end
      end

      def yahoo
        params = {
          appid: @yahoo_appid,
          query: @query,
          results: '1',
          detail: 'full',
          output: 'xml'
        }
        "#{BASE_YAHOO_URL}?#{encode_uri(params)}"
      end

      def geocoding
        params = { q: @query }
        "#{BASE_GEOCODING_URL}?#{encode_uri(params)}"
      end

      def encode_uri(params)
        URI.encode_www_form(params)
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sora_geocoding-0.2.2 lib/sora_geocoding/url.rb
sora_geocoding-0.2.0 lib/sora_geocoding/url.rb
sora_geocoding-0.1.0 lib/sora_geocoding/url.rb