Class: Endorser::Client
- Inherits:
-
Object
- Object
- Endorser::Client
- Defined in:
- lib/endorser/client.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#realm ⇒ Object
readonly
Returns the value of attribute realm.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #build_receipt(args = {}) ⇒ Object
- #delete(url, headers = {}) ⇒ Hash
-
#execute(args = {}) ⇒ Hash
Wrapper around RestClient::Request.execute method.
- #get(url, headers = {}) ⇒ Hash
- #host(url) ⇒ Object
- #include_port?(uri) ⇒ Boolean
-
#initialize(key, secret, realm) ⇒ Client
constructor
A new instance of Client.
- #post(url, payload = {}, headers = {}) ⇒ Hash
- #put(url, payload = {}, headers = {}) ⇒ Hash
- #signable_headers(headers) ⇒ Object
- #signed_request(args = {}) ⇒ Object
- #uri_path_and_query(url) ⇒ Object
Constructor Details
#initialize(key, secret, realm) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 |
# File 'lib/endorser/client.rb', line 6 def initialize(key, secret, realm) @key = key @secret = secret @realm = realm end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'lib/endorser/client.rb', line 4 def key @key end |
#realm ⇒ Object (readonly)
Returns the value of attribute realm.
4 5 6 |
# File 'lib/endorser/client.rb', line 4 def realm @realm end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
4 5 6 |
# File 'lib/endorser/client.rb', line 4 def secret @secret end |
Instance Method Details
#build_receipt(args = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# 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 = {}) ⇒ Hash
56 57 58 59 60 61 62 |
# File 'lib/endorser/client.rb', line 56 def delete(url, headers={}) execute({ method: :delete, url: url, headers: headers }) end |
#execute(args = {}) ⇒ Hash
Wrapper around RestClient::Request.execute method
69 70 71 72 73 |
# 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 = {}) ⇒ Hash
16 17 18 19 20 21 22 |
# File 'lib/endorser/client.rb', line 16 def get(url, headers={}) execute({ method: :get, url: url, headers: headers }) end |
#host(url) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# 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) ⇒ Boolean
139 140 141 |
# 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 = {}) ⇒ Hash
29 30 31 32 33 34 35 36 |
# 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 = {}) ⇒ Hash
43 44 45 46 47 48 49 50 |
# 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) ⇒ Object
143 144 145 146 147 148 149 |
# 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 = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
119 120 121 122 123 124 125 126 |
# 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 |