class Dgrid::API::ServerRestAdapter
Attributes
environment[RW]
Public Class Methods
new()
click to toggle source
# File lib/dgrid/api/connection.rb, line 59 def initialize @environment = select_environment # FIXME: Remove this once the server has been made less picky. # HACK # Prior to 12/2013, the server rejected all requests from any # unsupported browser. Forging chrome identity was a hack # workaround for this ill-advised "feature" (bug). @user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36' end
Public Instance Methods
rest_delete(path, params = {})
click to toggle source
# File lib/dgrid/api/connection.rb, line 96 def rest_delete(path, params = {}) url_params_list = [] params.each { |k,v| url_params_list << "#{k}=#{v}" } url_params = url_params_list.join('&') full_url = "#{base_url}/#{path}" full_url += ("?#{url_params}") if url_params.length > 0 debug_puts "about to delete #{full_url}" response = RestClient.delete(full_url, { :content_type => :json, :accept => :json, :user_agent => @user_agent}, &method(:permit_redirects)) debug_puts "delete response was #{response}" process_rest_response(response, expect_json = false) end
rest_get(path, params = {})
click to toggle source
# File lib/dgrid/api/connection.rb, line 81 def rest_get(path, params = {}) url_params_list = [] params.each { |k,v| url_params_list << "#{k}=#{v}" } url_params = url_params_list.join('&') full_url = (path =~ /^https?:\/\//) ? path : "#{base_url}/#{path}" full_url += ("?#{url_params}") if url_params.length > 0 debug_puts "about to get #{full_url}" response = RestClient.get(full_url, { :content_type => :json, :accept => :json, :user_agent => @user_agent}, &method(:permit_redirects)) debug_puts "get response was #{response}" process_rest_response(response) end
rest_post(path, params = {})
click to toggle source
# File lib/dgrid/api/connection.rb, line 69 def rest_post(path, params = {}) full_url = "#{base_url}/#{path}" debug_puts "about to post to #{full_url} with #{params.to_json}" response = ::RestClient.post(full_url, params.to_json, { :content_type => :json, :accept => :json, :user_agent => @user_agent}, &method(:permit_redirects)) debug_puts "post response was #{response}" process_rest_response(response) end
Protected Instance Methods
base_url()
click to toggle source
# File lib/dgrid/api/connection.rb, line 141 def base_url @@BASE_URLS[@environment] end
debug_puts(message)
click to toggle source
# File lib/dgrid/api/connection.rb, line 172 def debug_puts(message) STDERR.puts message end
permit_redirects(response,request,result,&block)
click to toggle source
A block to pass to RestClient transactions that prevents them from freaking out about redirect responses.
# File lib/dgrid/api/connection.rb, line 117 def permit_redirects(response,request,result,&block) redirect_response_codes = [301, 302, 307] if redirect_response_codes.include? response.code return response # do not throw exception just because of redirect else response.return!(request,result,&block) # do the usual thing for all other cases end end
process_rest_response(response, expect_json = true)
click to toggle source
# File lib/dgrid/api/connection.rb, line 145 def process_rest_response(response, expect_json = true) case response.code # TODO enumerate more codes if needed when 400 raise NotFoundError when 401 raise AuthError when 500..599 raise ServerError when 200..299 # various success codes when 300..308 # TODO: should we really be accepting all of these # redirects as success? return Hash.new # avoid json parsing errors below else raise HTTPError end if expect_json json = response.body obj = JSON.parse(json) obj else response.body end end
select_environment()
click to toggle source
# File lib/dgrid/api/connection.rb, line 127 def select_environment env_str = ENV.include?('DGRID_ENV') ? ENV['DGRID_ENV'] : 'development' case env_str.downcase when 'development' return :development when 'preview' return :preview when 'production' return :production else raise "invalid DGRID_ENV: #{env_str}" end end