billimatic-client 0.1.0

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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +7 -0
  3. data/.gitignore +41 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +11 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +120 -0
  8. data/Guardfile +70 -0
  9. data/LICENSE +21 -0
  10. data/README.md +184 -0
  11. data/Rakefile +9 -0
  12. data/billimatic-client-ruby.gemspec +43 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +7 -0
  15. data/lib/billimatic.rb +53 -0
  16. data/lib/billimatic/client.rb +47 -0
  17. data/lib/billimatic/configuration.rb +12 -0
  18. data/lib/billimatic/entities/address_information.rb +15 -0
  19. data/lib/billimatic/entities/base.rb +9 -0
  20. data/lib/billimatic/entities/company.rb +25 -0
  21. data/lib/billimatic/entities/contract.rb +25 -0
  22. data/lib/billimatic/entities/customer.rb +12 -0
  23. data/lib/billimatic/entities/entity_service_item.rb +11 -0
  24. data/lib/billimatic/entities/invoice.rb +8 -0
  25. data/lib/billimatic/entities/invoice_rule.rb +8 -0
  26. data/lib/billimatic/entities/plan.rb +21 -0
  27. data/lib/billimatic/entities/product.rb +6 -0
  28. data/lib/billimatic/entities/subscription.rb +6 -0
  29. data/lib/billimatic/entities/webhook.rb +8 -0
  30. data/lib/billimatic/exception.rb +11 -0
  31. data/lib/billimatic/http.rb +32 -0
  32. data/lib/billimatic/request.rb +51 -0
  33. data/lib/billimatic/resources/base.rb +116 -0
  34. data/lib/billimatic/resources/company.rb +26 -0
  35. data/lib/billimatic/resources/contract.rb +10 -0
  36. data/lib/billimatic/resources/hooks.rb +25 -0
  37. data/lib/billimatic/resources/invoice.rb +11 -0
  38. data/lib/billimatic/resources/invoice_rule.rb +10 -0
  39. data/lib/billimatic/resources/plan.rb +11 -0
  40. data/lib/billimatic/resources/subscription.rb +19 -0
  41. data/lib/billimatic/resources/webhook.rb +10 -0
  42. data/lib/billimatic/response.rb +32 -0
  43. data/lib/billimatic/signature.rb +14 -0
  44. data/lib/billimatic/version.rb +3 -0
  45. metadata +272 -0
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
7
+ rescue LoadError
8
+ # no rspec available
9
+ end
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'billimatic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'billimatic-client'
8
+ spec.version = Billimatic::VERSION
9
+ spec.authors = ['Rodrigo Tassinari de Oliveira', 'Victor Franco']
10
+ spec.email = ['rodrigo@pittlandia.net', 'victor.alexandrefs@gmail.com']
11
+
12
+ spec.summary = %q{This is the official Ruby client for the Billimatic API.}
13
+ spec.description = %q{This is the official Ruby client for the Billimatic API. See http://www.billimatic.com,br for more information.}
14
+ spec.homepage = 'https://github.com/myfreecomm/billimatic-client-ruby/'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'typhoeus', '~> 1.0.1'
30
+ spec.add_dependency 'multi_json', '~> 1.11.2'
31
+ spec.add_dependency 'virtus', '~> 1.0.5'
32
+ spec.add_dependency 'wisper', '~> 1.6.1'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.11'
35
+ spec.add_development_dependency 'rake', '~> 11.1'
36
+ spec.add_development_dependency 'vcr', '~> 3.0'
37
+ spec.add_development_dependency 'pry-byebug', '~> 3.3'
38
+ spec.add_development_dependency 'rspec', '~> 3.4'
39
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.5'
40
+ spec.add_development_dependency 'simplecov', '~> 0.11'
41
+ spec.add_development_dependency 'guard-rspec', '~> 4.6'
42
+ spec.add_development_dependency 'test_notifier', '~> 2.0'
43
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "billimatic"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/billimatic.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'typhoeus'
2
+ require 'multi_json'
3
+ require 'wisper'
4
+
5
+ require 'billimatic/version'
6
+ require 'billimatic/configuration'
7
+ require 'billimatic/client'
8
+ require 'billimatic/http'
9
+ require 'billimatic/signature'
10
+
11
+ require 'billimatic/entities/base'
12
+ require 'billimatic/entities/entity_service_item'
13
+ require 'billimatic/entities/product'
14
+ require 'billimatic/entities/plan'
15
+ require 'billimatic/entities/address_information'
16
+ require 'billimatic/entities/customer'
17
+ require 'billimatic/entities/company'
18
+ require 'billimatic/entities/contract'
19
+ require 'billimatic/entities/invoice'
20
+ require 'billimatic/entities/invoice_rule'
21
+ require 'billimatic/entities/subscription'
22
+ require 'billimatic/entities/webhook'
23
+
24
+ require 'billimatic/resources/base'
25
+ require 'billimatic/resources/company'
26
+ require 'billimatic/resources/contract'
27
+ require 'billimatic/resources/invoice'
28
+ require 'billimatic/resources/invoice_rule'
29
+ require 'billimatic/resources/plan'
30
+ require 'billimatic/resources/subscription'
31
+ require 'billimatic/resources/webhook'
32
+
33
+ module Billimatic
34
+ def self.configuration
35
+ @configuration ||= Configuration.new
36
+ end
37
+
38
+ def self.configure
39
+ yield(configuration) if block_given?
40
+ end
41
+
42
+ def self.client(token)
43
+ Client.new(token)
44
+ end
45
+
46
+ def self.subscribe(event, callback)
47
+ Wisper.subscribe(callback, on: event, with: :call)
48
+ end
49
+
50
+ def self.signature(secret)
51
+ Signature.new(secret)
52
+ end
53
+ end
@@ -0,0 +1,47 @@
1
+ module Billimatic
2
+ class Client
3
+ attr_reader :http
4
+
5
+ def initialize(token)
6
+ @http = Http.new(token)
7
+ end
8
+
9
+ def authenticated?
10
+ http.get("/companies/search?cnpj=auth_test") do |response|
11
+ response.code == 200
12
+ end
13
+ rescue RequestError => e
14
+ raise e unless e.code == 401
15
+ false
16
+ end
17
+
18
+ def plans
19
+ Resources::Plan.new(http)
20
+ end
21
+
22
+ def subscriptions
23
+ Resources::Subscription.new(http)
24
+ end
25
+
26
+ def contracts
27
+ Resources::Contract.new(http)
28
+ end
29
+
30
+ def invoices
31
+ Resources::Invoice.new(http)
32
+ end
33
+
34
+ def invoice_rules
35
+ Resources::InvoiceRule.new(http)
36
+ end
37
+
38
+ def companies
39
+ Resources::Company.new(http)
40
+ end
41
+
42
+ def webhooks
43
+ Resources::Webhook.new(http)
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,12 @@
1
+ require "base64"
2
+
3
+ module Billimatic
4
+ class Configuration
5
+ attr_accessor :url, :user_agent
6
+
7
+ def initialize
8
+ @url = "https://app.billimatic.com.br/api/v1"
9
+ @user_agent = "Billimatic Ruby Client v#{Billimatic::VERSION}"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Billimatic
2
+ module Entities
3
+ class AddressInformation < Base
4
+ attribute :id, Integer
5
+ attribute :address, String
6
+ attribute :number, String
7
+ attribute :complement, String
8
+ attribute :district, String
9
+ attribute :zipcode, String
10
+ attribute :city, String
11
+ attribute :state, String
12
+ attribute :ibge_code, String
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require "virtus"
2
+
3
+ module Billimatic
4
+ module Entities
5
+ class Base
6
+ include Virtus.model
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Company < Base
4
+ attribute :id, Integer
5
+ attribute :account_id, Integer
6
+ attribute :name, String
7
+ attribute :company_name, String
8
+ attribute :cnpj, String
9
+ attribute :address, String
10
+ attribute :number, String
11
+ attribute :zipcode, String
12
+ attribute :district, String
13
+ attribute :complement, String
14
+ attribute :city, String
15
+ attribute :state, String
16
+ attribute :ibge_code, String
17
+ attribute :contacts, String
18
+ attribute :billing_contacts, String
19
+ attribute :comments, String
20
+ attribute :kind, String
21
+ attribute :created_at, DateTime
22
+ attribute :updated_at, DateTime
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Contract < Base
4
+ attribute :id, Integer
5
+ attribute :name, String
6
+ attribute :title, String
7
+ attribute :token, String
8
+ attribute :description, String
9
+ attribute :customer_id, Integer
10
+ attribute :customer_type, String
11
+ attribute :supplier_id, Integer
12
+ attribute :supplier_type, String
13
+ attribute :state, Integer
14
+ attribute :comments, String
15
+ attribute :init_date, Date
16
+ attribute :end_date, Date
17
+ attribute :created_at, DateTime
18
+ attribute :kind, String
19
+ attribute :registration_method, String
20
+ attribute :overdue, Boolean
21
+ attribute :status, String
22
+ attribute :plan, Plan
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Customer < Base
4
+ attribute :id, Integer
5
+ attribute :name, String
6
+ attribute :email, String
7
+ attribute :document, String
8
+ attribute :type, String
9
+ attribute :address_information, Billimatic::Entities::AddressInformation
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Billimatic
2
+ module Entities
3
+ class EntityServiceItem < Base
4
+ attribute :id, Integer
5
+ attribute :name, String
6
+ attribute :unit_value, Decimal
7
+ attribute :units, Decimal
8
+ attribute :value, Decimal
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Invoice < Base
4
+ attribute :id, Integer
5
+ # TODO attributes
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Billimatic
2
+ module Entities
3
+ class InvoiceRule < Base
4
+ attribute :id, Integer
5
+ # TODO attributes
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Plan < Base
4
+ attribute :id, Integer
5
+ attribute :name, String
6
+ attribute :description, String
7
+ attribute :price, Decimal
8
+ attribute :billing_period, Integer
9
+ attribute :translated_billing_period, String
10
+ attribute :has_trial, Boolean
11
+ attribute :trial_period, Integer
12
+ attribute :redirect_url, String
13
+ attribute :emites_service_values_id, Integer
14
+ attribute :cobrato_billet_charge_config_id, Integer
15
+ attribute :finance_category, String
16
+ attribute :finance_revenue_center, String
17
+ attribute :created_at, DateTime
18
+ attribute :products, Array[Product]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Product < EntityServiceItem
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Subscription < Contract
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Billimatic
2
+ module Entities
3
+ class Webhook < Base
4
+ attribute :id, Integer
5
+ # TODO attributes
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module Billimatic
2
+ class Exception < StandardError
3
+ attr_accessor :code, :body
4
+
5
+ def initialize(args = {})
6
+ super(args[:message])
7
+ @code = args[:code]
8
+ @body = args[:body]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ require "billimatic/request"
2
+ require "billimatic/response"
3
+
4
+ module Billimatic
5
+ class Http
6
+ attr_reader :token
7
+
8
+ def initialize(token)
9
+ @token = token
10
+ end
11
+
12
+ %w[get post delete put patch].each do |m|
13
+ define_method(m) do |path, options = {}, &block|
14
+ send_request(m.to_sym, path, options, &block)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def send_request(method, path, options, &block)
21
+ request = Request.new(options.merge!({
22
+ method: method,
23
+ token: token,
24
+ url: "#{Billimatic.configuration.url}#{path}",
25
+ user_agent: Billimatic.configuration.user_agent
26
+ }))
27
+ response = Response.new(request.run)
28
+ response.resolve!(&block)
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,51 @@
1
+ module Billimatic
2
+ class Request
3
+ attr_reader :args
4
+
5
+ def initialize(args)
6
+ @args = args
7
+ end
8
+
9
+ def run
10
+ request.run
11
+ request.response
12
+ end
13
+
14
+ private
15
+ def request
16
+ @request ||= Typhoeus::Request.new(args[:url], options)
17
+ end
18
+
19
+ def options
20
+ {
21
+ method: args[:method],
22
+ params: args[:params],
23
+ body: body,
24
+ headers: headers,
25
+ accept_encoding: "gzip, deflate"
26
+ }.reject {|k,v| v.nil?}
27
+ end
28
+
29
+ def headers
30
+ headers = args.fetch(:headers) { {} }
31
+ {
32
+ "Accept" => "application/json",
33
+ "Content-Type" => "application/json",
34
+ "User-Agent" => args[:user_agent],
35
+ "Authorization" => "Token token=#{token}",
36
+ "Accept-Language" => "pt-br",
37
+ }.merge(headers)
38
+ end
39
+
40
+ def body
41
+ body = args[:body]
42
+ body = MultiJson.dump(body) if body.is_a?(Hash)
43
+ body
44
+ end
45
+
46
+ def token
47
+ args[:token]
48
+ end
49
+
50
+ end
51
+ end