Class: Teapi::Sender
- Inherits:
-
Object
- Object
- Teapi::Sender
- Defined in:
- lib/teapi/sender.rb
Constant Summary collapse
- BASE_URL =
'/v1/'
- HEADERS =
{'Accept' => 'application/json'}
Instance Method Summary collapse
-
#initialize(configuration) ⇒ Sender
constructor
A new instance of Sender.
- #request(method, resource, args = {}, date = nil) ⇒ Object
- #sign(url, date, args) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Sender
Returns a new instance of Sender.
14 15 16 |
# File 'lib/teapi/sender.rb', line 14 def initialize(configuration) @configuration = configuration end |
Instance Method Details
#request(method, resource, args = {}, date = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/teapi/sender.rb', line 18 def request(method, resource, args = {}, date = nil) url = BASE_URL + resource.to_s d = date || Time.now.utc.httpdate args[:headers] = (args[:headers] || {}).merge({ 'Date' => d, 'Authorization' => sign(url, d, args), }) scheme = @configuration.secure ? "https" : "http" res = HTTParty.send(method, "#{scheme}://#{@configuration.host}#{url}", args) if res.code == 401 && res.parsed_response.include?('date') && date.nil? return request(method, resource, args, res.parsed_response['date']) end res end |
#sign(url, date, args) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/teapi/sender.rb', line 33 def sign(url, date, args) data = url + date data += args[:body] if args.include?(:body) signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @configuration.sync_secret, data) "HMAC-SHA256 Credential=#{@configuration.sync_key},Signature=#{signature}" end |