Module: GridRest::GridRestExtensions
- Includes:
- ActiveSupport::Benchmarkable
- Defined in:
- lib/grid_rest.rb
Instance Method Summary collapse
- #add_parameters_to_uri(uri, params) ⇒ Object
-
#additional_grid_rest_parameters ⇒ Object
Getter of additional parameters.
-
#current_namespace ⇒ Object
This will get the current namespace or nil during grid_rest_request processing.
-
#default_grid_rest_parameters(params, request_types = :global) ⇒ Object
(also: #set_default_grid_rest_parameters)
Global setter valid for all request/namespaces.
- #generate_url(url, rparams = {}) ⇒ Object
- #get_accept_header(f) ⇒ Object
-
#get_additional_grid_rest_parameters ⇒ Object
Return an array of additional parameters.
-
#get_request_specific_additional_grid_rest_parameters(request = :global) ⇒ Object
Get all the global parameters.
-
#grid_rest?(rparams = {}) ⇒ Boolean
This one is on the deprication list.
- #grid_rest_delete(url, rparams = {}) ⇒ Object
- #grid_rest_get(url, rparams = {}) ⇒ Object
-
#grid_rest_log(method, url, rparams = {}, emsg = "") ⇒ Object
Wrapper for grid_rest_log_message to write a log message in a consitant manner given the request parameters.
-
#grid_rest_log_message(msg) ⇒ Object
Write msg to the log file.
- #grid_rest_post(url, rparams = {}) ⇒ Object
- #grid_rest_put(url, rparams = {}) ⇒ Object
- #grid_rest_request(method, relative_url, rparams = {}) ⇒ Object
- #logger ⇒ Object
- #set_namespaced_default_grid_rest_parameters(ns, params, request_types = :global) ⇒ Object
Instance Method Details
#add_parameters_to_uri(uri, params) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/grid_rest.rb', line 244 def add_parameters_to_uri(uri, params) uri << '?' unless uri.include?('?') uri << '&' unless ['?', '&'].include?(uri.last) uri_params = params.reject{|k, v| RESERVED_REQUEST_PARAMETERS.include?(k.to_s)} uri << uri_params.map{|k, v| URI.encode("#{k}=#{v}")}.join('&') uri_params.each{|k, v| params.delete(k)} # Remove added get parameters from the header params uri end |
#additional_grid_rest_parameters ⇒ Object
Getter of additional parameters. Contains defaults and namespace specific versions
136 137 138 |
# File 'lib/grid_rest.rb', line 136 def additional_grid_rest_parameters GridRest.additional_parameters end |
#current_namespace ⇒ Object
This will get the current namespace or nil during grid_rest_request processing
100 101 102 |
# File 'lib/grid_rest.rb', line 100 def current_namespace @current_namespace end |
#default_grid_rest_parameters(params, request_types = :global) ⇒ Object Also known as: set_default_grid_rest_parameters
Global setter valid for all request/namespaces
105 106 107 |
# File 'lib/grid_rest.rb', line 105 def default_grid_rest_parameters(params, request_types = :global) set_namespaced_default_grid_rest_parameters(:default, params, request_types) end |
#generate_url(url, rparams = {}) ⇒ Object
253 254 255 256 257 258 259 260 261 |
# File 'lib/grid_rest.rb', line 253 def generate_url(url, rparams = {}) host = GridRest.grid_config.namespaces[rparams[:grid_rest_namespace]].try('[]', 'host') || GridRest.grid_config['host'] port = GridRest.grid_config.namespaces[rparams[:grid_rest_namespace]].try('[]', 'port') || GridRest.grid_config['port'] || 80 url_prefix = GridRest.grid_config.namespaces[rparams[:grid_rest_namespace]].try('[]', 'url_prefix') || GridRest.grid_config['url_prefix'] || '' raise "No host specified for GridRest" unless host gurl = File.join( "#{host}:#{port}", url_prefix, URI.encode(url) ) gurl = "http://#{gurl}" unless gurl =~ /^http/ gurl end |
#get_accept_header(f) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/grid_rest.rb', line 277 def get_accept_header(f) case f when :json then :json #'application/json' when :xml then :xml #'application/xml' else :json #'application/json' end end |
#get_additional_grid_rest_parameters ⇒ Object
Return an array of additional parameters. This request is namespace aware It will return an array given the additional default parameters for:
ALL, GET, POST, PUT and DELETE
requests in that order
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/grid_rest.rb', line 114 def get_additional_grid_rest_parameters globals = get_request_specific_additional_grid_rest_parameters(:global) return [ globals, globals.merge(get_request_specific_additional_grid_rest_parameters(:get)), globals.merge(get_request_specific_additional_grid_rest_parameters(:post)), globals.merge(get_request_specific_additional_grid_rest_parameters(:put)), globals.merge(get_request_specific_additional_grid_rest_parameters(:delete)) ] end |
#get_request_specific_additional_grid_rest_parameters(request = :global) ⇒ Object
Get all the global parameters. The defaults, extended/overwritten by the namespace specific ones
127 128 129 130 131 132 133 |
# File 'lib/grid_rest.rb', line 127 def get_request_specific_additional_grid_rest_parameters(request = :global) if current_namespace additional_grid_rest_parameters[:default][request].merge(additional_grid_rest_parameters[current_namespace.to_sym][request] || {}) else additional_grid_rest_parameters[:default][request] end end |
#grid_rest?(rparams = {}) ⇒ Boolean
This one is on the deprication list
92 93 94 95 96 97 |
# File 'lib/grid_rest.rb', line 92 def grid_rest?(rparams = {}) ns = rparams[:grid_rest_namespace].present? ? "grid_rest_#{rparams[:grid_rest_namespace]}" : 'grid_rest' raise "APP_CONFIG not available, make sure that config/config.yml and config/initializers/load_config.rb are available" unless defined?(APP_CONFIG) raise "Namespace #{ns} not available in config/config.yml" unless APP_CONFIG[ns] @grid_rest_active ||= Net::HTTP.new(APP_CONFIG[ns]['host'], APP_CONFIG[ns]['port']).start rescue nil end |
#grid_rest_delete(url, rparams = {}) ⇒ Object
271 272 273 |
# File 'lib/grid_rest.rb', line 271 def grid_rest_delete(url, rparams = {}) grid_rest_request(:delete, url, rparams) end |
#grid_rest_get(url, rparams = {}) ⇒ Object
263 264 265 |
# File 'lib/grid_rest.rb', line 263 def grid_rest_get(url, rparams = {}) return grid_rest_request(:get, url, rparams) end |
#grid_rest_log(method, url, rparams = {}, emsg = "") ⇒ Object
Wrapper for grid_rest_log_message to write a log message in a consitant manner given the request parameters
149 150 151 152 153 154 155 156 |
# File 'lib/grid_rest.rb', line 149 def grid_rest_log(method, url, rparams = {}, emsg = "") if current_namespace return unless GridRest.grid_config.namespaces[current_namespace]['logging'] else return unless GridRest.grid_config['logging'] end (rparams.any? ? "#{Time.now.to_s(:db)} #{method.to_s.upcase} #{url} with #{rparams.inspect} #{emsg}" : "#{Time.now.to_s(:db)} #{method.to_s.upcase} #{url} #{emsg}") end |
#grid_rest_log_message(msg) ⇒ Object
Write msg to the log file. Should only be called from grid_rest_log unless you know what you are doing
159 160 161 162 163 |
# File 'lib/grid_rest.rb', line 159 def (msg) GridRest.log_file ||= File.open(File.join(Rails.root, 'log', "#{Rails.env}_grid_rest.log"), 'a+') GridRest.log_file.puts msg GridRest.log_file.flush unless Rails.env == 'production' end |
#grid_rest_post(url, rparams = {}) ⇒ Object
274 275 276 |
# File 'lib/grid_rest.rb', line 274 def grid_rest_post(url, rparams={}) return grid_rest_request(:post, url, rparams) end |
#grid_rest_put(url, rparams = {}) ⇒ Object
267 268 269 |
# File 'lib/grid_rest.rb', line 267 def grid_rest_put(url, rparams = {}) grid_rest_request(:put, url, rparams) end |
#grid_rest_request(method, relative_url, rparams = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/grid_rest.rb', line 165 def grid_rest_request(method, relative_url, rparams = {}) #return DummyResponse.new # test return rest_url = generate_url(relative_url, rparams) #return Error.new('unavailable', :url => rest_url) unless grid_rest?(rparams) # Specify defaults per method for format format = rparams.delete(:format) || {:get => :json, :post => :json, :put => :json}[method] accept = get_accept_header(format) @current_namespace = rparams.delete(:grid_rest_namespace) # Remove this setting from request parameters additional_get_parameters, additional_post_parameters, additional_put_parameters, additional_delete_parameters = get_additional_grid_rest_parameters begin r = benchmark "Fetching #{method.to_s.upcase} #{relative_url} #{rparams.inspect}", :level => :debug do case method when :get then RestClient.get rest_url, :params => rparams.update(additional_get_parameters), :accept => accept when :post then if rparams[:json_data] rparams[:json_data] = rparams[:json_data].merge(additional_post_parameters).to_json if rparams[:json_data].is_a?(Hash) rparams[:json_data] = rparams[:json_data].to_json if rparams[:json_data].is_a?(Array) RestClient.post rest_url, rparams[:json_data], :content_type => :json, :accept => :json elsif rparams[:xml_data] rparams[:xml_data] = rparams[:xml_data].merge(additional_post_parameters).to_xml if rparams[:xml_data].is_a?(Hash) rparams[:xml_data] = rparams[:xml_data].to_xml if rparams[:xml_data].is_a?(Array) RestClient.post rest_url, rparams[:xml_data], :content_type => :xml, :accept => :xml elsif rparams[:binary] RestClient.post rest_url, rparams[:binary], :content_type => 'binary/octet-stream' else rparams[:headers] ||= {} rparams[:headers][:accept] = accept rparams[:multipart] = true RestClient.post rest_url, rparams.update(additional_post_parameters) end when :put then if rparams[:json_data] rparams[:json_data] = rparams[:json_data].merge(additional_put_parameters).to_json if rparams[:json_data].is_a?(Hash) rparams[:json_data] = rparams[:json_data].to_json if rparams[:json_data].is_a?(Array) RestClient.put rest_url, rparams[:json_data], :content_type => :json, :accept => :json elsif rparams[:xml_data] rparams[:xml_data] = rparams[:xml_data].merge(additional_put_parameters).to_xml if rparams[:xml_data].is_a?(Hash) rparams[:xml_data] = rparams[:xml_data].to_xml if rparams[:xml_data].is_a?(Array) RestClient.put rest_url, rparams[:xml_data], :content_type => :xml, :accept => :xml elsif rparams[:binary] RestClient.put rest_url, rparams[:binary], :content_type => 'binary/octet-stream' else rparams[:headers] ||= {} rparams[:headers][:accept] = accept rparams[:multipart] = true RestClient.put rest_url, rparams.update(additional_put_parameters) end when :delete then rparams[:headers] ||= {} rparams[:headers][:accept] = accept new_uri = add_parameters_to_uri(rest_url, rparams.update(additional_delete_parameters)) RestClient.delete(new_uri, rparams) else raise "No proper method (#{method}) for a grid_rest_request call" end end grid_rest_log method, rest_url, rparams, "response code: #{r.code}" if format == :json #r = benchmark("decoding response JSON", :level => :debug ){ ActiveSupport::JSON.decode(r.body) rescue r } # Slow r = benchmark("decoding response JSON", :level => :debug ){ JSON.parse(r.body) rescue r } end # Singleton class extensions r = "" if r == false # RestClient problem giving false on empty 204 def r.valid? true end rescue RestClient::ResourceNotFound => e r = Error.new(e, :url => rest_url, :method => method) grid_rest_log method, rest_url, rparams, "resource not found response" rescue Errno::ECONNREFUSED => e r = Error.new(e, :url => rest_url, :method => method) grid_rest_log method, rest_url, rparams, "connection refused response" rescue => e r = Error.new e, :url => rest_url, :method => method grid_rest_log method, rest_url, rparams, "error in request" end r end |
#logger ⇒ Object
87 88 89 |
# File 'lib/grid_rest.rb', line 87 def logger Rails.logger end |
#set_namespaced_default_grid_rest_parameters(ns, params, request_types = :global) ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/grid_rest.rb', line 140 def set_namespaced_default_grid_rest_parameters(ns, params, request_types = :global) additional_grid_rest_parameters[ns] ||= {} for request_type in Array.wrap(request_types).map(&:to_sym) additional_grid_rest_parameters[ns][request_type] ||= {} additional_grid_rest_parameters[ns][request_type].update(params) end end |