Sha256: 6aa68757c5bc472e3d1e5fe69519d38679d1c3377c7fed21c39c1055ef911dea

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

require_relative 'apis/yahoo_weather'

module Barometer
  module Query
    module Service
      class FromWeatherId
        def initialize(query)
          @query = query
        end

        def call
          converted_query = query.get_conversion(:weather_id)
          return unless converted_query

          @payload = YahooWeather::Api.new(converted_query).get
          parse_payload
        end

        private

        attr_reader :query, :payload

        def parse_payload
          Data::Geo.new(
            locality: locality,
            region: region,
            country: country,
            country_code: country_code,
            latitude: latitude,
            longitude: longitude
          )
        end

        def locality
          payload.fetch('location', '@city')
        end

        def region
          payload.fetch('location', '@region')
        end

        def country
          _country if _country.size > 2
        end

        def country_code
          _country if _country.size <= 2
        end

        def latitude
          payload.fetch('item', 'lat')
        end

        def longitude
          payload.fetch('item', 'long')
        end

        def _country
          @country ||= payload.fetch('location', '@country')
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
barometer-0.9.7 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.6 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.5 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.4 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.3 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.2 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.1 lib/barometer/query/services/from_weather_id.rb
barometer-0.9.0 lib/barometer/query/services/from_weather_id.rb