Class: ZohoBooks::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_books/connection.rb

Class Method Summary collapse

Class Method Details

.access_tokenObject



48
49
50
51
52
53
54
# File 'lib/zoho_books/connection.rb', line 48

def self.access_token
  if ZohoBooks.config.access_token_expires_at.nil? || ZohoBooks.config.access_token_expires_at < Time.now.to_i
    ZohoBooks::Auth.refresh_access_token
  else
    ZohoBooks.config.access_token
  end
end

.delete(url) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/zoho_books/connection.rb', line 33

def self.delete(url)
  response = HTTParty.delete(url, headers:)

  return render_error(response) if response.code != 200

  response
end

.get(url) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/zoho_books/connection.rb', line 9

def self.get(url)
  response = HTTParty.get(url, headers:)

  return ZohoBooks::Error.new(response.code, response['error']) if response.code != 200

  response
end

.headersObject



41
42
43
44
45
46
# File 'lib/zoho_books/connection.rb', line 41

def self.headers
  {
    'Authorization' => "Zoho-oauthtoken #{access_token}",
    'Content-Type' => 'application/json'
  }
end

.post(url, body) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/zoho_books/connection.rb', line 17

def self.post(url, body)
  response = HTTParty.post(url, body:, headers:)

  return render_error(response) if response.code != 201

  response
end

.put(url, body) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/zoho_books/connection.rb', line 25

def self.put(url, body)
  response = HTTParty.put(url, body:, headers:)

  return render_error(response) if response.code != 200

  response
end

.render_error(response) ⇒ Object



56
57
58
# File 'lib/zoho_books/connection.rb', line 56

def self.render_error(response)
  ZohoBooks::Error.new(response.code, response['message'] || '')
end