将密钥添加到我正在创建的红宝石哈希中

时间:2019-01-23 17:13:13

标签: ruby hash

因此,我决定在用眼科带状疱疹签字的同时改善我的Ruby。

我编写了此脚本是为了从MetOffice的Datapoint API获取预测数据。

我被困住的是

#!/bin/ruby
# frozen_string_literal: true

#
# This script get the list of UK forecast locations
#
require 'pp'
require 'net/http'
require 'json'
require 'uri'

@apikey = 'insert-key-here'
@base_url = 'http://datapoint.metoffice.gov.uk/public/data/'
@format = 'json'

# Make API call to get list of UK locations

def sitelist_data
  res = "val/wxfcs/all/#{@format}/sitelist?"
  url = "#{@base_url}#{res}key=#{@apikey}"
  url = URI.parse(url)
  resp = Net::HTTP.get(url)
  data = ::JSON.parse(resp)
  data['Locations']['Location'] # Step into the array to get location list
end

# All i need from the list is Authority name and Site name
# Create a list to display all available locations
def sitelist
  @fulllist = sitelist_data
  region_list = {}
  @fulllist.map do |k, _v|
    k.each do |key, value|
      @auth = value if key == 'unitaryAuthArea'
      @name = value if key == 'name'
    end
    region_list[@auth] = @name
  end
  region_list # Return list of locations
end

# Get a 3 hrly forecast for the next 5 days for the chosen location

# Get the raw 3hrly data for a specific region 
def three_hourly_forecast_raw(region)
  res = 'val/wxfcs/all/'
  reg = region
  url = "#{@base_url}#{res}#{@format}/#{reg}?res=3hourly&key=#{@apikey}"
  url = URI.parse(url)
  resp = Net::HTTP.get(url)
  data = ::JSON.parse(resp)
  data['SiteRep']['DV']['Location'] # Step into array to get to forecasts data
end

# Get the headders from the data id, name, longitude and latittude
def three_hourly_forecast_headder(region)
  raw_data = three_hourly_forecast_raw(region)
  raw_data.each do |key, value|
    @id = value if key == 'i'
    @reg = value if key == 'name'
    @lon = value if key == 'lon'
    @lat = value if key == 'lat'
  end
end

# Create a hash of the forecast data with new keys as
# the ones provided by met office are not great
def three_hourly_forecast_values(region)
  three_hourly_forecast = {}
  raw_data = three_hourly_forecast_raw(region)
  raw_data['Period'].map do |key, _value|
    @date = key['value']
    key['Rep'].map do |weather_data, _v|
      three_hourly_forecast[@date] = forecast_hash(weather_data)
    end
  end
end

# Compile weather data hash
def forecast_hash(weather_data)
  {
    hr: weather_data["\$"],
    feels_like: weather_data['F'], # unit = c
    w_gust: weather_data['G'], # unit = mph
    rel_humid: weather_data['H'], # unit = %
    temp: weather_data['T'], # unit = c
    visability: weather_data['V'],
    wind_dir: weather_data['D'], # unit = compass
    wind_speed: weather_data['S'], # unit = mph
    max_uv: weather_data['U'],
    type: weather_data['W'],
    percipitation_probability: weather_data['Pp'] # unit = %
  }
end

pp three_hourly_forecast_values('310069')

运行脚本的那一刻,我得到了

