cassieq-client 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d4483c9c6138064c314afabc79c0977d1bed821
4
- data.tar.gz: c7cb4237be5e6b38f596ce012af2e6ad90c3da95
3
+ metadata.gz: ccb1bf9d217348ce9f96c072173cd3a5a403a8c9
4
+ data.tar.gz: a69c0ccc5d7b1dc49a873e5ce132b338580457ba
5
5
  SHA512:
6
- metadata.gz: 9a99389780258976d1707a6e8d7554a6c3ee746dcbb2ffa4f456fe0e890d5bece8b9af46378eac5ae45199a5822a9e204852429d62eb56f3280f2fcb9fb2047b
7
- data.tar.gz: d5b9b27e3b6fa728f25c5fa92f45ac73e186ce36de830529478891ba435bf8eeeae1489f44878d2afb9715169df9eebc95ae4fc3faf8df3a382afa802cba5e3b
6
+ metadata.gz: a9c61c987b6119314622da59e6f12e87ef75d534aff680b2aa2d62f5e5dc843418a0ece534de89e2ee570e4f559383bfded633de6f66ce1d51f3845eeeefb57b
7
+ data.tar.gz: 91b118383123db8969db63b0dbfea77287ecb047338a6fd8dcf4fdad0880bc7434974876fedbf2c66fbf56ac1541a919fa63dd1ab88df837e11b6e1220944610
@@ -3,23 +3,33 @@ require "openssl"
3
3
  require "time"
4
4
 
5
5
  module Cassieq
6
- module Authentication
7
- private
6
+ class Authentication
7
+ attr_reader :key, :account
8
+
9
+ def self.generate_auth_headers(key, account, method, path)
10
+ new(key, account).auth_headers(method, path)
11
+ end
8
12
 
9
- def generate_auth_headers(request_method, request_path)
10
- request_time = formated_time_now
11
- auth_signature = generate_signature_from_key(request_method.to_s.upcase, request_path, request_time)
13
+ def initialize(key, account)
14
+ @key = key
15
+ @account = account
16
+ end
17
+
18
+ def auth_headers(method, path, request_time = formated_time_now)
19
+ auth_signature = signature_from_key(method, path, request_time)
12
20
 
13
21
  { "X-Cassieq-Request-Time" => request_time, "Authorization" => "Signed #{auth_signature}" }
14
22
  end
15
23
 
16
- def generate_signature_from_key(request_method, request_path, request_time)
24
+ def signature_from_key(method, path, request_time = formated_time_now)
17
25
  key_bytes = Base64.urlsafe_decode64("#{key}==")
18
- string_to_sign = [account, request_method, request_path, request_time].join("\n")
26
+ string_to_sign = [account, method.to_s.upcase, path, request_time].join("\n")
19
27
  hmac = OpenSSL::HMAC.digest("sha256", key_bytes, string_to_sign)
20
28
  Base64.urlsafe_encode64(hmac).gsub(/=+$/, "")
21
29
  end
22
30
 
31
+ private
32
+
23
33
  def formated_time_now
24
34
  Time.now.utc.iso8601
25
35
  end
@@ -12,16 +12,15 @@ module Cassieq
12
12
  include Cassieq::Client::Queues
13
13
  include Cassieq::Client::Messages
14
14
  include Cassieq::Client::Statistics
15
- include Cassieq::Authentication
16
15
  include Cassieq::Utils
17
16
 
18
17
  attr_accessor :host, :account, :port, :key, :provided_params
19
18
 
20
19
  def initialize(params = {})
21
- @host = params.fetch(:host, nil)
22
- @key = params.fetch(:key, nil)
23
- @provided_params = params.fetch(:provided_params, nil)
24
- @account = params.fetch(:account, nil)
20
+ @host = params[:host]
21
+ @key = params[:key]
22
+ @provided_params = params[:provided_params]
23
+ @account = params[:account]
25
24
  @port = params.fetch(:port, 8080)
26
25
  yield(self) if block_given?
27
26
  end
@@ -43,21 +42,21 @@ module Cassieq
43
42
  "/api/v1/accounts/#{account}"
44
43
  end
45
44
 
46
- def request_path(path)
47
- "#{path_prefix}/#{path}"
48
- end
49
-
50
- def request(method, path, body = nil, params = nil)
45
+ def request(method, path, body = nil, params = {})
51
46
  handle_response do
52
- path = request_path(path)
53
- auth_headers = generate_auth_headers(method, path) unless key.nil?
54
- connection.run_request(method, path, body, auth_headers) do |req|
55
- req.params.merge!(params) unless params.nil?
56
- req.headers["Content-Type"] = "application/json" unless body.nil?
47
+ request_path = "#{path_prefix}/#{path}"
48
+ connection.run_request(method, request_path, body, nil) do |req|
49
+ req.params.merge!(params)
50
+ req.headers.merge!(generate_auth_headers(method, request_path)) unless key.nil?
51
+ req.headers.merge!("Content-Type" => "application/json") unless body.nil?
57
52
  end
58
53
  end
59
54
  end
60
55
 
56
+ def generate_auth_headers(method, path)
57
+ Cassieq::Authentication.generate_auth_headers(key, account, method, path)
58
+ end
59
+
61
60
  def handle_response(&request_block)
62
61
  response = request_block.call
63
62
  Cassieq::Error.from_status_and_body(response)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassieq-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Conroy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-29 00:00:00.000000000 Z
11
+ date: 2016-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport