lib/rest_connection.rb in rest_connection-0.0.13 vs lib/rest_connection.rb in rest_connection-0.0.14
- old
+ new
@@ -27,11 +27,11 @@
# settings.merge! {
# :common_headers => { "X_CUSTOM_HEADER" => "BLAH" },
# :api_url =>
# :user =>
# :pass =>
- attr_accessor :settings
+ attr_accessor :settings, :cookie
# RestConnection api settings configuration file:
# Settings are loaded from a yaml configuration file in users home directory.
# Copy the example config from the gemhome/config/rest_api_config.yaml.sample to ~/.rest_connection/rest_api_config.yaml
# OR to /etc/rest_connection/rest_api_config.yaml
@@ -46,10 +46,12 @@
else
logger("\nWARNING: you must setup config file rest_api_config.yaml in #{config_yaml} or #{etc_config}")
logger("WARNING: see GEM_HOME/rest_connection/config/rest_api_config.yaml for example config")
@settings = {}
end
+ @settings[:extension] = ".js"
+ @settings[:api_href] = @settings[:api_url] unless @settings[:api_href]
end
# Main HTTP connection loop. Common settings are set here, then we yield(BASE_URI, OPTIONAL_HEADERS) to other methods for each type of HTTP request: GET, PUT, POST, DELETE
#
# The block must return a Net::HTTP Request. You have a chance to taylor the request inside the block that you pass by modifying the url and headers.
@@ -58,20 +60,23 @@
# headers.merge! {:my_header => "blah"}
# Net::HTTP::Get.new(base_uri, headers)
# end
#
def rest_connect(&block)
- uri = URI.parse(@settings[:api_url])
+ uri = URI.parse(@settings[:api_href])
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
headers = @settings[:common_headers]
+ headers.merge!("Cookie" => @cookie) if @cookie
http.start do |http|
req = yield(uri, headers)
- req.basic_auth(@settings[:user], @settings[:pass]) if @settings[:user]
+ unless @cookie
+ req.basic_auth(@settings[:user], @settings[:pass]) if @settings[:user]
+ end
logger("#{req.method}: #{req.path}")
logger("\trequest body: #{req.body}") if req.body
response, body = http.request(req)
handle_response(response)
end
@@ -82,11 +87,11 @@
# href = "servers" this will be concat'd on to the api_url from the settings
# additional_parameters = Hash or String of parameters to pass to HTTP::Get
def get(href, additional_parameters = "")
rest_connect do |base_uri,headers|
href = "#{base_uri}/#{href}" unless begins_with_slash(href)
- new_path = URI.escape(href + '.js?' + requestify(additional_parameters))
+ new_path = URI.escape(href + @settings[:extension] + "?") + requestify(additional_parameters)
Net::HTTP::Get.new(new_path, headers)
end
end
# connection.post(server_url + "/start")
@@ -144,10 +149,10 @@
#
# decoding and post processing goes here. This is where you may need some customization if you want to handle the response differently (or not at all!). Luckily it's easy to modify based on this handler.
def handle_response(res)
if res.code.to_i == 201
return res['Location']
- elsif [200,203,204].detect { |d| d == res.code.to_i }
+ elsif [200,203,204,302].detect { |d| d == res.code.to_i }
if res.body
begin
return JSON.load(res.body)
rescue => e
return res