-
-
Save lyrical-hogege/ede40b9e6e69f96df3a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$appid = ARGV[0] | |
class YahooAPIBase | |
USER_AGENT = "webapi_test" | |
$appid | |
def build_querystring(params) | |
query = [] | |
params.each do |k,v| | |
query << "#{k}=#{v}" | |
end | |
query << "output=json" | |
"?"+query.join("&") | |
end | |
end | |
class WeatherAPI < YahooAPIBase | |
def exec(req_params = { "coordinates" => "35.647306,140.034503" }) | |
protocol = "http" | |
domain = "weather.olp.yahooapis.jp" | |
endpoint = "/v1/place" | |
port = "80" | |
method = "GET" | |
req_params.merge!({ | |
"appid" => $appid, | |
"past" => 1 | |
}) | |
query = build_querystring(req_params) | |
SimpleHttp.new(protocol, domain, port).request(method, endpoint+query, {'User-Agent' => "#{USER_AGENT}"}).body | |
end | |
end | |
class Cli | |
def initialize | |
@cli_commands = {} | |
end | |
def add_command(command) | |
@cli_commands.merge!(command) | |
end | |
def run | |
comp = proc { |s| @cli_commands.keys.grep( /^#{s}/ ) } | |
Readline.completion_append_character = " " | |
Readline.completion_proc = comp | |
while command = Readline.readline('hogegeshell$ ', true) | |
toplevel_command = command.split[0] | |
if @cli_commands[toplevel_command] | |
@cli_commands[toplevel_command].call command | |
else | |
puts "#{toplevel_command} is not defined." | |
end | |
end | |
end | |
end | |
cli = Cli.new | |
cli.add_command({ | |
"weather" => proc { |s| | |
api = WeatherAPI.new | |
puts api.exec | |
} | |
}) | |
cli.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment