尝试编写包含weather API的代码

时间:2014-02-27 00:04:49

标签: ruby

这是我要在命令行运行的天气应用程序的一些代码。我根本无法让它运行。

如何实施此天气API以便向我提供旧金山天气的反馈?

require 'json'
require 'rest-client'

class Weather
  attr_accessor :weather
  def initialize
    weather = []
  end

  def play
    puts "Hi, this app is about to tell you the current weather in San Francisco! It is called CaliSunshine and is meant to bum you out about being so cold, would you like to proceed????? (Yes or No)"
    get_input 
  end

  if get_input == "yes"
    puts "#{temp_f}"
  elsif 
    puts "Too Bad! #{temp_f} dont you just wish you were in San Francisco?!"
  end
end 


def get_input
  print ">>>"
  user_input = gets
  user_input = user_input.strip
end

def SF_weather

  response_hash = JSON.load(RestClient.get('http://api.wunderground.com/api/a68a17ccd80a1ab1/geolookup/conditions/q/CA/San_Francisco.json')) 
  response_hash["current_observation"].each do |temp|
  end

end

Weather.new

1 个答案:

答案 0 :(得分:0)

你需要把事情整理好:

require 'json'
require 'rest-client'

class Weather
  attr_accessor :weather

  def initialize
    weather = []
  end

  def play
    puts "Hi, this app is about to tell you the current weather in San Francisco! It is called CaliSunshine and is meant to bum you out about being so cold, would you like to proceed????? (Yes or No)"

    if get_input == "yes"
      puts "#{temp_f}"
    elsif
      puts "Too Bad! #{temp_f} dont you just wish you were in San Francisco?!"
    end
  end

  def get_input
    print ">>> "
    gets.chomp
  end

  def temp_f
    response_hash = JSON.parse(RestClient.get('http://api.wunderground.com/api/a68a17ccd80a1ab1/geolookup/conditions/q/CA/San_Francisco.json'))
    response_hash["current_observation"]["temp_f"]
  end
end

Weather.new.play