Class: Flow::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, url = 'https://flow.ribbon.co', version = 1) ⇒ Connection

Returns a new instance of Connection.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flow/connection.rb', line 17

def initialize(api_key, url='https://flow.ribbon.co', version=1)
  raise Flow::Errors::ConnectionError, "API Key must be defined." unless api_key
  raise Flow::Errors::ConnectionError, "URL must be defined." unless url
  raise Flow::Errors::ConnectionError, "Version must be defined." unless version

  @version = version

  @url = (url[-1] == '/' ? url[0..-2] : url).dup.freeze # Remove trailing slash.
  
  if /^([A-Za-z0-9]{20})\.([A-Za-z0-9]{43})$/.match(api_key.to_s)
    @access_key = $1.dup.freeze
    @signing_key = $2.dup.freeze
  else
    raise Flow::Errors::ConnectionError, "Invalid API Key format."
  end

  URI.parse(@url).tap do |uri|
    @scheme = uri.scheme
    @host = uri.host
    @port = uri.port
  end

  # RestClient::Resource.new(
  #   'https://example.com',
  #   :ssl_client_cert  =>  OpenSSL::X509::Certificate.new(File.read("cert.pem")),
  #   :ssl_client_key   =>  OpenSSL::PKey::RSA.new(File.read("key.pem"), "passphrase, if any"),
  #   :ssl_ca_file      =>  "ca_certificate.pem",
  #   :verify_ssl       =>  OpenSSL::SSL::VERIFY_PEER
  # )

  @api = RestClient::Resource.new(
    @url,
    verify_ssl: true,
    headers: {
      "Content-Type" => "application/json"
    }
  ) {|response, request, result| response}
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



10
11
12
# File 'lib/flow/connection.rb', line 10

def access_key
  @access_key
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/flow/connection.rb', line 14

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/flow/connection.rb', line 15

def port
  @port
end

#schemeObject (readonly)

Returns the value of attribute scheme.



13
14
15
# File 'lib/flow/connection.rb', line 13

def scheme
  @scheme
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/flow/connection.rb', line 9

def url
  @url
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/flow/connection.rb', line 11

def version
  @version
end

Instance Method Details

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



56
57
58
59
60
61
# File 'lib/flow/connection.rb', line 56

def get(path, params={})
  path = _build_path(path)
  auth_header = _authorization_header(:get, path, params)
  response = @api[path].get(authorization: auth_header, params: params)
  Flow::Response.new(self, response)
end

#post(path, params = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/flow/connection.rb', line 63

def post(path, params={}, &block)
  if block_given?
    body_hash = block.call
    if body_hash && !body_hash.empty?
      body = body_hash.to_json
    end
  end

  path = _build_path(path)
  auth_header = _authorization_header(:post, path, params, body)
  response = @api[path].post(body, authorization: auth_header, params: params)
  
  Flow::Response.new(self, response)
end