class Endorser::Client
Attributes
key[R]
realm[R]
secret[R]
Public Class Methods
new(key, secret, realm)
click to toggle source
# File lib/endorser/client.rb, line 6 def initialize(key, secret, realm) @key = key @secret = secret @realm = realm end
Public Instance Methods
build_receipt(args={})
click to toggle source
# File lib/endorser/client.rb, line 103 def build_receipt(args={}) headers = args[:headers] method = args[:method].to_s url = args[:url] receipt = Endorser::Receipt.new receipt.host = host(url) receipt.uri = uri_path_and_query(url) receipt.request_method = method receipt.api_key = key receipt.api_secret = secret receipt.content_type = headers['Content-Type'] receipt.body = RestClient::Payload.generate(args[:payload]).to_s receipt end
delete(url, headers={})
click to toggle source
@param [String] url @param [Hash] headers @raise [Sacred::Errors::Error] if an error is encountered with sacred @return [Hash]
# File lib/endorser/client.rb, line 56 def delete(url, headers={}) execute({ method: :delete, url: url, headers: headers }) end
execute(args={})
click to toggle source
Wrapper around RestClient::Request.execute method
@param [Hash] args @raise [Sacred::Errors::Error] when the request fails @return [Hash]
# File lib/endorser/client.rb, line 69 def execute(args={}) RestClient::Request.execute(signed_request(args)) do |response, request, result, &block| Endorser::Response.new(response, request) end end
get(url, headers={})
click to toggle source
@param [String] url @param [Hash] headers @raise [Sacred::Errors::Error] if an error is encountered with sacred @return [Hash]
# File lib/endorser/client.rb, line 16 def get(url, headers={}) execute({ method: :get, url: url, headers: headers }) end
host(url)
click to toggle source
# File lib/endorser/client.rb, line 128 def host(url) uri = URI(url) value = [uri.scheme, '://', uri.host] if include_port?(uri) value += [':', uri.port.to_s] end value.join end
include_port?(uri)
click to toggle source
# File lib/endorser/client.rb, line 139 def include_port?(uri) (uri.scheme != 'http' || uri.port != 80) && (uri.scheme != 'https' || uri.port != 443) end
post(url, payload={}, headers={})
click to toggle source
@param [String] url @param [Hash] payload @param [Hash] headers @raise [Sacred::Errors::Error] if an error is encountered with sacred @return [Hash]
# File lib/endorser/client.rb, line 29 def post(url, payload={}, headers={}) execute({ method: :post, url: url, payload: payload, headers: headers }) end
put(url, payload={}, headers={})
click to toggle source
@param [String] url @param [Hash] payload @param [Hash] headers @raise [Sacred::Errors::Error] if an error is encountered with sacred @return [Hash]
# File lib/endorser/client.rb, line 43 def put(url, payload={}, headers={}) execute({ method: :put, url: url, payload: payload, headers: headers }) end
signable_headers(headers)
click to toggle source
# File lib/endorser/client.rb, line 143 def signable_headers(headers) headers.select { |k,v| k.downcase =~ /^zk-/ }.sort_by { |k,v| k.to_s.downcase } end
signed_request(args={})
click to toggle source
# File lib/endorser/client.rb, line 75 def signed_request(args={}) if args[:headers].nil? args[:headers] = Hash.new end headers = args[:headers] headers[:raw_response] = true headers[:content_type] = "application/json;q=0.1;version=1, */*;q=0.0;version=1" headers['ZK-Date'] = Time.now.httpdate headers['ZK-Nonce'] = SecureRandom.uuid receipt = build_receipt(args) signable_headers(headers).each do |k,v| receipt.headers[k] = v end Endorser.logger.debug "[ENDORSER] arguments=#{args.inspect} receipt=#{receipt.to_s}" digest = OpenSSL::Digest::Digest.new('sha1') validation = OpenSSL::HMAC.hexdigest(digest, secret, receipt.to_s) headers["Authorization"] = "#{realm} #{key}:#{validation}" args end
uri_path_and_query(url)
click to toggle source
# File lib/endorser/client.rb, line 119 def uri_path_and_query(url) uri = URI(url) if uri.query.nil? uri.path else [uri.path, '?', uri.query].join end end