Sha256: c2bb071ab479d9b536672c58b504cc191b0ff2eb7165fac8597476daff0bfc1e

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'httparty'
require 'json'

module Regio
  class Core
    include HTTParty
    base_uri 'https://api.regio.ee'

    attr_accessor :options

    def initialize(options = {})
      @options = default_options.merge(options)
    end

    COMPONENTS = [
      { name: :country, type: 'A0' },
      { name: :county, type: 'A1' },
      { name: :municipality, type: 'A2' },
      { name: :settlement, type: 'A3' },
      { name: :small_place, type: 'A4' },
      { name: :street, type: 'A5' },
      { name: :farmstead, type: 'A6' },
      { name: :house, type: 'A7' },
      { name: :apartment, type: 'A8' }
    ].freeze

    def run(path, params = {})
      JSON.parse(self.class.get(path, query: params).body, symbolize_names: true)
    end

    private

    def transform(result)
      hash = default_hash_for(result)

      COMPONENTS.each do |component|
        item = result[:components].detect { |o| o[:type] == component[:type] }
        hash[component[:name]] = item.nil? ? nil : item[:name]
      end

      hash
    end

    def default_hash_for(result)
      {
        regio_id: result[:id],
        title: result[:address],
        valid: result[:is_valid],
        complete: result[:is_complete],
        zipcode: result[:postcode],
        lat: result[:geometry].last,
        lon: result[:geometry].first
      }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
regio-0.3.3 lib/regio/core.rb
regio-0.3.2 lib/regio/core.rb
regio-0.3.1 lib/regio/core.rb
regio-0.3.0 lib/regio/core.rb
regio-0.2.0 lib/regio/core.rb