class NolijWeb::Connection
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
Attributes
Public Class Methods
# File lib/nolij_web/connection.rb 35 def initialize(config) 36 if config.is_a?(String) 37 configure_with(config) 38 elsif config.is_a?(Hash) 39 configure(config) 40 else 41 raise ConnectionConfigurationError, 'Invalid configuration options supplied.' 42 end 43 end
Public Instance Methods
# File lib/nolij_web/connection.rb 89 def close_connection 90 rest_client_wrapper(:get, "#{@base_url}/j_spring_security_logout", :cookies => @cookies) if @connection 91 @connection = nil 92 @cookies = nil 93 @headers = {} 94 return true 95 end
Configure using hash
# File lib/nolij_web/connection.rb 46 def configure(opts = {}) 47 @config = clean_config_hash(opts) 48 49 raise ConnectionConfigurationError, 'Nolij Web Connection configuration failed.' unless @config 50 51 @base_url = @config[:base_url] || '' 52 @username = @config[:username] || '' 53 @password = @config[:password] || '' 54 @verify_ssl = (@config[:verify_ssl].nil? || @config[:verify_ssl] === true) ? true : !((@config[:verify_ssl] === false) || (@config[:verify_ssl] =~ /^(false|f|no|n|0)$/i)) 55 @connection = nil 56 @cookies = nil 57 @headers = {} 58 end
Configure with yaml
# File lib/nolij_web/connection.rb 61 def configure_with(path_to_yaml_file) 62 raise ConnectionConfigurationError, "Invalid request. #configure_with requires string" unless path_to_yaml_file.is_a?(String) 63 begin 64 @config = YAML::load(ERB.new(IO.read(path_to_yaml_file)).result) 65 rescue Errno::ENOENT 66 raise ConnectionConfigurationError, "YAML configuration file was not found." 67 return 68 rescue Psych::SyntaxError 69 raise ConnectionConfigurationError, "YAML configuration file contains invalid syntax." 70 return 71 end 72 73 configure(@config) 74 end
# File lib/nolij_web/connection.rb 103 def delete(path, headers = {}, &block) 104 execute(headers) do 105 delete_custom_connection(path, @headers, &block) 106 end 107 end
Use this inside an execute block to make mulitiple calls in the same request
# File lib/nolij_web/connection.rb 133 def delete_custom_connection(path, headers = {}, &block) 134 block ||= default_response_handler 135 url = URI.join(@base_url, URI.parse(@base_url).path + '/', URI.encode(path.to_s)).to_s 136 rest_client_wrapper(:delete, url, headers, &block) 137 end
# File lib/nolij_web/connection.rb 76 def establish_connection 77 @connection = post_custom_connection("#{@base_url}/j_spring_security_check", {:j_username => @username, :j_password => @password}) { |response, request, result, &block| 78 if [301, 302, 307].include? response.code 79 response 80 else 81 response.return!(request, result, &block) 82 end 83 } 84 @cookies = @connection.cookies if @connection 85 @headers = {:cookies => @cookies} 86 return true if @connection 87 end
# File lib/nolij_web/connection.rb 115 def execute(headers = {}, &block) 116 establish_connection 117 if @connection 118 merge_headers(headers) 119 yield(block) 120 end 121 ensure 122 close_connection 123 end
# File lib/nolij_web/connection.rb 97 def get(path, headers = {}, &block) 98 execute(headers) do 99 get_custom_connection(path, @headers, &block) 100 end 101 end
Use this inside an execute block to make mulitiple calls in the same request
# File lib/nolij_web/connection.rb 126 def get_custom_connection(path, headers = {}, &block) 127 block ||= default_response_handler 128 url = URI.join(@base_url, URI.parse(@base_url).path + '/', URI.encode(path.to_s)).to_s 129 rest_client_wrapper(:get, url, headers, &block) 130 end
# File lib/nolij_web/connection.rb 109 def post(path, payload, headers = {}, &block) 110 execute(headers) do 111 post_custom_connection(path, payload, @headers, &block) 112 end 113 end
Use this inside an execute block to make mulitiple calls in the same request
# File lib/nolij_web/connection.rb 140 def post_custom_connection(path, payload, headers = {}, &block) 141 block ||= default_response_handler 142 url = URI.join(@base_url, URI.parse(@base_url).path + '/', URI.encode(path.to_s)).to_s 143 RestClient::Request.execute(method: :post, url: url, payload: payload, headers: headers, verify_ssl: @verify_ssl, &block) 144 end
RestClient.get/put/delete offer no way to pass additional arguments, so a wrapper is recommended: github.com/rest-client/rest-client/issues/297
# File lib/nolij_web/connection.rb 149 def rest_client_wrapper(method, url, headers={}, &block) 150 RestClient::Request.execute(method: method, url: url, headers: headers, verify_ssl: @verify_ssl, &block) 151 end
Private Instance Methods
# File lib/nolij_web/connection.rb 155 def clean_config_hash(config) 156 clean_config = {} 157 config = config.each {|k,v| config[k.to_sym] = v} 158 @@valid_config_keys.each{|k| clean_config[k] = config[k]} 159 160 return clean_config 161 end
# File lib/nolij_web/connection.rb 171 def default_response_handler 172 @default_response_handler ||= lambda{ |response, request, result, &block| 173 case response.code 174 when 200 175 #Success! 176 return response 177 when 302 178 raise AuthenticationError, 'User is not logged in.' 179 when 401 180 raise AuthenticationError, 'Request requires authentication' 181 else 182 # Some other error. Let it bubble up. 183 response.return!(request, result, &block) 184 end 185 } 186 end
# File lib/nolij_web/connection.rb 163 def merge_headers(headers = {}) 164 instance_cookies = @headers.delete(:cookies) || {} 165 local_cookies = headers.delete(:cookies) || {} 166 @headers = @headers.merge(headers) 167 @headers[:cookies] = instance_cookies.merge(local_cookies) 168 return @headers 169 end