Class: Bob::API

Inherits:
Object
  • Object
show all
Defined in:
lib/bob/api/api.rb

Constant Summary collapse

BASE_URL =
'https://api.hibob.com'
MAX_RETRIES =
3

Class Method Summary collapse

Class Method Details

.authorization_header(use_api_key: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bob/api/api.rb', line 81

def self.authorization_header(use_api_key: false)
  if use_api_key
    {
      Authorization: Bob.api_key
    }
  else
    {
      Authorization: "Basic #{Base64.strict_encode64("#{Bob.access_user_name}:#{Bob.access_token}")}"
    }
  end
end

.build_url(endpoint, params = {}) ⇒ Object



100
101
102
103
104
105
# File 'lib/bob/api/api.rb', line 100

def self.build_url(endpoint, params = {})
  url = "#{BASE_URL}/#{Bob.api_version}/#{endpoint}"
  url += "?#{URI.encode_www_form(params)}" unless params.empty?

  url
end

.content_headersObject



93
94
95
96
97
98
# File 'lib/bob/api/api.rb', line 93

def self.content_headers
  {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
end

.create_csv(content) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bob/api/api.rb', line 107

def self.create_csv(content)
  file_name = SecureRandom.alphanumeric(15)

  content.gsub!("\r", '').gsub!('', '')
  splitted = content.split("\n")
  CSV.open("tmp/#{file_name}.csv", 'wb') do |csv|
    csv << splitted.shift.split(',')
    splitted.each do |row|
      csv << CSV.parse_line(row)
    end
  end

  "tmp/#{file_name}.csv"
end

.delete(endpoint) ⇒ Object



65
66
67
68
69
# File 'lib/bob/api/api.rb', line 65

def self.delete(endpoint)
  url = build_url(endpoint)
  response = RestClient.delete(url)
  response.code
end

.get(endpoint, params = {}, csv_response: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bob/api/api.rb', line 15

def self.get(endpoint, params = {}, csv_response: false)
  retries = 0

  begin
    url = build_url(endpoint, params)
    response = RestClient.get(url, authorization_header)
  rescue
    if (retries += 1) < MAX_RETRIES
      sleep 60
      retry
    end
  end

    return create_csv(response.body) if csv_response

  JSON.parse(response.body)
end

.post(endpoint, params = {}, use_api_key: false) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/bob/api/api.rb', line 33

def self.post(endpoint, params = {}, use_api_key: false)
  url = build_url(endpoint)
  response = RestClient.post(
    url,
    params.to_json,
    authorization_header(use_api_key: use_api_key).merge(content_headers)
  )
  response.code
end

.post_file(endpoint, file_path) ⇒ Object

rubocop:disable Metrics/MethodLength



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bob/api/api.rb', line 49

def self.post_file(endpoint, file_path) # rubocop:disable Metrics/MethodLength
  url = build_url(endpoint)
  payload = {
    multipart: true,
    file: File.new(file_path)
  }

  headers = {
    Accept: 'application/json',
    'Content-Type': 'multipart/form-data',
    Authorization: "Basic #{Base64.strict_encode64("#{Bob.access_user_name}:#{Bob.access_token}")}"
  }
  response = RestClient.post(url, payload, headers)
  response.code
end

.post_media(endpoint, params = {}) ⇒ Object



43
44
45
46
47
# File 'lib/bob/api/api.rb', line 43

def self.post_media(endpoint, params = {})
  url = build_url(endpoint)
  response = RestClient.post(url, params.to_json, authorization_header.merge(content_headers))
  response.code
end

.put(endpoint, params = {}, use_api_key: false) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/bob/api/api.rb', line 71

def self.put(endpoint, params = {}, use_api_key: false)
  url = build_url(endpoint)
  response = RestClient.put(
    url,
    params.to_json,
    authorization_header(use_api_key: use_api_key).merge(content_headers)
  )
  response.code
end