如何从api获取数据并编写函数

时间:2015-07-23 06:32:26

标签: ruby json

好的,已经很多天了。我一直在这个api上来回走动,并希望得到以下结果。

这是问题所在。

uri = URI('https://api.wmata.com/StationPrediction.svc/json/GetPrediction/All')
uri.query = URI.encode_www_form({'api_key' => 'ihaveit',})
request = Net::HTTP::Get.new(uri.request_uri)
@response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(request)
    @data = JSON.parse @response.body

现在我将@data解析为JSON。我正在尝试编写一个类Trains并使用以下数据。

{
      "Car": "6",
      "Destination": "SilvrSpg",
      "DestinationCode": "B08",
      "DestinationName": "Silver Spring",
      "Group": "1",
      "Line": "RD",
      "LocationCode": "A01",
      "LocationName": "Metro Center",
      "Min": "3"
    },

以下是Trains类的代码

class Trains
def initialize(car, destination, destinationcode, destinationname, group, line, locationcode, locationname)
  @car = car
  @destination = destination
  @destinationcode = destinationcode
  @destinationname = destinationname
  @group = group
  @line = locationcode
  @locationname = locationname
end

现在我陷入了下一步的困境。我对api完全不熟悉。我可以为静态类编写以下内容。

  def to_s
      puts(@car + @destination + @destinationcode + @destinationname + @group + @line + @locationname)
   end
   = Trains.new
   puts Train
  end

到目前为止,我已经知道了。

class TrainLoader < Struct.new(:car, :destination, :destinationcode, :destinationname, :group, :line, :locationcode, :locationname)
    class Trains

    end
    t = Trains.new(@data["Car"],@data["DestinationCode"], @data[DestinationName],@data[Group],@data[Line], @data[LocationCode], @data[LocationName], @data[Min])

1 个答案:

答案 0 :(得分:0)

你已经有了一个班级,只需写一个to_s funtcion:

class Trains
  def initialize(car, destination, destinationcode, destinationname, group, line, locationcode, locationname)
    @car = car
    @destination = destination
    @destinationcode = destinationcode
    @destinationname = destinationname
    @group = group
    @line = locationcode
    @locationname = locationname
  end

  # inspect attributes with own `to_s` method
  def to_s
    "#{@car} #{@destination} #{@destinationcode} #{@destinationname}"
  end
end

现在创建一个类的实例:

>> train = Trains.new(@data["Car"], @data["Destination"], @data["DestinationCode"])..
>> train.to_s

但是我可以用struct

建议你一个更优雅的解决方案
>> data = {
?>       "Car": "6",
?>       "Destination": "SilvrSpg",
?>       "DestinationCode": "B08",
?>       "DestinationName": "Silver Spring",
?>       "Group": "1",
?>       "Line": "RD",
?>       "LocationCode": "A01",
?>       "LocationName": "Metro Center",
?>       "Min": "3"
>>     }
>> class Trains < Struct.new(*data.keys)
#> # create a struct with attributes like keys in data hash `"Cat"` or `"Min"`                  
>> end
>> t = Trains.new(*data.values)
# create instance of Trains class with values from a data hash `"6"`
=> #<struct Trains Car="6", Destination="SilvrSpg", DestinationCode="B08", DestinationName="Silver Spring", Group="1", Line="RD", LocationCode="A01", LocationName="Metro Center", Min="3">
>> t.Car
=> "6"
>>