[{:hr=>"0",
   :feels_like=>"-1",
   :w_gust=>"40",
   :rel_humid=>"76",
   :temp=>"4",
   :visability=>"GO",
   :wind_dir=>"NW",
   :wind_speed=>"22",
   :max_uv=>"0",
   :type=>"7",
   :percipitation_probability=>"13"},
  {:hr=>"180",
   :feels_like=>"-1",
   :w_gust=>"40",
   :rel_humid=>"75",
   :temp=>"4",
   :visability=>"GO",
   :wind_dir=>"NW",
   :wind_speed=>"22",
   :max_uv=>"0",
   :type=>"2",
   :percipitation_probability=>"10"},
  {:hr=>"360",
   :feels_like=>"-1",
   :w_gust=>"40",
   :rel_humid=>"74",
   :temp=>"4",
   :visability=>"VG",
   :wind_dir=>"NW",
   :wind_speed=>"22",
   :max_uv=>"0",
   :type=>"2",
   :percipitation_probability=>"6"},
  {:hr=>"540",
   :feels_like=>"-1",
   :w_gust=>"36",
   :rel_humid=>"74",
   :temp=>"4",
   :visability=>"VG",
   :wind_dir=>"NNW",
   :wind_speed=>"20",
   :max_uv=>"1",
   :type=>"3",
   :percipitation_probability=>"4"},
  {:hr=>"720",
   :feels_like=>"1",
   :w_gust=>"40",
   :rel_humid=>"63",
   :temp=>"6",
   :visability=>"VG",
   :wind_dir=>"NNW",
   :wind_speed=>"22",
   :max_uv=>"1",
   :type=>"3",
   :percipitation_probability=>"3"},
  {:hr=>"900",
   :feels_like=>"1",
   :w_gust=>"34",
   :rel_humid=>"63",
   :temp=>"6",
   :visability=>"VG",
   :wind_dir=>"NW",
   :wind_speed=>"20",
   :max_uv=>"1",
   :type=>"3",
   :percipitation_probability=>"4"},
  {:hr=>"1080",
   :feels_like=>"0",
   :w_gust=>"27",
   :rel_humid=>"71",
   :temp=>"4",
   :visability=>"VG",
   :wind_dir=>"NW",
   :wind_speed=>"16",
   :max_uv=>"0",
   :type=>"0",
   :percipitation_probability=>"3"},
  {:hr=>"1260",
   :feels_like=>"-1",
   :w_gust=>"20",
   :rel_humid=>"77",
   :temp=>"3",
   :visability=>"VG",
   :wind_dir=>"NW",
   :wind_speed=>"13",
   :max_uv=>"0",
   :type=>"0",
   :percipitation_probability=>"3"}]

我想要的是日期作为每个哈希的键,所以看起来像这样。

[日期=> { 天气数据 }

我在做什么错??

这纯粹是一个学习项目,因此它不一定是超级高效的,但如果您发现任何我可以改进的地方,请随时指出。

谢谢

3 个答案:

答案 0 :(得分:1)

three_hourly_forecast_values方法将返回.map调用的结果,而不是返回在map块内部构建的新哈希,因为它是最后一个对象。您可以将three_hourly_forecast哈希作为该方法的最后一行,以返回新哈希。

def three_hourly_forecast_values(region)
  three_hourly_forecast = {}
  raw_data = three_hourly_forecast_raw(region)
  raw_data['Period'].map do |key, _value|
    @date = key['value']
    three_hourly_forecast[@date] = []
    key['Rep'].map do |weather_data, _v|
      three_hourly_forecast[@date] << forecast_hash(weather_data)
    end
  end
  three_hourly_forecast
end

答案 1 :(得分:1)

您正在将map_function内部的Three_hourly_forecast [@date]分配给Forecast_hash(weather_data)。 map函数返回最后一行,在这种情况下,它只是哈希hash_hash(weather_data)。更改每个地图以进行修复。

 def three_hourly_forecast_values(region)
  three_hourly_forecast = {}
  raw_data = three_hourly_forecast_raw(region)
  raw_data['Period'].each do |key, _value|
    @date = key['value']
    key['Rep'].each do |weather_data, _v|
      three_hourly_forecast[@date] = forecast_hash(weather_data)
    end
  end
end

答案 2 :(得分:0)

我假设您将数据放入名为 data

的变量中
data = [{:hr=>"0",
         :feels_like=>"-1",
         :w_gust=>"40",
         :rel_humid=>"76",
         :temp=>"4",
         :visability=>"GO",
         :wind_dir=>"NW",
         :wind_speed=>"22",
         :max_uv=>"0",
         :type=>"7",
         :percipitation_probability=>"13"},
        ]

 data.each do |f|
   f.merge!(date: "Whether data")
  end

输出

       [{:hr=>"0",
         :feels_like=>"-1",
         :w_gust=>"40",
         :rel_humid=>"76",
         :temp=>"4",
         :visability=>"GO",
         :wind_dir=>"NW",
         :wind_speed=>"22",
         :max_uv=>"0",
         :type=>"7",
         :percipitation_probability=>"13"},
         :date => "Whether data"
        ]

这将在每个哈希后面附加及其。 希望对您有所帮助。