bling-ruby-api 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.
@@ -0,0 +1,72 @@
1
+ module Bling
2
+ module API
3
+ # This class is used to make requests to all available
4
+ # actions related to users in Bling api. Is strongly recommended
5
+ # to use the Bling::API module wrapper
6
+ #
7
+ class User < Request
8
+ # Get a list of available users
9
+ #
10
+ # @example
11
+ # users = Bling::API::User.new.list
12
+ #
13
+ # @return [Array] Call users index and return an array with records
14
+ #
15
+ def list
16
+ get_request(t_url(:users))
17
+ end
18
+
19
+ # Get a specific object based on user_id
20
+ #
21
+ # @example
22
+ # user = Bling::API::user.new.get
23
+ #
24
+ # @param [String] cpf or cnpj from the user in Bling
25
+ #
26
+ # @return [Array] Call user#show and return an array with one record
27
+ #
28
+ def get(user_id)
29
+ get_request(t_url(:user, user_id))
30
+ end
31
+
32
+ # Insert a user in Bling
33
+ #
34
+ # @example
35
+ # user = Bling::API::User.new.create(
36
+ # {
37
+ # name: 'Jonh Doe',
38
+ # company_name: '',
39
+ # tax_type: 1,
40
+ # document: '35165478662',
41
+ # ir_rg: '306153420',
42
+ # address: 'My great street',
43
+ # number: '33',
44
+ # additional_address: 'apt 12',
45
+ # zipcode: '03454020',
46
+ # city: 'Sao Paulo',
47
+ # uf: 'SP',
48
+ # phone: '(11) 2233-3322',
49
+ # email: jonh@@doe.com'
50
+ # }
51
+ # )
52
+ #
53
+ # @param [Hash] params A hash with user options
54
+ #
55
+ # @return [Array] Call user#create and return an array with one record
56
+ #
57
+ def create(params)
58
+ post_request(t_url(:user), parsed_xml(params))
59
+ end
60
+
61
+ private
62
+
63
+ def parsed_xml(params)
64
+ params = translate(params) unless Bling.config.default_language == :en
65
+ validate(params, :name, :user_type, :tax_type, :document)
66
+ user_template % params
67
+ end
68
+
69
+ end
70
+ end
71
+ end
72
+
data/lib/bling/base.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'bling/config'
2
+
3
+ module Bling
4
+ class Error < ::StandardError
5
+ end
6
+
7
+ # @return [Config] The config singleton.
8
+ def self.config
9
+ @config ||= Bling::Config.new
10
+ end
11
+
12
+ def self.config=(config)
13
+ @config = config
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ module Bling
2
+ class Config
3
+
4
+ def initialize
5
+ @api_url = 'https://bling.com.br/Api/v2/'
6
+ @response_format = :json
7
+ end
8
+
9
+ # The api key supplied by Bling service.
10
+ #
11
+ # @return [String]
12
+ attr_accessor :api_key
13
+
14
+ # Set response format from Bling, can be :xml or :json (default).
15
+ #
16
+ # @param [:xml, :json, 'xml', 'json']
17
+ def response_format=(response_format)
18
+ @response_format = response_format.to_sym
19
+ end
20
+
21
+ # The base API url
22
+ #
23
+ # @return [String]
24
+ attr_reader :api_url
25
+
26
+ # The response format from Bling, can be :xml or :json (default).
27
+ #
28
+ # @return [Symbol]
29
+ def response_format
30
+ @response_format || :json
31
+ end
32
+
33
+ # The default language if set to english lib will translate
34
+ # to portuguese before actually make request. Default to :en
35
+ #
36
+ # @param [:pt, :en]
37
+ def default_language=(language)
38
+ raise ArgumentError unless [:en, :pt].include?(language.to_sym)
39
+ @default_language = language.to_sym
40
+ end
41
+
42
+ # The default language of API params
43
+ #
44
+ # @return [Symbol]
45
+ def default_language
46
+ @default_language || :en
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+
3
+ # @private
4
+ class Bling::Railtie < ::Rails::Railtie
5
+
6
+ config.before_configuration do
7
+ config.bling = Bling.config
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Bling
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bling::API::Client do
4
+ before(:each) { allow(Net::HTTP).to receive(:get_response) }
5
+ let(:client) { Bling::API::Client.new('path', {}, :product) }
6
+
7
+ it 'always return a Response object' do
8
+ expect(client.get).to be_kind_of(Bling::API::Response)
9
+ end
10
+
11
+ it 'always set api key' do
12
+ expect(Bling.config).to receive(:api_key)
13
+ client.get
14
+ end
15
+
16
+ it 'get default response format' do
17
+ expect(Bling.config).to receive(:response_format)
18
+ client.get
19
+ end
20
+
21
+ it 'get default api endpoint' do
22
+ expect(Bling.config).to receive(:api_url).and_return('https://foo.bar')
23
+ client.get
24
+ end
25
+
26
+ describe '#get' do
27
+ it 'set parameter to uri' do
28
+ expect( URI ).to receive(:encode_www_form)
29
+ client.get
30
+ end
31
+
32
+ it 'makes a get request' do
33
+ expect(Net::HTTP).to receive(:get_response)
34
+ client.get
35
+ end
36
+ end
37
+
38
+ describe '#post' do
39
+ it 'makes a post request' do
40
+ expect(Net::HTTP).to receive(:post_form)
41
+ client.post
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bling::API::Product do
4
+
5
+ describe 'parsed hash' do
6
+ it 'correctly parses it to template' do
7
+ params = {
8
+ code: '92314',
9
+ description: 'Awesome product',
10
+ additional_description: 'In lovely colors',
11
+ unit: 'Pc',
12
+ price: 20.50,
13
+ cost_price: 14.30,
14
+ raw_weight: 2,
15
+ weight: 1.8,
16
+ tax_category: '1000.01.01',
17
+ origin: 0,
18
+ quantity: 10,
19
+ gtin: 832222,
20
+ gtin_package: 13414,
21
+ width: 25,
22
+ height: 15,
23
+ depth: 10,
24
+ min_quantity: 1,
25
+ max_quantity: 200,
26
+ cest: ''
27
+ }
28
+ product = Bling::API::Product.new
29
+ product.instance_variable_set(:@params, params)
30
+
31
+ parsed_params = product.send :parsed_xml
32
+ expect(parsed_params).to match(/Awesome/)
33
+ end
34
+
35
+
36
+ it 'works with the min required params' do
37
+ params = {
38
+ description: 'Awesome product',
39
+ price: 20.50,
40
+ }
41
+
42
+ product = Bling::API::Product.new
43
+ product.instance_variable_set(:@params, params)
44
+
45
+ parsed_params = product.send :parsed_xml
46
+ expect(parsed_params).to match(/20.5/)
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bling::API::Record do
4
+
5
+ it 'create readers to each hash key' do
6
+ params = { name: 'Bilbo', age: 111 }
7
+ hobbit = Bling::API::Record.new(params)
8
+ expect(hobbit.name).to eq 'Bilbo'
9
+ expect(hobbit.age).to eq 111
10
+ end
11
+
12
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bling::API::Response do
4
+
5
+
6
+ context 'with a valid http response' do
7
+ let(:http_response) { Net::HTTPOK.new('1.1', '200', 'OK') }
8
+
9
+ it 'is a success' do
10
+ pending
11
+ expect(described_class.new(http_response, 'foo').success?).to be_truthy
12
+ end
13
+
14
+ end
15
+
16
+ context 'with a invalid http response' do
17
+ let(:http_response) { Net::HTTPBadRequest.new('1.1', '400', 'Bad request') }
18
+
19
+ it 'is a failure' do
20
+ expect(described_class.new(http_response, 'foo').success?).to be_falsey
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bling::API::Translator do
4
+
5
+ describe '#translate_url' do
6
+ it 'accepts a Symbol as key' do
7
+ expect(described_class.translate_url(:product)).to eq 'produto'
8
+ end
9
+
10
+ it 'accepts a String as key' do
11
+ expect(described_class.translate_url('product')).to eq 'produto'
12
+ end
13
+
14
+ it 'return a nested id if passed' do
15
+ expect(described_class.translate_url(:product, id: 1)).to eq 'produto/1'
16
+ end
17
+
18
+ it 'accepts anything as id' do
19
+ expect(described_class.translate_url(:product, id: 'foobar')).to eq 'produto/foobar'
20
+ end
21
+ end
22
+
23
+ describe '#translate_hash' do
24
+ context 'to en' do
25
+ let(:response) { { code: '001', description: 'Awesome thing' } }
26
+
27
+ it 'translate a hash of Symbol keys' do
28
+ params = { codigo: '001', descricao: 'Awesome thing' }
29
+ expect(described_class.translate_hash(params)).to eq response
30
+ end
31
+
32
+ it 'converts non-snakecase to snakecase' do
33
+ params = { 'pesoBruto' => '1', descricao: 'Awesome thing' }
34
+ response = { :raw_weight => '1', description: 'Awesome thing' }
35
+ expect(described_class.translate_hash(params)).to eq response
36
+ end
37
+
38
+ it 'accepts Strings and Symbols as keys' do
39
+ params = { 'codigo' => '001', descricao: 'Awesome thing' }
40
+ expect(described_class.translate_hash(params)).to eq response
41
+ end
42
+
43
+ it 'recursively translate hashes and arrays' do
44
+ params = { resultado: { produtos: [{codigo: '001'}, {codigo: '002'}] } }
45
+ result = { result: { products: [{code: '001'}, {code: '002'}] } }
46
+ expect(described_class.translate_hash(params)).to eq result
47
+ end
48
+
49
+ it 'keeps keys missing on yml' do
50
+ params = { codigo: '001', nao_existente: 'foo' }
51
+ result = { code: '001', nao_existente: 'foo' }
52
+ expect(described_class.translate_hash(params)).to eq result
53
+ end
54
+ end
55
+
56
+ context 'to pt' do
57
+ let(:response) { { codigo: '001', descricao: 'Awesome thing' } }
58
+
59
+ it 'translate a hash of Symbol keys' do
60
+ params = { code: '001', description: 'Awesome thing' }
61
+ expect(described_class.translate_hash(params, to: :pt)).to eq response
62
+ end
63
+
64
+ it 'accepts Strings and Symbols as keys' do
65
+ params = { 'code' => '001', description: 'Awesome thing' }
66
+ expect(described_class.translate_hash(params, to: :pt)).to eq response
67
+ end
68
+
69
+ it 'recursively translate hashes and arrays' do
70
+ params = { result: { products: [{code: '001'}, {code: '002'}] } }
71
+ result = { resultado: { produtos: [{codigo: '001'}, {codigo: '002'}] } }
72
+ expect(described_class.translate_hash(params, to: :pt)).to eq result
73
+ end
74
+
75
+ it 'keeps keys missing on yml' do
76
+ params = { code: '001', nao_existente: 'foo' }
77
+ result = { codigo: '001', nao_existente: 'foo' }
78
+ expect(described_class.translate_hash(params, to: :pt)).to eq result
79
+ end
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,8 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'bling'
5
+
6
+ RSpec.configure do |config|
7
+ config.color = true
8
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bling-ruby-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Denis Tierno
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Gem to access Bling API in ruby
42
+ email: contato@locomotiva.pro
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - bling.gemspec
54
+ - lib/bling.rb
55
+ - lib/bling/api.rb
56
+ - lib/bling/api/client.rb
57
+ - lib/bling/api/locales/translations.yml
58
+ - lib/bling/api/order.rb
59
+ - lib/bling/api/parser.rb
60
+ - lib/bling/api/product.rb
61
+ - lib/bling/api/record.rb
62
+ - lib/bling/api/request.rb
63
+ - lib/bling/api/response.rb
64
+ - lib/bling/api/templates/order_installment_template
65
+ - lib/bling/api/templates/order_item_template
66
+ - lib/bling/api/templates/order_template
67
+ - lib/bling/api/templates/order_update_template
68
+ - lib/bling/api/templates/product_template
69
+ - lib/bling/api/templates/user_template
70
+ - lib/bling/api/translator.rb
71
+ - lib/bling/api/user.rb
72
+ - lib/bling/base.rb
73
+ - lib/bling/config.rb
74
+ - lib/bling/railtie.rb
75
+ - lib/bling/version.rb
76
+ - spec/lib/bling/api/client_spec.rb
77
+ - spec/lib/bling/api/product_spec.rb
78
+ - spec/lib/bling/api/record_spec.rb
79
+ - spec/lib/bling/api/response_spec.rb
80
+ - spec/lib/bling/api/translator_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: http://locomotiva.pro
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '2.1'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements:
100
+ - none
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Gem to access Bling API in ruby
106
+ test_files:
107
+ - spec/lib/bling/api/client_spec.rb
108
+ - spec/lib/bling/api/product_spec.rb
109
+ - spec/lib/bling/api/record_spec.rb
110
+ - spec/lib/bling/api/response_spec.rb
111
+ - spec/lib/bling/api/translator_spec.rb
112
+ - spec/spec_helper.rb
113
+ has_rdoc: