Last active
March 7, 2019 23:38
-
-
Save clementi/8b68e75dc90be95492be24ec0f771422 to your computer and use it in GitHub Desktop.
CLI for http://pointfree.io
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
#!/usr/bin/env ruby | |
require 'json' | |
require 'net/http' | |
def success?(status_code) | |
status_code[0] == '2' | |
end | |
begin | |
code = ARGV.length < 1 ? ARGF.read : ARGV[0] | |
uri = URI("http://pointfree.io/snippet?code=#{URI.encode_www_form_component(code)}") | |
req = Net::HTTP::Get.new(uri) | |
req['Accept'] = 'application/json' | |
res = Net::HTTP.start(uri.hostname, uri.port) do |http| | |
http.request(req) | |
end | |
success?(res.code) or raise IOError.new("Received HTTP #{res.code}") | |
parsed = JSON.parse(res.body) | |
puts parsed['code'] | |
rescue StandardError => e | |
puts "Error: #{e.message}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment