omie-client 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +73 -0
- data/LICENSE +21 -0
- data/README.md +104 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/omie.rb +12 -0
- data/lib/omie/base_resource.rb +24 -0
- data/lib/omie/company.rb +180 -0
- data/lib/omie/connection.rb +94 -0
- data/lib/omie/error.rb +64 -0
- data/lib/omie/info.rb +7 -0
- data/lib/omie/version.rb +5 -0
- data/omie-client.gemspec +37 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6847f5e59fb2f87a9261b2fb956b0d9cef5095f1dfd9cc814ba750198e1caea0
|
4
|
+
data.tar.gz: 59bea11c7e639e1aadbc7a4b4f41604853e3d4d20103cd5f5014fda93ce89885
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c365f53634daf54cf5ce0c66e6e69b4dac1605835fc359da4b2de30fabaab3626bfc78a1988202a533ca2136c7e4cd315952e748daa54f558b107ad8705900da
|
7
|
+
data.tar.gz: ce4e5d8c753ef947e5c86d8c1a3a91b2c599396a091327be8bf9e14a7af0216d46812d1acdfe0d060ec1aea56692009e79eab8acc6347a1d6f2ec4ca7a53daf3
|
data/.gitignore
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
.rspec_status
|
8
|
+
.yardoc
|
9
|
+
.bundle
|
10
|
+
|
11
|
+
coverage
|
12
|
+
doc
|
13
|
+
omie-client-*.gem
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
omie-client (0.1)
|
5
|
+
activesupport
|
6
|
+
rest-client
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (6.0.2.1)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
zeitwerk (~> 2.2)
|
17
|
+
concurrent-ruby (1.1.5)
|
18
|
+
cpf_cnpj (0.5.0)
|
19
|
+
diff-lcs (1.3)
|
20
|
+
domain_name (0.5.20190701)
|
21
|
+
unf (>= 0.0.5, < 1.0.0)
|
22
|
+
factory_bot (5.1.1)
|
23
|
+
activesupport (>= 4.2.0)
|
24
|
+
http-accept (1.7.0)
|
25
|
+
http-cookie (1.0.3)
|
26
|
+
domain_name (~> 0.5)
|
27
|
+
i18n (1.8.2)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
mime-types (3.3.1)
|
30
|
+
mime-types-data (~> 3.2015)
|
31
|
+
mime-types-data (3.2019.1009)
|
32
|
+
minitest (5.14.0)
|
33
|
+
netrc (0.11.0)
|
34
|
+
rake (10.5.0)
|
35
|
+
rest-client (2.1.0)
|
36
|
+
http-accept (>= 1.7.0, < 2.0)
|
37
|
+
http-cookie (>= 1.0.2, < 2.0)
|
38
|
+
mime-types (>= 1.16, < 4.0)
|
39
|
+
netrc (~> 0.8)
|
40
|
+
rspec (3.9.0)
|
41
|
+
rspec-core (~> 3.9.0)
|
42
|
+
rspec-expectations (~> 3.9.0)
|
43
|
+
rspec-mocks (~> 3.9.0)
|
44
|
+
rspec-core (3.9.1)
|
45
|
+
rspec-support (~> 3.9.1)
|
46
|
+
rspec-expectations (3.9.0)
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
48
|
+
rspec-support (~> 3.9.0)
|
49
|
+
rspec-mocks (3.9.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.9.0)
|
52
|
+
rspec-support (3.9.2)
|
53
|
+
thread_safe (0.3.6)
|
54
|
+
tzinfo (1.2.6)
|
55
|
+
thread_safe (~> 0.1)
|
56
|
+
unf (0.1.4)
|
57
|
+
unf_ext
|
58
|
+
unf_ext (0.0.7.6)
|
59
|
+
zeitwerk (2.2.2)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
bundler (~> 1.16)
|
66
|
+
cpf_cnpj
|
67
|
+
factory_bot
|
68
|
+
omie-client!
|
69
|
+
rake (~> 10.0)
|
70
|
+
rspec (~> 3.0)
|
71
|
+
|
72
|
+
BUNDLED WITH
|
73
|
+
1.16.6
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 peerdustry / FLOSS
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Omie Client for Ruby
|
2
|
+
|
3
|
+
[Omie](https://app.omie.com.br/developer/service-list/) client for Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'omie-client'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install omie-client
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use the lib you must set the credentials to use your Omie instance. First,
|
24
|
+
you need to create the keys at
|
25
|
+
[Omie developer page](https://developer.omie.com.br/my-apps/)
|
26
|
+
or [Omie generate keys](https://app.omie.com.br/developer/generate-key/).
|
27
|
+
Then, set your credentials:
|
28
|
+
```ruby
|
29
|
+
Omie.app_key = "app_key"
|
30
|
+
Omie.app_secret = "app_secret"
|
31
|
+
```
|
32
|
+
|
33
|
+
**Now that the cliet is properly initialized you can start using it's
|
34
|
+
functions.**
|
35
|
+
|
36
|
+
The available classes of the Omie client corresponds the resources available in
|
37
|
+
Omie's API.
|
38
|
+
|
39
|
+
The lib follows the same design principles of Rail's ActiveRecord.
|
40
|
+
Each class provides a set of methods that perform direct calls to
|
41
|
+
Omie's API and return a Plain Old Ruby Objects (PORO) for each corresponding
|
42
|
+
entry. The resources' instance variables have the same portuguese names of
|
43
|
+
attributes of their respective JSON payloads.
|
44
|
+
|
45
|
+
The available resources are:
|
46
|
+
|
47
|
+
* Company (Clientes)
|
48
|
+
|
49
|
+
Ongoing Work:
|
50
|
+
|
51
|
+
* Procut (Produtos)
|
52
|
+
* SalesOrder (Ordem de Venda)
|
53
|
+
|
54
|
+
Future Versions:
|
55
|
+
|
56
|
+
* PurchaseOrder (Ordem de Compra)
|
57
|
+
|
58
|
+
### Company Resource
|
59
|
+
|
60
|
+
This class aims at manipulating resources available for Omie's companies,
|
61
|
+
including clients and providers.
|
62
|
+
|
63
|
+
> Ref: [Company documentation](https://app.omie.com.br/api/v1/geral/clientes/)
|
64
|
+
|
65
|
+
To create a new Company resource you can:
|
66
|
+
```ruby
|
67
|
+
company = Omie::Company.create(codigo_cliente_integracao: "XPTO_INTERNAL_CODE", cnpj_cpf: '26742035000190', nome_fantasia: 'Peerdustry Tecnologia LTDA', razao_social: 'Peerdustry Manufatura Compartilhada', email: 'contato@peerdustry.com')
|
68
|
+
|
69
|
+
# Update the email locally
|
70
|
+
company.email = "another@peerdustry.com"
|
71
|
+
|
72
|
+
# Update the register on Omie
|
73
|
+
company.save
|
74
|
+
```
|
75
|
+
|
76
|
+
Search for a specific entry:
|
77
|
+
```ruby
|
78
|
+
company = Omie::Company.find(cnpj_cpf: '26742035000190')
|
79
|
+
```
|
80
|
+
|
81
|
+
List all entries:
|
82
|
+
```ruby
|
83
|
+
companies = Omie::Company.list
|
84
|
+
puts companies.class # Array
|
85
|
+
|
86
|
+
puts companies.count
|
87
|
+
companies.each {|c| puts c.nome_fantasia }
|
88
|
+
```
|
89
|
+
|
90
|
+
## Development
|
91
|
+
|
92
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
93
|
+
|
94
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
Bug reports and pull requests are welcome. If you want to add a new resource
|
99
|
+
from the API, please open an issue first to avoid duplicated work.
|
100
|
+
|
101
|
+
## License
|
102
|
+
|
103
|
+
See the [LICENSE](LICENSE) file.
|
104
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "omie/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/omie.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'omie/base_resource'
|
4
|
+
require_relative 'omie/company'
|
5
|
+
require_relative 'omie/connection'
|
6
|
+
require_relative 'omie/error'
|
7
|
+
require_relative 'omie/info'
|
8
|
+
require_relative 'omie/version'
|
9
|
+
|
10
|
+
module Omie
|
11
|
+
cattr_accessor :app_key, :app_secret
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'active_support/json'
|
5
|
+
|
6
|
+
module Omie
|
7
|
+
# This class contains all logic shared among available resources.
|
8
|
+
class BaseResource
|
9
|
+
# Initialize the object based on a Hash of attributes their respective
|
10
|
+
# values
|
11
|
+
def initialize(args = {})
|
12
|
+
args.each do |key, value|
|
13
|
+
send("#{key}=", value) if respond_to?(key)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Update the object with the informed attributes passed as a Hash
|
18
|
+
def update_attributes(args = {})
|
19
|
+
args.each do |key, value|
|
20
|
+
send("#{key}=", value) if respond_to?(key)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/omie/company.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
|
5
|
+
module Omie
|
6
|
+
# This class abstracts the Company resource from Omie (Ref: Cliente) which
|
7
|
+
# is used for all kinds of companies in Omie, mainly Clients and Suppliers.
|
8
|
+
# It aims at providing abstractions to the endpoints described in
|
9
|
+
# {https://app.omie.com.br/api/v1/geral/clientes/}.
|
10
|
+
#
|
11
|
+
# The class methods of Omie::Company usually perform requests to Omie API and
|
12
|
+
# manipulate Company objects that contain the returned values.
|
13
|
+
# Attributes' names are equal to the Portuguese names described in the API
|
14
|
+
# documentation.
|
15
|
+
class Company < Omie::BaseResource
|
16
|
+
COMPANY_CALLS = {
|
17
|
+
LIST: 'ListarClientes',
|
18
|
+
CREATE: 'IncluirCliente',
|
19
|
+
UPDATE: 'AlterarCliente',
|
20
|
+
FIND: 'ConsultarCliente',
|
21
|
+
DELETE: 'ExcluirCliente',
|
22
|
+
SIMPLE: 'UpsertCliente'
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
URI = '/v1/geral/clientes/'
|
26
|
+
|
27
|
+
attr_accessor :email, :cnpj_cpf, :razao_social, :contato, :nome_fantasia
|
28
|
+
attr_accessor :codigo_cliente_integracao, :codigo_cliente_omie, :endereco
|
29
|
+
attr_accessor :cidade, :complemento, :estado, :endereco_numero, :cep
|
30
|
+
attr_accessor :codigo_pais, :bairro, :inscricao_estadual
|
31
|
+
|
32
|
+
# Record a new company using the
|
33
|
+
# {https://app.omie.com.br/api/v1/geral/clientes/#IncluirCliente
|
34
|
+
# IncluirCliente} call and returns an instance of Omie::Company
|
35
|
+
# with the data from the created company.
|
36
|
+
#
|
37
|
+
# @!scope class
|
38
|
+
# @param params [Hash]
|
39
|
+
# a hash containing the data to be recorded in the company based on
|
40
|
+
# the available attributes of this class
|
41
|
+
# @return [Omie::Company]
|
42
|
+
# the created company
|
43
|
+
# @raise [Omie::RequestError]
|
44
|
+
# in case of failed requests due to failed validations
|
45
|
+
def self.create(params = {})
|
46
|
+
response = Omie::Connection.request(URI, COMPANY_CALLS[:CREATE], params)
|
47
|
+
Omie::Company.new(response)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Updates an existing company using the
|
51
|
+
# {https://app.omie.com.br/api/v1/geral/clientes/#AlterarCliente
|
52
|
+
# AlterarCliente} call and returns an instance of the updated company.
|
53
|
+
# Omie will use either the {#codigo_cliente_integracao} or the
|
54
|
+
# {#codigo_cliente_omie} to identify the entry to be changed. It will
|
55
|
+
# change only the informed attributes in params.
|
56
|
+
#
|
57
|
+
# @!scope class
|
58
|
+
# @param params [Hash]
|
59
|
+
# a hash containing the search attribute to locate the company and
|
60
|
+
# the attributes/values to be updated.
|
61
|
+
# @return [Omie::Company]
|
62
|
+
# the updated company
|
63
|
+
# @raise [Omie::RequestError]
|
64
|
+
# in case of failed requests due to failed validations or when the
|
65
|
+
# company was not found.
|
66
|
+
def self.update(params = {})
|
67
|
+
response = Omie::Connection.request(URI, COMPANY_CALLS[:UPDATE], params)
|
68
|
+
Omie::Company.new(response)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Search for a company using the
|
72
|
+
# {https://app.omie.com.br/api/v1/geral/clientes/#ConsultarCliente
|
73
|
+
# ConsultarCliente} call and returns an instance of the found company
|
74
|
+
# or nil otherwise.
|
75
|
+
# One may use either the {#codigo_cliente_omie} or
|
76
|
+
# {#codigo_cliente_integracao} to search for company
|
77
|
+
#
|
78
|
+
# @!scope class
|
79
|
+
# @param params [Hash]
|
80
|
+
# a hash containing the search attribute to locate the company
|
81
|
+
# @return [Omie::Company]
|
82
|
+
# the found company
|
83
|
+
# @return [nil]
|
84
|
+
# in case of no company found
|
85
|
+
def self.find(params)
|
86
|
+
response = Omie::Connection.request(URI, COMPANY_CALLS[:FIND], params)
|
87
|
+
Omie::Company.new(response)
|
88
|
+
rescue Omie::RequestError
|
89
|
+
nil
|
90
|
+
end
|
91
|
+
|
92
|
+
# Get a paginated list of companies recorded in Omie by using the
|
93
|
+
# {https://app.omie.com.br/api/v1/geral/clientes/#ListarClientes
|
94
|
+
# ListarClientes}. You may change the params to get other pages of
|
95
|
+
# records.
|
96
|
+
#
|
97
|
+
# @!scope class
|
98
|
+
# @param page [Integer]
|
99
|
+
# the page to be returned.
|
100
|
+
# @param per_page [Integer]
|
101
|
+
# the number of items per page (max: 50).
|
102
|
+
# @return [Array<Omie::Company>]
|
103
|
+
# the list of found companies
|
104
|
+
def self.list(page = 1, per_page = 50)
|
105
|
+
params = { pagina: page, registros_por_pagina: per_page }
|
106
|
+
|
107
|
+
response = Omie::Connection.request(URI, COMPANY_CALLS[:LIST], params)
|
108
|
+
response['clientes_cadastro'].map { |client| Omie::Company.new(client) }
|
109
|
+
rescue Omie::RequestError
|
110
|
+
[]
|
111
|
+
end
|
112
|
+
|
113
|
+
# Get method for tags attribute.
|
114
|
+
#
|
115
|
+
# @return [Array<Hash>]
|
116
|
+
# list of hashes containing the tags' information with the following
|
117
|
+
# structure => {tag: "tag value"}
|
118
|
+
def tags
|
119
|
+
@tags ||= []
|
120
|
+
@tags
|
121
|
+
end
|
122
|
+
|
123
|
+
# Set method for tags attribute to be used for mass assignment of
|
124
|
+
# attribuites returned from Omie. It also sets {#tag_values}.
|
125
|
+
def tags=(value)
|
126
|
+
@tags = value
|
127
|
+
@tag_values = @tags.map { |t| t[:tag] }
|
128
|
+
end
|
129
|
+
|
130
|
+
# Get method for tag_values attribute.
|
131
|
+
#
|
132
|
+
# @return [Array<String>]
|
133
|
+
# list containing only the values of {#tags}
|
134
|
+
def tag_values
|
135
|
+
@tag_values ||= []
|
136
|
+
@tag_values
|
137
|
+
end
|
138
|
+
|
139
|
+
# Add a new tag method to formatted into Omie`s structure. It
|
140
|
+
# does not duplicate entries.
|
141
|
+
#
|
142
|
+
# @return [Omie::Company]
|
143
|
+
# self instance
|
144
|
+
def add_tag(tag = nil)
|
145
|
+
if tag && !tag_values.include?(tag)
|
146
|
+
tags << {
|
147
|
+
tag: tag
|
148
|
+
}
|
149
|
+
tag_values << tag
|
150
|
+
end
|
151
|
+
self
|
152
|
+
end
|
153
|
+
|
154
|
+
# Save the company.
|
155
|
+
#
|
156
|
+
# If the company is new a record is created on Omie, otherwise
|
157
|
+
# the existing record gets updated.
|
158
|
+
#
|
159
|
+
# @return [Omie::Company]
|
160
|
+
# the company itself updated
|
161
|
+
def save
|
162
|
+
company = if saved?
|
163
|
+
Omie::Company.update(as_json.except(['tag_values']))
|
164
|
+
else
|
165
|
+
Omie::Company.create(as_json.except(['tag_values']))
|
166
|
+
end
|
167
|
+
|
168
|
+
self.codigo_cliente_omie = company.codigo_cliente_omie if company
|
169
|
+
company
|
170
|
+
end
|
171
|
+
|
172
|
+
# Check whether the object has a related record on Omie based on the
|
173
|
+
# {#codigo_cliente_omie} attribute
|
174
|
+
#
|
175
|
+
# @return [Boolean]
|
176
|
+
def saved?
|
177
|
+
!codigo_cliente_omie.blank?
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rest-client'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Omie
|
7
|
+
# This class is used internally to send HTTP requests to Omie API based
|
8
|
+
# on RestClient gem. It is responsible for parsing JSON to Hash objects
|
9
|
+
# and handle errors.
|
10
|
+
class Connection
|
11
|
+
API_URI = 'https://app.omie.com.br/api' # Base URL for Omie API
|
12
|
+
|
13
|
+
# Perform a request to Omie API based on params. It does not make any
|
14
|
+
# kind of manipulation, and validation of such params. Rather, it only
|
15
|
+
# abstracts the interaction with the API, parsing the response or handling
|
16
|
+
# possible errors.
|
17
|
+
#
|
18
|
+
# @!scope class
|
19
|
+
# @param url [String]
|
20
|
+
# the url path to the resource. It is appended in the end of
|
21
|
+
# {Omie::Connection::API_URI}.
|
22
|
+
# @param call [String]
|
23
|
+
# the OMIE_CALL parameter that defines which resource is being
|
24
|
+
# requested. The Omie API defines endpoint based on this parameter
|
25
|
+
# instead of HTTP methods and URI.
|
26
|
+
# @param payload_to_request [Hash]
|
27
|
+
# the hash corresponding to the JSON payload of the request.
|
28
|
+
#
|
29
|
+
# @raise [Omie::RequestError]
|
30
|
+
# in case of a client error returned by the API through JSON payloads
|
31
|
+
# @raise [Omie::InvalidResponseError]
|
32
|
+
# in case of errors with unhandled responses
|
33
|
+
#
|
34
|
+
# @return [Hash]
|
35
|
+
# the response JSON parsed
|
36
|
+
def self.request(url, call, payload_to_request = {})
|
37
|
+
payload = create_payload(call, payload_to_request)
|
38
|
+
|
39
|
+
response = RestClient::Request.new(
|
40
|
+
method: :post,
|
41
|
+
url: API_URI + url,
|
42
|
+
payload: payload,
|
43
|
+
headers: { content_type: :json }
|
44
|
+
).execute
|
45
|
+
|
46
|
+
JSON.parse(response.body)
|
47
|
+
rescue RestClient::ExceptionWithResponse => e
|
48
|
+
Omie::Connection.handle_error_with_response(e.response)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Create the payload of the request with the credentials, the specific call
|
52
|
+
# and the payload data.
|
53
|
+
#
|
54
|
+
# @raise [Omie::MissingCredentialsError]
|
55
|
+
# when either {Omie.app_key} or {Omie.app_secret} is blank
|
56
|
+
#
|
57
|
+
# @return [String]
|
58
|
+
# a string in JSON format ready to be used as the payload of requests
|
59
|
+
# to Omie API.
|
60
|
+
def self.create_payload(call, data = {})
|
61
|
+
if Omie.app_key.blank? || Omie.app_secret.blank?
|
62
|
+
raise Omie::MissingCredentialsError,
|
63
|
+
'Omie.app_key and Omie.app_secret cannot be blank'
|
64
|
+
end
|
65
|
+
|
66
|
+
payload_request = {
|
67
|
+
app_key: Omie.app_key,
|
68
|
+
app_secret: Omie.app_secret,
|
69
|
+
call: call,
|
70
|
+
param: [data]
|
71
|
+
}
|
72
|
+
|
73
|
+
payload_request.to_json
|
74
|
+
end
|
75
|
+
|
76
|
+
# Handle errors with a response from Omie API by raising custom exceptions.
|
77
|
+
# The API does not use the HTTP
|
78
|
+
# status codes appropriately since it always returns error 500 for all
|
79
|
+
# kind of known errors (client errors). Moreover, the specific details of
|
80
|
+
# the error are described in the returned JSON. See {Omie::RequestError} for
|
81
|
+
# more details.
|
82
|
+
#
|
83
|
+
# @raise [Omie::RequestError]
|
84
|
+
# in case of documented error from Omie API (with status code 500)
|
85
|
+
# since it displays the error message returned by Omie API.
|
86
|
+
# @raise [Omie::InvalidResponseError]
|
87
|
+
# in case of errors with status code different from 500
|
88
|
+
def self.handle_error_with_response(response)
|
89
|
+
raise Omie::RequestError.new(nil, response) if response.code == 500
|
90
|
+
|
91
|
+
raise Omie::InvalidResponseError, "Invalid response received: #{response}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/lib/omie/error.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Omie
|
4
|
+
# This is the base Omie exception class. Rescue it if you want to catch
|
5
|
+
# any exceptions that your request might raise. In addition to the
|
6
|
+
# message, it may receive the response, a RestClient::Response object
|
7
|
+
# that can be used to improve the error messages.
|
8
|
+
class Error < RuntimeError
|
9
|
+
attr_accessor :message, :response
|
10
|
+
|
11
|
+
# Initialize the exception with the message and the request's response and
|
12
|
+
# call the {#after_initialize} callback
|
13
|
+
def initialize(message = nil, response = nil)
|
14
|
+
@message = message
|
15
|
+
@response = response
|
16
|
+
after_initialize
|
17
|
+
end
|
18
|
+
|
19
|
+
# Callback to be overridden by underlying classes
|
20
|
+
def after_initialize; end
|
21
|
+
end
|
22
|
+
|
23
|
+
# This exception must be used for failed requests with JSON responses
|
24
|
+
# from Omie.
|
25
|
+
#
|
26
|
+
# Omie does not use the semantic value of HTTP status codes. It always
|
27
|
+
# returns the 500 error code to any failed request that has an associated
|
28
|
+
# message to the client, which was supposed to use the 400* error codes.
|
29
|
+
# Moreover, the error message is provided through a returned JSON object
|
30
|
+
# that has the keys 'faultstring' and 'faultcode'. Such data is
|
31
|
+
# used in the exception message for a better understanding of the error.
|
32
|
+
class RequestError < Omie::Error
|
33
|
+
# Sets fault_string and fault_code based on Omie's responses for failed
|
34
|
+
# requests.
|
35
|
+
def after_initialize
|
36
|
+
return unless @response
|
37
|
+
|
38
|
+
json = JSON.parse(@response.body)
|
39
|
+
@fault_string = json['faultstring']
|
40
|
+
@fault_code = json['faultcode']
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return the custom message or a default message with the fault code and
|
44
|
+
# string.
|
45
|
+
def message
|
46
|
+
@message || default_message
|
47
|
+
end
|
48
|
+
|
49
|
+
def default_message
|
50
|
+
"Omie returned the error #{@fault_code}: '#{@fault_string}'"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# One may use this exception when a request produces a HTTP status codes
|
55
|
+
# different from *500*
|
56
|
+
class InvalidResponseError < Omie::Error
|
57
|
+
end
|
58
|
+
|
59
|
+
# This exception is used when the Omie credentials are not set.
|
60
|
+
#
|
61
|
+
# See {Omie.app_key} and {Omie.app_secret} for more details.
|
62
|
+
class MissingCredentialsError < Omie::Error
|
63
|
+
end
|
64
|
+
end
|
data/lib/omie/info.rb
ADDED
data/lib/omie/version.rb
ADDED
data/omie-client.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "omie/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "omie-client"
|
8
|
+
spec.version = Omie::VERSION
|
9
|
+
spec.authors = ["Peerdustry"]
|
10
|
+
spec.email = ["contato@peerdustry.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby client for Omie ERP API}
|
13
|
+
spec.description = %q{A Ruby client for Omie ERP API}
|
14
|
+
spec.homepage = "https://gitlab.com/peerdustry/floss/omie-client"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
spec.required_ruby_version = ">= 2.0.0"
|
26
|
+
|
27
|
+
# development dependencies
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency 'factory_bot', '>= 5.0'
|
32
|
+
spec.add_development_dependency 'cpf_cnpj', '~> 0.5'
|
33
|
+
|
34
|
+
# development dependencies
|
35
|
+
spec.add_dependency 'rest-client', '>= 2.0'
|
36
|
+
spec.add_dependency 'activesupport', '>= 6.0'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omie-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peerdustry
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_bot
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: cpf_cnpj
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rest-client
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '6.0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '6.0'
|
111
|
+
description: A Ruby client for Omie ERP API
|
112
|
+
email:
|
113
|
+
- contato@peerdustry.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- Gemfile
|
121
|
+
- Gemfile.lock
|
122
|
+
- LICENSE
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- lib/omie.rb
|
128
|
+
- lib/omie/base_resource.rb
|
129
|
+
- lib/omie/company.rb
|
130
|
+
- lib/omie/connection.rb
|
131
|
+
- lib/omie/error.rb
|
132
|
+
- lib/omie/info.rb
|
133
|
+
- lib/omie/version.rb
|
134
|
+
- omie-client.gemspec
|
135
|
+
homepage: https://gitlab.com/peerdustry/floss/omie-client
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: 2.0.0
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.7.8
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: A Ruby client for Omie ERP API
|
159
|
+
test_files: []
|