Class: Holistics::Helpers::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/holistics/helpers/http_request.rb

Constant Summary collapse

DEFAULT_ERROR_MSG =
'Error occurred!'
SERVER_URL =
'https://api.holistics.io/'
API_KEY_HEADER =

must match Holistics::API_KEY_HEADER in main repo

'X-Holistics-Key'

Instance Method Summary collapse

Instance Method Details

#auth_helperObject



12
13
14
# 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) ⇒ Object



31
32
33
34
35
36
# 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_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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/holistics/helpers/http_request.rb', line 64

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
  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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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_urlObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



38
39
40
41
# 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