Class: Harborapp::Api
- Inherits:
-
Object
- Object
- Harborapp::Api
- Defined in:
- lib/harborapp/api.rb
Constant Summary collapse
- @@api_jack =
nil
Class Method Summary collapse
- .execute_request(params) ⇒ Object
- .execute_xml_request(params) ⇒ Object
- .jack ⇒ Object
- .jack=(double) ⇒ Object
- .request(method, url, params = {}, api_key = :account_api_key) ⇒ Object
- .xml_request(method, url, params = {}) ⇒ Object
Class Method Details
.execute_request(params) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/harborapp/api.rb', line 48 def self.execute_request(params) if Harborapp::Api.jack ApiResponse.new Harborapp::Api.jack.execute(params) else RestClient::Request.new(params).execute do |response, request, result, &block| ApiResponse.new(response) end end end |
.execute_xml_request(params) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/harborapp/api.rb', line 66 def self.execute_xml_request(params) if Harborapp::Api.jack ApiResponse.new Harborapp::Api.jack.execute(params) else RestClient::Request.new(params).execute do |response, request, result, &block| puts response response end end end |
.jack ⇒ Object
10 11 12 |
# File 'lib/harborapp/api.rb', line 10 def self.jack @@api_jack end |
.jack=(double) ⇒ Object
7 8 9 |
# File 'lib/harborapp/api.rb', line 7 def self.jack=(double) @@api_jack = double end |
.request(method, url, params = {}, api_key = :account_api_key) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/harborapp/api.rb', line 14 def self.request method, url, params = {}, api_key = :account_api_key begin basic_username = Harborapp.api_key if params[:basic_username] basic_username = params[:basic_username] params.delete(:basic_username) end if params[:basic_password] basic_password = params[:basic_password] params.delete(:basic_password) end req_params = { :user => basic_username, :password => basic_password, :method => method, :url => "#{Harborapp.api_url}#{url}", :payload => params} self.execute_request(req_params) rescue Errno::ECONNREFUSED => e puts e puts e.backtrace raise ApiError.new(500, {}, {"errors" => [{"code" => 993, "message" => "Unable to connect to API server"}]}) rescue ExpiredApiKey => e raise e rescue InvalidApiKey => e raise e rescue Exception => e # what kind of generic exceptions might we be loking for? puts e puts e.backtrace raise ApiError.new(500, {}, {"errors" => [{"code" => 996, "message" => "Error getting response from API server "+e.inspect}]}) end end |
.xml_request(method, url, params = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/harborapp/api.rb', line 58 def self.xml_request method, url, params = {} basic_username = Harborapp.api_key req_params = { :method => method, :url => url, :payload => params} self.execute_xml_request(req_params) end |