Class: Bob::API

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

Constant Summary collapse

BASE_URL =
'https://api.hibob.com'
SANDBOX_URL =
'https://api.sandbox.hibob.com'

Class Method Summary collapse

Class Method Details

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



75
76
77
78
79
80
81
# File 'lib/bob/api/api.rb', line 75

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

  url
end

.create_csv(content) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bob/api/api.rb', line 83

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



51
52
53
54
55
# File 'lib/bob/api/api.rb', line 51

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

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



14
15
16
17
18
19
20
# File 'lib/bob/api/api.rb', line 14

def self.get(endpoint, params = {}, csv_response: false)
  url = build_url(endpoint, params)
  response = RestClient.get(url, headers)
  return create_csv(response.body) if csv_response

  JSON.parse(response.body)
end

.headersObject



67
68
69
70
71
72
73
# File 'lib/bob/api/api.rb', line 67

def self.headers
  {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    Authorization: "Basic #{Base64.strict_encode64("#{Bob.access_user_name}:#{Bob.access_token}")}"
  }
end

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bob/api/api.rb', line 22

def self.post(endpoint, params = {})
  url = build_url(endpoint)
  response = RestClient.post(
    url,
    params.to_json,
    headers
  )
rescue RestClient::BadRequest => e
  p e
  response.code if response.try(:code)
end

.post_file(endpoint, file_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/bob/api/api.rb', line 40

def self.post_file(endpoint, file_path)
  url = build_url(endpoint)
  payload = {
    multipart: true,
    file: File.new(file_path)
  }

  response = RestClient.post(url, payload, headers.merge({ 'Content-Type': 'multipart/form-data' }))
  response.code
end

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



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

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

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



57
58
59
60
61
62
63
64
65
# File 'lib/bob/api/api.rb', line 57

def self.put(endpoint, params = {})
  url = build_url(endpoint)
  response = RestClient.put(
    url,
    params.to_json,
    headers
  )
  response.code
end