Class: Code42::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/code42/connection.rb', line 9

def initialize(options = {})
  self.host         = options[:host]
  self.port         = options[:port]
  self.scheme       = options[:scheme]
  self.path_prefix  = options[:path_prefix]
  self.username     = options[:username]
  self.password     = options[:password]
  self.token        = options[:token] if options[:token]
  self.verify_https = !options[:verify_https].nil? ? options[:verify_https] : true

  adapter.host        = host
  adapter.port        = port
  adapter.scheme      = scheme
  adapter.path_prefix = path_prefix
  adapter.ssl[:verify] = verify_https
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



119
120
121
122
# File 'lib/code42/connection.rb', line 119

def method_missing(method_name, *args, &block)
  return super unless adapter.respond_to?(method_name)
  adapter.send(method_name, *args, &block)
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



8
9
10
# File 'lib/code42/connection.rb', line 8

def adapter
  @adapter
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/code42/connection.rb', line 8

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/code42/connection.rb', line 8

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/code42/connection.rb', line 8

def password
  @password
end

#path_prefixObject

Returns the value of attribute path_prefix.



8
9
10
# File 'lib/code42/connection.rb', line 8

def path_prefix
  @path_prefix
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/code42/connection.rb', line 8

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



8
9
10
# File 'lib/code42/connection.rb', line 8

def scheme
  @scheme
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/code42/connection.rb', line 8

def token
  @token
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/code42/connection.rb', line 8

def username
  @username
end

#verify_httpsObject

Returns the value of attribute verify_https.



8
9
10
# File 'lib/code42/connection.rb', line 8

def verify_https
  @verify_https
end

Instance Method Details

#delete(path, data) ⇒ Object



83
84
85
# File 'lib/code42/connection.rb', line 83

def delete(path, data)
  adapter.delete(path, data)
end

#get(path, data) ⇒ Object



69
70
71
# File 'lib/code42/connection.rb', line 69

def get(path, data)
  adapter.get(path, data)
end

#has_valid_credentials?Boolean



38
39
40
# File 'lib/code42/connection.rb', line 38

def has_valid_credentials?
  username && password
end

#make_request(method, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/code42/connection.rb', line 59

def make_request(method, *args)
  begin
    response = self.send(method, *args)
  rescue Faraday::Error::ConnectionFailed
    raise Code42::Error::ConnectionFailed
  end
  check_for_errors(response)
  response.body
end

#post(path, data) ⇒ Object



78
79
80
81
# File 'lib/code42/connection.rb', line 78

def post(path, data)
  adapter.headers['Content-Type'] = 'application/json'
  adapter.post path, data.to_json
end

#put(path, data) ⇒ Object



73
74
75
76
# File 'lib/code42/connection.rb', line 73

def put(path, data)
  adapter.headers['Content-Type'] = 'application/json'
  adapter.put path, data.to_json
end

#respond_to?(method_name, include_private = false) ⇒ Boolean



87
88
89
# File 'lib/code42/connection.rb', line 87

def respond_to?(method_name, include_private = false)
  adapter.respond_to?(method_name, include_private) || super
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean



91
92
93
# File 'lib/code42/connection.rb', line 91

def respond_to_missing?(method_name, include_private = false)
  adapter.respond_to?(method_name, include_private) || super
end