Skip to content

Instantly share code, notes, and snippets.

@DNNX
Created July 1, 2014 17:51
Show Gist options
  • Save DNNX/bba4cda01f2c9aafb0a8 to your computer and use it in GitHub Desktop.
Save DNNX/bba4cda01f2c9aafb0a8 to your computer and use it in GitHub Desktop.
Ruby client for rest-example
module RestexampleApi
@version = '1.0.0'
class BaseApi
def initialize (resUrl)
@url = resUrl + '/v1.0.0/'
@api = self
end
def Post ()
return AccessPost.new(@url + 'post/', @api)
end
def User ()
return AccessUser.new(@url + 'user/', @api)
end
end
class AccessPost
def initialize (resUrl, myApi)
@url = resUrl
@api = myApi
end
def byId (integer)
return Post.new(@url + 'id/' + integer + '/', @api, :json)
end
def latest ()
return Post.new(@url + 'latest/', @api, :json)
end
def list (params = {}, headers = {})
internalSilkRequest(@api, :get, @url + '', params, 'text/plain', :json, nil, headers)
end
def removeManyById (json, params = {}, headers = {})
internalSilkRequest(@api, :delete, @url + 'id/', params, 'text/json', :json, mkJson(json), headers)
end
def create (json, params = {}, headers = {})
internalSilkRequest(@api, :post, @url + '', params, 'text/json', :json, mkJson(json), headers)
end
end
class Post
def initialize (resUrl, myApi, retData = :data)
@url = resUrl
@dataType = retData
@api = myApi
end
def get (params = {}, headers = {})
internalSilkRequest(@api, :get, @url, params, 'text/plain', @dataType, headers)
end
def remove (params = {}, headers = {})
internalSilkRequest(@api, :delete, @url + '', params, 'text/plain', :data, nil, headers)
end
def Comment ()
return AccessPostComment.new(@url + 'comment/', @api)
end
end
class AccessPostComment
def initialize (resUrl, myApi)
@url = resUrl
@api = myApi
end
def list (params = {}, headers = {})
internalSilkRequest(@api, :get, @url + '', params, 'text/plain', :json, nil, headers)
end
def create (json, params = {}, headers = {})
internalSilkRequest(@api, :post, @url + '', params, 'text/json', :json, mkJson(json), headers)
end
end
class PostComment
def initialize (resUrl, myApi, retData = :data)
@url = resUrl
@dataType = retData
@api = myApi
end
def get (params = {}, headers = {})
internalSilkRequest(@api, :get, @url, params, 'text/plain', @dataType, headers)
end
end
class AccessUser
def initialize (resUrl, myApi)
@url = resUrl
@api = myApi
end
def list (params = {}, headers = {})
internalSilkRequest(@api, :get, @url + '', params, 'text/plain', :json, nil, headers)
end
def create (json, params = {}, headers = {})
internalSilkRequest(@api, :post, @url + '', params, 'text/json', :json, mkJson(json), headers)
end
end
class User
def initialize (resUrl, myApi, retData = :data)
@url = resUrl
@dataType = retData
@api = myApi
end
def get (params = {}, headers = {})
internalSilkRequest(@api, :get, @url, params, 'text/plain', @dataType, headers)
end
end
endrequire 'net/http'
require 'uri'
require 'rubygems'
require 'cgi'
require 'json'
require 'rexml/document'
print "Fix url encoding when this is going to be used"
def internalSilkRequest (api, method, url, params = {}, contentType = 'text/plain', dataType = :data, data = nil, headers = {})
uri = URI(url + '?' + params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&'))
request = case method
when :get then Net::HTTP::Get.new(uri.request_uri)
when :post then Net::HTTP::Post.new(uri.request_uri)
when :put then Net::HTTP::Put.new(uri.request_uri)
when :delete then Net::HTTP::Delete.new(uri.request_uri)
end
request.initialize_http_header(headers)
request.body = data
request['Content-Type'] = contentType
request['Cookie'] = api.cookies.to_a.map { |kv| kv.join('=') } .join(';')
case dataType
when :data then request['Accept'] = 'text/plain'
when :json then request['Accept'] = 'application/json'
when :xml then request['Accept'] = 'application/xml'
end
response = Net::HTTP.new(uri.host, uri.port).request request
pdat = response.body
if response.code == '200'
pdat = case dataType
when :json then JSON.parse response.body
when :xml then REXML::Document.new response.body
end
end
return pdat, response
end
def mkJson (x)
if x.class == Hash
return JSON.generate x
else
return x.to_s
end
end
module SilkApi
class Api < BaseApi
attr_accessor :cookies
def initialize (apiURL)
super(apiURL)
@cookies = {}
end
def login (email, password)
pdat, res = self.User.signin({'email' => email, 'password' => password})
cookie = res['Set-Cookie']
@cookies['silk_sid'] = cookie.sub /.*silk_sid=([^;]+).*/, '\1'
@cookies['silk_tid'] = cookie.sub /.*silk_tid=([^;]+).*/, '\1'
return pdat, res
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment