class Holistics::Helpers::HttpRequest
Constants
- API_KEY_HEADER
- DEFAULT_ERROR_MSG
- SERVER_URL
Public Instance Methods
auth_helper()
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 12 def auth_helper @auth_info ||= Helpers::AuthInfo.new end
get(url, msg_if_error = DEFAULT_ERROR_MSG)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 31 def get url, msg_if_error = DEFAULT_ERROR_MSG url = auth_helper.api_url_for url response = simple_get url exit_if_error(msg_if_error, response) JSON.parse response.body end
post_file(url, params, file, content_type, msg_if_error = DEFAULT_ERROR_MSG)
click to toggle source
NOTE use_ssl option is required for connection to staging and production servers, they use HTTPS
see this link for setting it github.com/nicksieger/multipart-post/issues/18#issuecomment-171479987
# File lib/holistics/helpers/http_request.rb, line 64 def post_file url, params, file, content_type, msg_if_error = DEFAULT_ERROR_MSG uri = URI.parse(endpoint_for(url)) upload_file = UploadIO.new file, content_type, File.basename(file.path) params = params.merge file: upload_file post_data = Net::HTTP::Post::Multipart.new uri.path, params post_data.add_field(API_KEY_HEADER, auth_helper.get_token_from_gconfig) https_request = Net::HTTP.new uri.host, uri.port https_request.use_ssl = true if uri.scheme == 'https' response = https_request.request post_data exit_if_error(msg_if_error, response) JSON.parse response.body end
post_json(url, params, msg_if_error = DEFAULT_ERROR_MSG)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 43 def post_json url, params, msg_if_error = DEFAULT_ERROR_MSG options = { body: params.to_json, headers: { 'Content-Type' => 'application/json', API_KEY_HEADER => auth_helper.get_token_from_gconfig } } response = HTTParty.post("#{server_url}#{url}", options) exit_if_error(msg_if_error, response) JSON.parse response.body end
server_url()
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 16 def server_url host = if ENV['HOLISTICS_DEV'] || %w(development test).include?(ENV['HOLISTICS_ENV']) 'http://localhost:3000' elsif ENV['HOLISTICS_STAGING'] || ENV['HOLISTICS_ENV'] == 'staging' 'https://staging.holistics.io' elsif ENV['HOLISTICS_HOST'] ENV['HOLISTICS_HOST'] else SERVER_URL end host += '/' if host[-1] != '/' host end
simple_get(url, token = nil)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 38 def simple_get(url, token = nil) HTTParty .get url, headers: { API_KEY_HEADER => token || auth_helper.get_token_from_gconfig } end
Private Instance Methods
endpoint_for(route)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 98 def endpoint_for route "#{server_url}/#{route}" end
err_and_exit(message, response)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 87 def err_and_exit(message, response) STDERR.puts message STDERR.puts "Error Response Code: #{response.code}" STDERR.puts response.body exit 1 end
exit_if_error(message, response)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 83 def exit_if_error (message, response) err_and_exit(message, response) if response_has_error?(response) end
response_has_error?(response)
click to toggle source
# File lib/holistics/helpers/http_request.rb, line 94 def response_has_error?(response) response.code.to_i != 200 end