Class: Dgrid::API::ServerRestAdapter

Inherits:
RestAdapter show all
Defined in:
lib/dgrid/api/connection.rb

Constant Summary collapse

@@BASE_URLS =
{ :development => "http://localhost:3000",
:preview => "https://bridge-preview.decisiongrid.com/",
:production => "https://app.decisiongrid.com/" }

Instance Method Summary collapse

Constructor Details

#initializeServerRestAdapter

Returns a new instance of ServerRestAdapter.



59
60
61
62
63
64
65
66
67
# 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

Instance Method Details

#rest_delete(path, params = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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 = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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 = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# 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