Created
February 8, 2011 15:02
-
-
Save gildo/816542 to your computer and use it in GitHub Desktop.
a tiny rest client dressed in a DSL
This file contains hidden or 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
# mini rest client v1 | |
# Gildo Fiorito [email protected] | |
def on(*args, &block) | |
return super unless (name = args.first) && block | |
require 'net/http' ; require 'uri' ; require 'json' | |
klass = Class.new do | |
uri = URI.parse args[0] ; @http = Net::HTTP.new uri.host, uri.port | |
if uri.port == 443; require 'net/https'; @http.use_ssl = true;end | |
def self.get p | |
req = Net::HTTP::Get.new(p) | |
if @user and @pass;req.basic_auth @user, @pass;end | |
puts JSON.parse @http.request(req).body | |
end | |
def self.post(p,var={}) | |
req = Net::HTTP::Post.new(p) | |
req.set_form_data(var) | |
if @user and @pass;req.basic_auth @user, @pass;end | |
puts @http.request(req).body | |
end | |
def self.conf(&block);instance_eval █end | |
end | |
klass.class_eval do; attr_accessor :http; end | |
klass.class_eval &block | |
end |
This file contains hidden or 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
require './minirest' | |
on "https://convore.com" do | |
conf do | |
@user = "fyskij" | |
@pass = "mypass" | |
end | |
get "/api/account/verify.json" | |
get "/api/groups.json" | |
post "https://convore.com/api/topics/1902/messages/create.json", | |
{:message => "hey!, this is shitty awesome"} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment