Class: NolijWeb::Connection

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

Overview

Nolijweb::Connection is the class that handles actual Nolijweb api sessions and requests.

Basic Usage

#get, #post, #delete will automatically wrap your request in open and close connection. Arguments are passed through to RestClient, so see RestClient docs for more options.

conn = NolijWeb::Connection.new(config_hash_or_yaml_path)
conn.get('/print', :query_params => { :document_id => 1})

Manual Usage

You can manually establish a connection and use the _custom_connection methods to execute multiple requests in series.

Be sure to close the connection when you are finished.

conn = NolijWeb::Connection.new(config_hash_or_yaml_path)
conn.establish_connection
 # do some stuff using get, post, etc. (_custom_connection methods)
close_connection

Constant Summary collapse

@@valid_config_keys =
[:username, :password, :base_url, :verify_ssl]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



35
36
37
38
39
40
41
42
43
# File 'lib/nolij_web/connection.rb', line 35

def initialize(config)
  if config.is_a?(String)
    configure_with(config)
  elsif config.is_a?(Hash)
    configure(config)
  else
    raise ConnectionConfigurationError, 'Invalid configuration options supplied.'
  end
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



27
28
29
# File 'lib/nolij_web/connection.rb', line 27

def base_url
  @base_url
end

#connectionObject (readonly)

Returns the value of attribute connection.



30
31
32
# File 'lib/nolij_web/connection.rb', line 30

def connection
  @connection
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



29
30
31
# File 'lib/nolij_web/connection.rb', line 29

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



31
32
33
# File 'lib/nolij_web/connection.rb', line 31

def headers
  @headers
end

#usernameObject (readonly)

Returns the value of attribute username.



28
29
30
# File 'lib/nolij_web/connection.rb', line 28

def username
  @username
end

Instance Method Details

#close_connectionObject



89
90
91
92
93
94
95
# File 'lib/nolij_web/connection.rb', line 89

def close_connection
  rest_client_wrapper(:get, "#{@base_url}/j_spring_security_logout", :cookies => @cookies) if @connection
  @connection = nil
  @cookies = nil
  @headers = {}
  return true
end

#configure(opts = {}) ⇒ Object

Configure using hash



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nolij_web/connection.rb', line 46

def configure(opts = {})
  @config = clean_config_hash(opts)

  raise ConnectionConfigurationError, 'Nolij Web Connection configuration failed.' unless @config

  @base_url = @config[:base_url] || ''
  @username = @config[:username] || ''
  @password = @config[:password] || ''
  @verify_ssl = (@config[:verify_ssl].nil? || @config[:verify_ssl] === true) ? true : !((@config[:verify_ssl] === false) || (@config[:verify_ssl] =~ /^(false|f|no|n|0)$/i))
  @connection = nil
  @cookies = nil
  @headers = {}
end

#configure_with(path_to_yaml_file) ⇒ Object

Configure with yaml



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nolij_web/connection.rb', line 61

def configure_with(path_to_yaml_file)
  raise ConnectionConfigurationError, "Invalid request. #configure_with requires string" unless path_to_yaml_file.is_a?(String)
  begin
    @config = YAML::load(ERB.new(IO.read(path_to_yaml_file)).result)
  rescue Errno::ENOENT
    raise ConnectionConfigurationError, "YAML configuration file was not found."
    return
  rescue Psych::SyntaxError
   raise ConnectionConfigurationError, "YAML configuration file contains invalid syntax."
   return
  end

  configure(@config)
end

#delete(path, headers = {}, &block) ⇒ Object



103
104
105
106
107
# File 'lib/nolij_web/connection.rb', line 103

def delete(path, headers = {}, &block)
  execute(headers) do
    delete_custom_connection(path, @headers, &block)
  end
end

#delete_custom_connection(path, headers = {}, &block) ⇒ Object

Use this inside an execute block to make mulitiple calls in the same request



133
134
135
136
137
# File 'lib/nolij_web/connection.rb', line 133

def delete_custom_connection(path, headers = {}, &block)
  block ||= default_response_handler
  url = URI.join(@base_url, URI.parse(@base_url).path + '/', URI.encode(path.to_s)).to_s
  rest_client_wrapper(:delete, url, headers, &block)
end

#establish_connectionObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/nolij_web/connection.rb', line 76

def establish_connection
  @connection = post_custom_connection("#{@base_url}/j_spring_security_check", {:j_username => @username, :j_password => @password}) { |response, request, result, &block|
    if [301, 302, 307].include? response.code
      response
    else
      response.return!(request, result, &block)
    end
  }
  @cookies = @connection.cookies if @connection
  @headers = {:cookies => @cookies}
  return true if @connection
end

#execute(headers = {}, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/nolij_web/connection.rb', line 115

def execute(headers = {}, &block)
  establish_connection
  if @connection
    merge_headers(headers)
    yield(block)
  end
ensure
  close_connection
end

#get(path, headers = {}, &block) ⇒ Object



97
98
99
100
101
# File 'lib/nolij_web/connection.rb', line 97

def get(path, headers = {}, &block)
  execute(headers) do
    get_custom_connection(path, @headers, &block)
  end
end

#get_custom_connection(path, headers = {}, &block) ⇒ Object

Use this inside an execute block to make mulitiple calls in the same request



126
127
128
129
130
# File 'lib/nolij_web/connection.rb', line 126

def get_custom_connection(path, headers = {}, &block)
  block ||= default_response_handler
  url = URI.join(@base_url, URI.parse(@base_url).path + '/', URI.encode(path.to_s)).to_s
  rest_client_wrapper(:get, url, headers, &block)
end

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



109
110
111
112
113
# File 'lib/nolij_web/connection.rb', line 109

def post(path, payload, headers = {}, &block)
  execute(headers) do
    post_custom_connection(path, payload, @headers, &block)
  end
end

#post_custom_connection(path, payload, headers = {}, &block) ⇒ Object

Use this inside an execute block to make mulitiple calls in the same request



140
141
142
143
144
# File 'lib/nolij_web/connection.rb', line 140

def post_custom_connection(path, payload, headers = {}, &block)
  block ||= default_response_handler
  url = URI.join(@base_url, URI.parse(@base_url).path + '/', URI.encode(path.to_s)).to_s
  RestClient::Request.execute(method: :post, url: url, payload: payload, headers: headers, verify_ssl: @verify_ssl, &block)
end

#rest_client_wrapper(method, url, headers = {}, &block) ⇒ Object

RestClient.get/put/delete offer no way to pass additional arguments, so a wrapper is recommended: github.com/rest-client/rest-client/issues/297



149
150
151
# File 'lib/nolij_web/connection.rb', line 149

def rest_client_wrapper(method, url, headers={}, &block)
  RestClient::Request.execute(method: method, url: url, headers: headers, verify_ssl: @verify_ssl, &block)
end