zoho_books 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd3b88e597aa413b526eaa39c177a76363b6b8ab1db56dcc75a6d01584990c73
4
- data.tar.gz: 2197745702a2b55be3c2df53c22afd2925bb0e6109355986ea72c0805284cd98
3
+ metadata.gz: bb4b8600d21b217c2da5192625ef25fb2148a45bbe74107b19c34dba8c0dd8d2
4
+ data.tar.gz: 0265b974da780a632dbd3c5a8bc441413a3c81ff04201c7bf7d4aa1ef83a2325
5
5
  SHA512:
6
- metadata.gz: 6c33ff3e16a6fd0c3b682f7893b47d168bb8c9b4ba498db79e281ceddf392f5eafd7c9914ee5c6241b53a6edf08c05d7fb188bc76267b39ac988fd4247752322
7
- data.tar.gz: f94429205a0be708f1ecc204cc6f1f0b33eb28b1fb4899b1fe3ba7f3957824203f44ce77d9d37aa668eb7b840b7d74a729f1da36d6ccad2e4db0bf8687a553cb
6
+ metadata.gz: '08a246a314ef77fc8dac92e7d3ee6973aa6766c9783a793f641a342d91c94ac366fdad3ec09622c201a518ff0d03536dfe7d4279125b40423db7d5e06983879d'
7
+ data.tar.gz: 4337a73f4112cd54a7c0cfd106f4f10c0f41bdea7cfb192615580814b105a03469324de06a9ad7e20d1407fa7696b1cefb9ce18016dbc4cf6c7c8e61eb3ac432
@@ -1,15 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
  require 'httparty'
3
3
 
4
- class ZohoBooks::Auth
5
- def self.refresh_access_token
6
- response = HTTParty.post("#{ENV['ZOHO_BASE_URL']}/oauth/v2/token?refresh_token="\
7
- "#{ENV['ZOHO_REFRESH_TOKEN']}&client_id=#{ENV['ZOHO_CLIENT_ID']}&client_secret="\
8
- "#{ENV['ZOHO_CLIENT_SECRET']}&redirect_urrefresh_access_tokeni=#{ENV['ZOHO_REDIRECT_URI']}"\
9
- '&grant_type=refresh_token')
4
+ module ZohoBooks
5
+ class Auth
6
+ def self.refresh_access_token
7
+ endoint_url = "oauth/v2/token?refresh_token=#{ZohoBooks.config.refresh_token}&client_id=#{ZohoBooks.config.client_id}&client_secret=#{ZohoBooks.config.client_secret}&redirect_uri=#{ZohoBooks.config.redirect_uri}&grant_type=refresh_token"
8
+ response = HTTParty.post("#{ZohoBooks.config.base_url}/#{endoint_url}")
10
9
 
11
- return ZohoBooks::Error.new(response.code, response["error"]) if response.code != 200
10
+ return ZohoBooks::Error.new(response.code, response["error"]) if response.code != 200
12
11
 
13
- response.parsed_response['access_token']
12
+ ZohoBooks.config.access_token = response.parsed_response['access_token']
13
+ ZohoBooks.config.access_token_expires_at = Time.now.to_i + response.parsed_response['expires_in'].to_i
14
+
15
+ response.parsed_response['access_token']
16
+ end
14
17
  end
15
18
  end
@@ -0,0 +1,30 @@
1
+ module ZohoBooks
2
+ class Config
3
+ attr_accessor :client_id, :client_secret, :redirect_uri, :refresh_token, :base_url,
4
+ :portal_id, :access_token, :organization_id, :refresh_token, :access_token_expires_at
5
+
6
+ DEFAULT_BASE_URL = "https://accounts.zoho.eu".freeze
7
+
8
+ def initialize(config = {})
9
+ @base_url = config["base_url"] || DEFAULT_BASE_URL
10
+ @portal_id = config["portal_id"]
11
+ @access_token = config["access_token"]
12
+ @client_id = config["client_id"]
13
+ @client_secret = config["client_secret"]
14
+ @redirect_uri = config["redirect_uri"]
15
+ @organization_id = config["organization_id"]
16
+ @refresh_token = config["refresh_token"]
17
+
18
+ @access_token_expires_at = nil
19
+ end
20
+ end
21
+
22
+ class << self
23
+ attr_accessor :config
24
+
25
+ def configure
26
+ self.config ||= Config.new
27
+ yield(config)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+ require 'httparty'
3
+
4
+ module ZohoBooks
5
+ class Connection
6
+
7
+ def self.get(url)
8
+ response = HTTParty.get(url, headers: headers)
9
+
10
+ return ZohoBooks::Error.new(response.code, response["error"]) if response.code != 200
11
+
12
+ response
13
+ end
14
+
15
+ def self.post(url, body)
16
+ response = HTTParty.post(url, body: body, headers: headers)
17
+
18
+ return render_error(response) if response.code != 201
19
+
20
+ response
21
+ end
22
+
23
+ def self.put(url, body)
24
+ response = HTTParty.put(url, body: body, headers: headers)
25
+
26
+ return render_error(response) if response.code != 200
27
+
28
+ response
29
+ end
30
+
31
+ def self.delete(url)
32
+ response = HTTParty.delete(url, headers: headers)
33
+
34
+ return render_error(response) if response.code != 200
35
+
36
+ response
37
+ end
38
+
39
+ private
40
+
41
+ def self.headers
42
+ {
43
+ 'Authorization' => "Zoho-oauthtoken #{access_token}",
44
+ 'Content-Type' => 'application/json'
45
+ }
46
+ end
47
+
48
+ def self.access_token
49
+ if ZohoBooks.config.access_token_expires_at.nil? || ZohoBooks.config.access_token_expires_at < Time.now.to_i
50
+ ZohoBooks::Auth.refresh_access_token
51
+ else
52
+ ZohoBooks.config.access_token
53
+ end
54
+ end
55
+
56
+ def self.render_error(response)
57
+ ZohoBooks::Error.new(response.code, response["message"] || '')
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ require 'httparty'
3
+
4
+ module ZohoBooks
5
+ BASE_URL='https://www.zohoapis.eu/books/v3'
6
+
7
+ class Contact
8
+ def self.list(opts = {})
9
+ query = opts.map { |k, v| "#{k}=#{v}" }.join('&')
10
+ ZohoBooks::Connection.get("#{BASE_URL}/contacts?organization_id=#{ZohoBooks.config.organization_id}&#{query}")
11
+ end
12
+
13
+ def self.get(id, opts = {})
14
+ ZohoBooks::Connection.get("#{BASE_URL}/contacts/#{id}?organization_id=#{ZohoBooks.config.organization_id}")
15
+ end
16
+
17
+ def self.create(body, opts = {})
18
+ ZohoBooks::Connection.post("#{BASE_URL}/contacts?organization_id=#{ZohoBooks.config.organization_id}", body.to_json)
19
+ end
20
+
21
+ def self.update(id, body, opts = {})
22
+ ZohoBooks::Connection.put("#{BASE_URL}/contacts/#{id}?organization_id=#{ZohoBooks.config.organization_id}", body.to_json)
23
+ end
24
+
25
+ def self.delete(id, opts = {})
26
+ ZohoBooks::Connection.delete("#{BASE_URL}/contacts/#{id}?organization_id=#{ZohoBooks.config.organization_id}")
27
+ end
28
+ end
29
+ end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class ZohoBooks::Error
4
- attr_reader :code, :message
3
+ module ZohoBooks
4
+ class Error < StandardError
5
+ attr_reader :code, :message
5
6
 
