Class: Cubits::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cubits/connection.rb

Constant Summary collapse

CONTENT_TYPE =
'application/vnd.api+json'

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Connection

Creates a new Connection object

Parameters:

  • params (Hash)
  • params (:key)
    String
  • params (:secret)
    String


17
18
19
20
21
22
23
# File 'lib/cubits/connection.rb', line 17

def initialize(params)
  fail ArgumentError, 'String is expected as :key' unless params[:key].is_a?(String)
  fail ArgumentError, 'String is expected as :secret' unless params[:secret].is_a?(String)
  @key = params[:key]
  @secret = params[:secret]
  @params = params.dup
end

Instance Method Details

#get(path, data = {}) ⇒ Object

Executes a GET request



27
28
29
30
31
# File 'lib/cubits/connection.rb', line 27

def get(path, data = {})
  fail ArgumentError, 'Hash is expected as request data' unless data.is_a?(Hash)
  encoded_data = URI.encode_www_form(data)
  request(:get, path, encoded_data)
end

#post(path, data = {}) ⇒ Object

Executes a POST request



35
36
37
38
39
# File 'lib/cubits/connection.rb', line 35

def post(path, data = {})
  fail ArgumentError, 'Hash is expected as request data' unless data.is_a?(Hash)
  encoded_data = data.to_json
  request(:post, path, encoded_data)
end

#sign_message(msg) ⇒ String

Signs the message with preconfigured secret

Parameters:

  • msg (String)

Returns:

  • (String)

    Calculated signature



47
48
49
# File 'lib/cubits/connection.rb', line 47

def sign_message(msg)
  OpenSSL::HMAC.hexdigest('sha512', @secret, msg)
end