Class: Holistics::Helpers::HttpRequest
- Inherits:
-
Object
- Object
- Holistics::Helpers::HttpRequest
- Defined in:
- lib/holistics/helpers/http_request.rb
Constant Summary collapse
- DEFAULT_ERROR_MSG =
'Error occurred!'- SERVER_URL =
'https://api.holistics.io/'
Instance Method Summary collapse
- #auth_helper ⇒ Object
- #get(url, msg_if_error = DEFAULT_ERROR_MSG) ⇒ Object
-
#post_csv(url, params, file, msg_if_error = DEFAULT_ERROR_MSG) ⇒ Object
NOTE use_ssl option is required for connection to staging and production servers, they use HTTPS.
- #post_json(url, params, msg_if_error = DEFAULT_ERROR_MSG) ⇒ Object
- #server_url ⇒ Object
Instance Method Details
#auth_helper ⇒ Object
11 12 13 |
# File 'lib/holistics/helpers/http_request.rb', line 11 def auth_helper @auth_info ||= Helpers::AuthInfo.new end |
#get(url, msg_if_error = DEFAULT_ERROR_MSG) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/holistics/helpers/http_request.rb', line 30 def get url, msg_if_error = DEFAULT_ERROR_MSG url = auth_helper.api_url_for url response = HTTParty.get url exit_if_error(msg_if_error, response) JSON.parse response.body end |
#post_csv(url, params, file, msg_if_error = DEFAULT_ERROR_MSG) ⇒ Object
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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/holistics/helpers/http_request.rb', line 58 def post_csv url, params, file, msg_if_error = DEFAULT_ERROR_MSG uri = URI.parse(endpoint_for(url)) csv = UploadIO.new file, "text/csv", File.basename(file.path) params = params.merge file: csv post_data = Net::HTTP::Post::Multipart.new uri.path, params 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) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/holistics/helpers/http_request.rb', line 40 def post_json url, params, msg_if_error = DEFAULT_ERROR_MSG = { body: params.to_json, headers: {'Content-Type' => 'application/json'} } response = HTTParty.post("#{server_url}#{url}", ) exit_if_error(msg_if_error, response) JSON.parse response.body end |
#server_url ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/holistics/helpers/http_request.rb', line 15 def server_url host = if %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 |