6
- def initialize(code, message)
7
- @code = code
8
- @message = message
7
+ def initialize(code, message)
8
+ @code = code
9
+ @message = message
10
+ end
9
11
  end
10
12
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ require 'httparty'
3
+
4
+ module ZohoBooks
5
+ BASE_URL='https://www.zohoapis.eu/books/v3'
6
+
7
+ class Item
8
+ def self.list(opts = {})
9
+ query = opts.map { |k, v| "#{k}=#{v}" }.join('&')
10
+ ZohoBooks::Connection.get("#{BASE_URL}/items?organization_id=#{ZohoBooks.config.organization_id}&#{query}")
11
+ end
12
+
13
+ def self.create(body, opts = {})
14
+ ZohoBooks::Connection.post("#{BASE_URL}/items?organization_id=#{ZohoBooks.config.organization_id}", body.to_json)
15
+ end
16
+
17
+ def self.update(id, body, opts = {})
18
+ ZohoBooks::Connection.put("#{BASE_URL}/items/#{id}?organization_id=#{ZohoBooks.config.organization_id}", body.to_json)
19
+ end
20
+
21
+ def self.delete(id, opts = {})
22
+ ZohoBooks::Connection.delete("#{BASE_URL}/items/#{id}?organization_id=#{ZohoBooks.config.organization_id}")
23
+ end
24
+ end
25
+ end
data/lib/zoho_books.rb CHANGED
@@ -1,6 +1,10 @@
1
- class ZohoBooks
2
-
1
+ module ZohoBooks
3
2
  end
4
3
 
5
- require_relative 'zoho_books/auth'
6
- require_relative 'zoho_books/error'
4
+ require 'zoho_books/auth'
5
+ require 'zoho_books/error'
6
+ require 'zoho_books/connection'
7
+ require 'zoho_books/item'
8
+ require 'zoho_books/config'
9
+ require 'zoho_books/contact'
10
+
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zoho_books
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - PapaLoup
8
8
  - Raphtml
9
+ - theobcl
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2023-09-12 00:00:00.000000000 Z
13
+ date: 2023-10-02 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: httparty
@@ -33,8 +34,11 @@ extra_rdoc_files: []
33
34
  files:
34
35
  - lib/zoho_books.rb
35
36
  - lib/zoho_books/auth.rb
37
+ - lib/zoho_books/config.rb
38
+ - lib/zoho_books/connection.rb
39
+ - lib/zoho_books/contact.rb
36
40
  - lib/zoho_books/error.rb
37
- - lib/zoho_books/invoice.rb
41
+ - lib/zoho_books/item.rb
38
42
  homepage: https://rubygems.org/gems/zoho_books
39
43
  licenses:
40
44
  - MIT
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class ZohoBooks::Invoice
4
- class << self
5
- @id = id
6
- end
7
-
8
- def self.list(access_token:, **args)
9
- response = HTTParty.get("#{ENV['ZOHO_BASE_URL']}/api/v3/invoices?#{query(args)}",headers: {
10
- 'Authorization' => "Zoho-oauthtoken #{access_token}"
11
- })
12
-
13
- response.parsed_response['invoices'].presence || raise(Zoho::GetInvoicesError)
14
- end
15
-
16
- private
17
-
18
- def query(query_params)
19
- if query_params.present?
20
- query_from_given_block = query_params.map { |k, v| "#{k}=#{v}" }.join('&')
21
- "organization_id=#{ENV['ZOHO_ORGANISATION_ID']}&#{query_from_given_block}"
22
- else
23
- "organization_id=#{ENV['ZOHO_ORGANISATION_ID']}"
24
- end
25
- end
26
- end