Class: Endorser::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/endorser/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/endorser/client.rb', line 4

def key
  @key
end

#realmObject (readonly)

Returns the value of attribute realm.



4
5
6
# File 'lib/endorser/client.rb', line 4

def realm
  @realm
end

#secretObject (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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/endorser/client.rb', line 101

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

Parameters:

  • url (String)
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)

Raises:

  • (Sacred::Errors::Error)

    if an error is encountered with sacred



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

Parameters:

  • args (Hash) (defaults to: {})

Returns:

  • (Hash)

Raises:

  • (Sacred::Errors::Error)

    when the request fails



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

Parameters:

  • url (String)
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)

Raises:

  • (Sacred::Errors::Error)

    if an error is encountered with sacred



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



126
127
128
129
130
131
132
133
134
135
# File 'lib/endorser/client.rb', line 126

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

Returns:

  • (Boolean)


137
138
139
# File 'lib/endorser/client.rb', line 137

def include_port?(uri)
  (uri.scheme != 'http' || uri.port != 80) && (uri.scheme != 'https' || uri.port != 443)
end

#post(url, payload = {}, headers = {}) ⇒ Hash

Parameters:

  • url (String)
  • payload (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)

Raises:

  • (Sacred::Errors::Error)

    if an error is encountered with sacred



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

Parameters:

  • url (String)
  • payload (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)

Raises:

  • (Sacred::Errors::Error)

    if an error is encountered with sacred



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



141
142
143
144
145
146
147
# File 'lib/endorser/client.rb', line 141

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
# 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

  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



117
118
119
120
121
122
123
124
# File 'lib/endorser/client.rb', line 117

def uri_path_and_query(url)
  uri = URI(url)
  if uri.query.nil?
    uri.path
  else
    [uri.path, '?', uri.query].join
  end
end