Class: RubyGLS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-gls/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, basic_auth, username, password, contact_id) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
# File 'lib/ruby-gls/client.rb', line 9

def initialize(base_url, basic_auth, username, password, contact_id)
  @base_url = base_url
  @basic_auth = basic_auth
  @username = username
  @password = password
  @contact_id = contact_id

  raise ArgumentError('Either basic_auth or username and password not provided') unless basic_auth || (username && password)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



7
8
9
# File 'lib/ruby-gls/client.rb', line 7

def base_url
  @base_url
end

#basic_authObject (readonly)

Returns the value of attribute basic_auth.



7
8
9
# File 'lib/ruby-gls/client.rb', line 7

def basic_auth
  @basic_auth
end

#contact_idObject (readonly)

Returns the value of attribute contact_id.



7
8
9
# File 'lib/ruby-gls/client.rb', line 7

def contact_id
  @contact_id
end

#passwordObject (readonly)

Returns the value of attribute password.



7
8
9
# File 'lib/ruby-gls/client.rb', line 7

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/ruby-gls/client.rb', line 7

def username
  @username
end

Instance Method Details

#action(path, http_method: :post, payload: {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby-gls/client.rb', line 19

def action(path, http_method: :post, payload: {})
  begin 
    response = ::RestClient::Request.execute(
      method: http_method,
      url: full_url(path),
      payload: payload.to_json,
      headers: headers,
      timeout: 5,
      verify_ssl: ::OpenSSL::SSL::VERIFY_NONE
    )
  rescue => e
    return struct_response.new(false, { error: e.message })
  end

  parse(response)
end

#full_url(path) ⇒ Object



51
52
53
# File 'lib/ruby-gls/client.rb', line 51

def full_url(path)
  "#{base_url}#{path}"
end

#get_basic_authObject



36
37
38
39
40
# File 'lib/ruby-gls/client.rb', line 36

def get_basic_auth
  return basic_auth if basic_auth

  Base64.encode64("#{username}:#{password}")
end

#headersObject



42
43
44
45
46
47
48
49
# File 'lib/ruby-gls/client.rb', line 42

def headers
  {
    'User-Agent': "RubyGLS client v#{RubyGLS::VERSION})",
    'Content-Type': 'application/glsVersion1+json',
    'Accept': 'application/glsVersion1+json, application/json',
    'Authorization': "Basic #{get_basic_auth}"
  }
end

#parse(response) ⇒ Object



55
56
57
58
59
60
# File 'lib/ruby-gls/client.rb', line 55

def parse(response)
  success       = (200..308).to_a.include?(response.code.to_i) ? true : false
  hash_response = JSON.parse(response.body)

  struct_response.new(success, hash_response)
end

#struct_responseObject



62
63
64
# File 'lib/ruby-gls/client.rb', line 62

def struct_response
  Struct.new(:success?, :hash_response)
end