Skip to content

Instantly share code, notes, and snippets.

@gdotdesign
Created March 25, 2012 19:50
Show Gist options
  • Save gdotdesign/2199330 to your computer and use it in GitHub Desktop.
Save gdotdesign/2199330 to your computer and use it in GitHub Desktop.
INK API Implementation
require "net/http"
require 'cgi'
class Hash
# Create a Query String from a Hash
def to_query
def to_query
map { |k,v|
::CGI.escape(k.to_s)+"="+::CGI.escape(v)
}.join("&")
end
end
end
class Ink
# Constructor
def initialize(options)
@secret = options[:secret].to_s
@token = options[:token].to_s
end
# Create a digest string from a Hash
def digest(hash)
::OpenSSL::HMAC::hexdigest("sha256", @secret, hash.to_query)
end
# Fetch a render document
def fetch(view)
data = {:view => view.to_s, :type => 'haml'}
data[:digest] = digest(data)
data[:token] = @token
uri = URI("http://inkit.org/api/document?"+data.to_query)
response = ::Net::HTTP.get uri
end
end
ink = Ink.new({
:secret => "dK0UBjOLKmm",
:token => "NeQXssZHuiX"
})
run lambda { |env|
[200, {"Content-Type" => "text/plain"}, [ink.fetch(:test01)]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment