vindi 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b6a28994b0f5108cc5c11887854de4db578eddb
4
+ data.tar.gz: 9c77d88efb1d79de6abc352bc25a2a633a920a5d
5
+ SHA512:
6
+ metadata.gz: 7b470b6602760c7787bcd5b53b77651cff3e0043f368a8a2d02cefa54d88fb967f67beeeb71f59fafca5c94553383b744fad104c78b6e67b9edf51b3d554baf4
7
+ data.tar.gz: c22faffb32cfe3dd9be3cf98916c6eb8c1495e64fad00faa46d41bcb2d19035f6a21dfc2d097ad2d3432ca7428d3df637d44eda12614673f64c80e2f27065963
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Vindi Ruby
2
+
3
+ Ruby toolkit para a [API de Recorrência][link-introducao-api] da [Vindi][link-vindi].
4
+
5
+ [![Build Status](https://semaphoreci.com/api/v1/projects/48986e75-f1a3-4ca3-ae41-2d6cf64ac507/1512354/badge.svg)](https://semaphoreci.com/vindi/vindi-ruby)
6
+
7
+ ## Instalação
8
+
9
+ ```ruby
10
+ gem 'vindi-ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install vindi-ruby
20
+
21
+ ## Uso
22
+ Os métodos da API estão disponíveis atraves dos métodos da instancia de um cliente
23
+
24
+ ```ruby
25
+ client = Vindi::Client.new(key: 'VINDI_KEY')
26
+ ```
27
+
28
+ ### Consumindo recursos
29
+ Os recursos são fornecidos através do objeto de retono e os campos retornados podem ser acessados pela notação de attributos de um Hash
30
+
31
+ ```ruby
32
+ # Listando planos de um lojista
33
+ client.list_plans
34
+ # => [{:id=>15, :name=>"My awesome plan", :interval=>"months", :interval_count=>1, :billing_trigger_type=>"beginning_of_period" ...
35
+
36
+ # Consultando um plano
37
+ client.plan(15)
38
+ # => {:id=>15, :name=>"My awesome plan", :interval=>"months", :interval_count=>1, :billing_trigger_type=>"beginning_of_period" ...
39
+
40
+ # Atualizando um plano
41
+ client.update_plan(15, {name: 'My plan'})
42
+ # => {:id=>15, :name=>"My plan", :interval=>"months", :interval_count=>1, :billing_trigger_type=>"beginning_of_period" ...
43
+
44
+ # Criando um plano
45
+ client.create_plan({name: 'My new plan', interval: 'months', interval_count: 1, billing_trigger_type: 'beginning_of_period'})
46
+ ```
47
+
48
+ ### Acessando respostas HTTP
49
+
50
+ ```ruby
51
+ client.list_plans
52
+ response = client.last_response
53
+ status = response.status
54
+ ```
55
+
56
+ ## Dúvidas
57
+ Caso necessite de informações sobre a plataforma ou API, por favor acesse o [Atendimento Vindi](http://atendimento.vindi.com.br/hc/pt-br).
58
+
59
+ ## Segurança
60
+ Se você descobrir qualquer questão relacionada a segurança, por favor, envie um e-mail para seguranca@vindi.com.br ao invés de utilizar os issues.
61
+
62
+ ## Changelog
63
+ Todas as informações sobre cada release podem ser consultadas em [CHANGELOG.md](CHANGELOG.md).
64
+
65
+ ## Créditos
66
+ - [Vindi][link-author]
67
+ - [Todos os Contribuidores][link-contributors]
68
+
69
+ ## Licença
70
+ GNU GPLv3. Por favor, veja o [Arquivo de Licença](license.txt) para mais informações.
71
+
72
+ [link-vindi]: https://www.vindi.com.br
73
+ [link-introducao-api]: http://atendimento.vindi.com.br/hc/pt-br/articles/203020644-Introdu%C3%A7%C3%A3o-%C3%A0-API-de-Recorr%C3%AAncia
74
+ [link-author]: https://github.com/vindi
75
+ [link-contributors]: https://github.com/vindi/vindi-ruby/graphs/contributors
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task test: :spec
8
+ task default: :spec
9
+
10
+ namespace :doc do
11
+ begin
12
+ require 'yard'
13
+ YARD::Rake::YardocTask.new do |task|
14
+ task.files = ['README.md', 'lib/**/*.rb']
15
+ task.options = [
16
+ '--output-dir', 'doc/yard',
17
+ '--markup', 'markdown',
18
+ ]
19
+ end
20
+ rescue LoadError
21
+ end
22
+ end
data/lib/vindi.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'vindi/client'
2
+
3
+ module Vindi
4
+ end
@@ -0,0 +1,15 @@
1
+ require 'vindi/configuration'
2
+ require 'vindi/request'
3
+ require 'vindi/rest'
4
+
5
+ module Vindi
6
+ class Client
7
+ include Vindi::Configuration
8
+ include Vindi::Request
9
+ include Vindi::Rest
10
+
11
+ def initialize(options = {}, &block)
12
+ setup && configure(options, &block)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ require 'vindi/default'
2
+
3
+ module Vindi
4
+
5
+ # Configuration options for Client
6
+ module Configuration
7
+ attr_reader :user_agent, :middleware, :connection_options
8
+ attr_accessor :key, :default_media_type, :api_endpoint
9
+
10
+ class << self
11
+ def keys
12
+ @keys ||= [:key, :default_media_type, :api_endpoint, :user_agent,
13
+ :middleware, :connection_options]
14
+ end
15
+ end
16
+
17
+ # Initializes a new Client object
18
+ #
19
+ # @param options [Hash]
20
+ # @return [Vindi::Client]
21
+ def configure(options = {})
22
+ options.each do |key, value|
23
+ instance_variable_set("@#{key}", value)
24
+ end
25
+
26
+ yield(self) if block_given?
27
+ end
28
+
29
+ def reset!
30
+ Vindi::Configuration.keys.each do |key|
31
+ instance_variable_set("@#{key}", Vindi::Default.options[key])
32
+ end
33
+ end
34
+ alias :setup :reset!
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Vindi
5
+ module Connection
6
+
7
+ # HTTP client for the Vindi API
8
+ #
9
+ # @return Faraday::Connection
10
+ def http_client
11
+ @http_client = Faraday.new(api_endpoint, connection_options) do |http|
12
+ http.request(:multipart)
13
+ http.request(:url_encoded)
14
+ http.basic_auth(@key, '')
15
+ http.builder.use @middleware
16
+ http.adapter(Faraday.default_adapter)
17
+ end
18
+ end
19
+
20
+ def last_response
21
+ @last_response if defined?(@last_response)
22
+ end
23
+
24
+ private
25
+
26
+ def request(method, path, data, options = {})
27
+ @last_response = response = http_client
28
+ .public_send(method, URI::Parser.new.escape(path.to_s), data, options)
29
+
30
+ response.body.empty? ? '' : symbolize_keys!(JSON.parse(response.body))
31
+ end
32
+
33
+ def symbolize_keys!(object)
34
+ if object.is_a?(Array)
35
+ object.each_with_index do |val, index|
36
+ object[index] = symbolize_keys!(val)
37
+ end
38
+ elsif object.is_a?(Hash)
39
+ object.keys.each do |key|
40
+ object[key.to_sym] = symbolize_keys!(object.delete(key))
41
+ end
42
+ end
43
+ object
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,63 @@
1
+ require 'vindi/response/raise_error'
2
+ require 'vindi/version'
3
+
4
+ module Vindi
5
+ module Default
6
+ # Default API endpoint
7
+ API_ENDPOINT = 'https://app.vindi.com.br/api/v1/'.freeze
8
+
9
+ # Default media type
10
+ MEDIA_TYPE = 'application/vnd.api+json'.freeze
11
+
12
+ # Default User Agent header string
13
+ USER_AGENT = "Vindi-Ruby/#{Vindi::VERSION}".freeze
14
+
15
+ MIDDLEWARE = Vindi::Response::RaiseError
16
+
17
+ class << self
18
+
19
+ # Default api endpoint string from ENV or API_ENDPOINT
20
+ # @return [String]
21
+ def api_endpoint
22
+ ENV['VINDI_API_ENDPOINT'] || API_ENDPOINT
23
+ end
24
+
25
+ # Default media type header string from ENV or MEDIA_TYPE
26
+ # @return [String]
27
+ def default_media_type
28
+ ENV['VINDI_MEDIA_TYPE'] || MEDIA_TYPE
29
+ end
30
+
31
+ # Default user KEY string from ENV
32
+ # @return [String]
33
+ def key
34
+ ENV['VINDI_KEY']
35
+ end
36
+
37
+ # Default User-Agent header string from ENV or USER_AGENT
38
+ # @return [String]
39
+ def user_agent
40
+ ENV['VINDI_USER_AGENT'] || USER_AGENT
41
+ end
42
+
43
+ def middleware
44
+ MIDDLEWARE
45
+ end
46
+
47
+ def connection_options
48
+ {
49
+ headers: {
50
+ 'User-Agent': user_agent
51
+ }
52
+ }
53
+ end
54
+
55
+ # Default Settings for Vindi Ruby Gem
56
+ # @return [Hash]
57
+ def options
58
+ Hash[Vindi::Configuration.keys
59
+ .map{ |key| [key, send(key)] }]
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,77 @@
1
+ module Vindi
2
+ # Custom error class for rescuing from all Vindi errors
3
+ class Error < StandardError
4
+
5
+ # Raised when Vindi returns a 4xx HTTP status code
6
+ ClientError = Class.new(self)
7
+
8
+ # Raised when Vindi returns the HTTP status code 400
9
+ BadRequest = Class.new(ClientError)
10
+
11
+ # Raised when Vindi returns the HTTP status code 401
12
+ Unauthorized = Class.new(ClientError)
13
+
14
+ # Raised when Vindi returns the HTTP status code 403
15
+ Forbidden = Class.new(ClientError)
16
+
17
+ # Raised when Vindi returns the HTTP status code 404
18
+ NotFound = Class.new(ClientError)
19
+
20
+ # Raised when Vindi returns the HTTP status code 406
21
+ NotAcceptable = Class.new(ClientError)
22
+
23
+ # Raised when Vindi returns the HTTP status code 422
24
+ UnprocessableEntity = Class.new(ClientError)
25
+
26
+ # Raised when Vindi returns the HTTP status code 429
27
+ TooManyRequests = Class.new(ClientError)
28
+
29
+ # Raised when Vindi returns a 5xx HTTP status code
30
+ ServerError = Class.new(self)
31
+
32
+ # Raised when Vindi returns the HTTP status code 502
33
+ BadGateway = Class.new(ServerError)
34
+
35
+ # Raised when Vindi returns the HTTP status code 503
36
+ ServiceUnavailable = Class.new(ServerError)
37
+
38
+ # Raised when Vindi returns the HTTP status code 504
39
+ GatewayTimeout = Class.new(ServerError)
40
+
41
+ class << self
42
+ def from_response(response)
43
+ status = response.status.to_i
44
+
45
+ if klass = case status
46
+ when 400 then Vindi::Error::BadRequest
47
+ when 401 then Vindi::Error::Unauthorized
48
+ when 403 then Vindi::Error::Forbidden
49
+ when 404 then Vindi::Error::NotFound
50
+ when 406 then Vindi::Error::NotAcceptable
51
+ when 415 then Vindi::Error::UnsupportedMediaType
52
+ when 422 then Vindi::Error::UnprocessableEntity
53
+ when 429 then Vindi::Error::TooManyRequests
54
+ when 400..499 then Vindi::Error::ClientError
55
+ when 500 then Vindi::Error::InternalServerError
56
+ when 502 then Vindi::Error::BadGateway
57
+ when 503 then Vindi::Error::ServiceUnavailable
58
+ when 504 then Vindi::Error::GatewayTimeout
59
+ when 500..599 then Vindi::Error::ServerError
60
+ end
61
+ klass.new(response) if klass
62
+ end
63
+ end
64
+ end
65
+
66
+ def initialize(response)
67
+ super(build_error_message(response))
68
+ end
69
+
70
+ def build_error_message(response)
71
+ return if response.nil?
72
+
73
+ message = "#{response[:method].to_s.upcase} "
74
+ message
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,61 @@
1
+ require 'vindi/connection'
2
+
3
+ module Vindi
4
+ module Request
5
+ include Vindi::Connection
6
+
7
+ # Make a HTTP GET request
8
+ #
9
+ # @param url [String] The path, relative to {#api_endpoint}
10
+ # @param options [Hash] Query and header params for request
11
+ # @return [Hash]
12
+ def get(url, options = {})
13
+ request :get, url, options
14
+ end
15
+
16
+ # Make a HTTP POST request
17
+ #
18
+ # @param url [String] The path, relative to {#api_endpoint}
19
+ # @param options [Hash] Query and header params for request
20
+ # @return Hash
21
+ def post(url, options = {})
22
+ request :post, url, options
23
+ end
24
+
25
+ # Make a HTTP PUT request
26
+ #
27
+ # @param url [String] The path, relative to {#api_endpoint}
28
+ # @param options [Hash] Query and header params for request
29
+ # @return Hash
30
+ def put(url, options = {})
31
+ request :put, url, options
32
+ end
33
+
34
+ # Make a HTTP PATCH request
35
+ #
36
+ # @param url [String] The path, relative to {#api_endpoint}
37
+ # @param options [Hash] Body and header params for request
38
+ # @return Hash
39
+ def patch(url, options = {})
40
+ request :patch, url, options
41
+ end
42
+
43
+ # Make a HTTP DELETE request
44
+ #
45
+ # @param url [String] The path, relative to {#api_endpoint}
46
+ # @param options [Hash] Body and header params for request
47
+ # @return Hash
48
+ def delete(url, options = {})
49
+ request :delete, url, options
50
+ end
51
+
52
+ # Make a HTTP HEAD request
53
+ #
54
+ # @param url [String] The path, relative to {#api_endpoint}
55
+ # @param options [Hash] Body and header params for request
56
+ # @return Hash
57
+ def head(url, options = {})
58
+ request :head, url, options
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ require 'faraday'
2
+ require 'vindi/error'
3
+
4
+ module Vindi
5
+ module Response
6
+
7
+ # This class raises exceptions based HTTP status codes retuned by the API
8
+ class RaiseError < Faraday::Response::Middleware
9
+
10
+ private
11
+
12
+ def on_complete(response)
13
+ if error = Vindi::Error.from_response(response)
14
+ raise error
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/vindi/rest.rb ADDED
@@ -0,0 +1,32 @@
1
+ Dir[File.expand_path("../rest/**/*.rb", __FILE__)].each do |file|
2
+ require file
3
+ end
4
+
5
+ module Vindi
6
+ module Rest
7
+ include Vindi::Rest::Plan
8
+ include Vindi::Rest::Product
9
+ include Vindi::Rest::Customer
10
+ include Vindi::Rest::PaymentMethod
11
+ include Vindi::Rest::Discount
12
+ include Vindi::Rest::Subscription
13
+ include Vindi::Rest::ProductItem
14
+ include Vindi::Rest::Period
15
+ include Vindi::Rest::Bill
16
+ include Vindi::Rest::BillItem
17
+ include Vindi::Rest::Charge
18
+ include Vindi::Rest::Transaction
19
+ include Vindi::Rest::PaymentProfile
20
+ include Vindi::Rest::Usage
21
+ include Vindi::Rest::Movement
22
+ include Vindi::Rest::Invoice
23
+ include Vindi::Rest::Message
24
+ include Vindi::Rest::ImportBatch
25
+ include Vindi::Rest::Notification
26
+ include Vindi::Rest::Issue
27
+ include Vindi::Rest::Merchant
28
+ include Vindi::Rest::Role
29
+ include Vindi::Rest::User
30
+ include Vindi::Rest::MerchantUser
31
+ end
32
+ end