emites-client 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8552e2d06dc597aca23a6b974ed14f976ffb24ac
4
- data.tar.gz: 7ec3f6ba1c62d9b89afc6a3af46143248c668c5b
3
+ metadata.gz: 8ab5dbafa9e33acea5e32432ceed77d93c22fdcc
4
+ data.tar.gz: f9a9cd095c8f57772e8d269bf6d1e66530a349eb
5
5
  SHA512:
6
- metadata.gz: 87c8f32af751a2d4bb5e4576ebb94ddf7f71bf2d6d53dfebb1cf8292e53f4ad82be9944d69d3f47ca87348ac2b8736312bebb73599d7f7800c51a6b0bbf39e29
7
- data.tar.gz: 1ddf8bef26a1a8dc7c3265335776b026386c84c01f9dab3f9036fff42a2fbbd3a3b29c054745f1eeeac1dcb83b7da914e0a26a5eb729d978d5eb834b579f4c71
6
+ metadata.gz: 6b975aa0fb8dbbd590c8d44b2e85c242252e87bcbe395dc84672faa0eb48b0786e50555e2da5863bc8e6645b98eb6e002cae314c704573300ad62b748c6d4cc8
7
+ data.tar.gz: 537c29b01418ae17db311c93ed6db3441e3eca30c667ea33816f455df4e176de41afa8a0fa10ad5cf19607728abad362039abc04a76edce56f595073f1c72da8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- emites-client (0.1.1)
4
+ emites-client (0.1.2)
5
5
  multi_json (~> 1.11)
6
6
  typhoeus (~> 0.8)
7
7
  virtus (~> 1.0)
data/Rakefile CHANGED
@@ -7,3 +7,8 @@ begin
7
7
  rescue LoadError
8
8
  # no rspec available
9
9
  end
10
+
11
+ desc 'Open an irb session preloaded with this library'
12
+ task :console do
13
+ sh "irb -rubygems -I lib -r emites.rb"
14
+ end
@@ -7,8 +7,8 @@ require "emites/version"
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = "emites-client"
9
9
  spec.version = Emites::VERSION
10
- spec.authors = ["Leandro Thimóteo", "Marcos Tagomori", "Rodrigo Tassinari", "Vagner Zampieri", "Wanderson Policarpo"]
11
- spec.email = %w(leandro.thimoteo marcos.tagomori rodrigo.tassinari vagner.zampieri wanderson.policarpo).map { |nick| "#{nick}@myfreecomm.com.br" }
10
+ spec.authors = ["Leandro Thimóteo", "Marcos Tagomori", "Rodrigo Tassinari", "Vagner Zampieri", "Wanderson Policarpo", "Luiz Carlos Buiatte"]
11
+ spec.email = %w(leandro.thimoteo marcos.tagomori rodrigo.tassinari vagner.zampieri wanderson.policarpo luiz.buiatte).map { |nick| "#{nick}@myfreecomm.com.br" }
12
12
  spec.summary = %q{Official Ruby client for the Emites API.}
13
13
  spec.description = %q{Official Ruby client for the Emites API. Emites is a system for creating "notas fiscais eletrônicas" (NFe / NFSe) in Brazil.}
14
14
  spec.homepage = "https://github.com/myfreecomm/emites-client-ruby"
data/lib/emites.rb CHANGED
@@ -6,8 +6,10 @@ require "emites/version"
6
6
  require "emites/configuration"
7
7
  require "emites/http"
8
8
  require "emites/client"
9
+ require "emites/params"
9
10
 
10
11
  require "emites/entities/base"
12
+ require "emites/entities/collection"
11
13
  require "emites/entities/account"
12
14
  require "emites/entities/emitter"
13
15
  require "emites/entities/taker_address"
@@ -0,0 +1,57 @@
1
+ module Emites
2
+ module Entities
3
+
4
+ class Collection < Base
5
+ extend Forwardable
6
+
7
+ PAGE_REGEX = /page=(\d+)/
8
+
9
+ attr_reader :total
10
+ def_delegators :@collection, :first, :last, :each, :map, :count, :empty?, :any?
11
+
12
+ def initialize(response, entity_class)
13
+ @response = response
14
+ @entity_class = entity_class
15
+ @total = parsed_body['count'].to_i
16
+ @collection = []
17
+ end
18
+
19
+ def self.build(response, entity_class)
20
+ self.new(response, entity_class).build
21
+ end
22
+
23
+ def build
24
+ build_collection
25
+ self
26
+ end
27
+
28
+ def next_page
29
+ page_for('next')
30
+ end
31
+
32
+ def previous_page
33
+ page_for('previous')
34
+ end
35
+
36
+ def to_a
37
+ @collection.clone
38
+ end
39
+
40
+ private
41
+
42
+ def page_for(page_rel)
43
+ parsed_body[page_rel].match(PAGE_REGEX)[1].to_i rescue nil
44
+ end
45
+
46
+ def build_collection
47
+ parsed_body['collection'].each do |attributes|
48
+ @collection.push(@entity_class.new(attributes))
49
+ end
50
+ end
51
+
52
+ def parsed_body
53
+ @parsed_body ||= @response.parsed_body
54
+ end
55
+ end
56
+ end
57
+ end
@@ -7,6 +7,7 @@ module Emites
7
7
  attribute :rps_situation, Integer
8
8
  attribute :serie, String
9
9
  attribute :number, Integer
10
+ attribute :nfse_number, Integer
10
11
  attribute :rps_type, Integer
11
12
  attribute :emission_date, DateTime
12
13
  attribute :emission_date_nfse, DateTime
@@ -27,4 +28,4 @@ module Emites
27
28
  end
28
29
  end
29
30
  end
30
- end
31
+ end
@@ -5,11 +5,11 @@ module Emites
5
5
  attribute :status, String
6
6
  attribute :description, String
7
7
  attribute :nfse_key, String
8
- attribute :nfse_number, String
8
+ attribute :nfse_number, Integer
9
9
  attribute :number, Integer
10
10
  attribute :mirror, String
11
11
  attribute :xml, String
12
12
  attribute :errors, Array
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -0,0 +1,20 @@
1
+ module Emites
2
+
3
+ class Params
4
+ def initialize(attributes = {})
5
+ @attributes = symbolize_keys(attributes)
6
+ end
7
+
8
+ def permit(filters)
9
+ filters.map(&:to_sym).each_with_object(Hash.new) do |key, result|
10
+ result[key] = @attributes[key] if @attributes.has_key?(key)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def symbolize_keys(hash)
17
+ Hash[hash.map { |key,value| [key.to_sym, value] }]
18
+ end
19
+ end
20
+ end
@@ -12,24 +12,14 @@ module Emites
12
12
  @http = http
13
13
  end
14
14
 
15
- def parsed_body(response)
16
- MultiJson.load(response.body)
17
- rescue MultiJson::ParseError
18
- {}
19
- end
20
-
21
15
  protected
22
16
 
23
17
  def respond_with_collection(response, naked_klass = entity_klass)
24
- hash = parsed_body(response)
25
- hash["collection"].map do |item|
26
- naked_klass.new(item)
27
- end
18
+ Emites::Entities::Collection.build(response, naked_klass)
28
19
  end
29
20
 
30
21
  def respond_with_entity(response, naked_klass = entity_klass)
31
- item = parsed_body(response)
32
- naked_klass.new(item)
22
+ naked_klass.new(response.parsed_body)
33
23
  end
34
24
 
35
25
  def base_klass
@@ -42,4 +32,4 @@ module Emites
42
32
 
43
33
  end
44
34
  end
45
- end
35
+ end
@@ -24,13 +24,28 @@ module Emites
24
24
  #
25
25
  # Documentation: http://myfreecomm.github.io/emites/v1/modules/nfse.html#listagem
26
26
  #
27
- # @return [Array] an array of Webhook
27
+ # @return Emites::Entities::Collection a collection of Emites::Entities::Nfse
28
28
  def list
29
29
  http.get("/nfse") do |response|
30
30
  respond_with_collection(response)
31
31
  end
32
32
  end
33
33
 
34
+ # Lists all NFSes matching search parameters
35
+ #
36
+ # [API]
37
+ # Method: <tt>GET /api/v1/nfse?status=:status&page=:page</tt>
38
+ #
39
+ # Documentation: http://myfreecomm.github.io/emites/v1/modules/nfse.html#filtros
40
+ #
41
+ # @param params [Hash] an optional hash with filter parameters
42
+ # @return Emites::Entities::Collection a collection of Emites::Entities::Nfse
43
+ def search(params = {})
44
+ http.get("/nfse", params: filter(params)) do |response|
45
+ respond_with_collection(response)
46
+ end
47
+ end
48
+
34
49
  # Retrieves a Nfse by it's id
35
50
  #
36
51
  # [API]
@@ -69,7 +84,7 @@ module Emites
69
84
  # Documentation: http://myfreecomm.github.io/emites/sandbox/v1/modules/nfse.html#historico
70
85
  #
71
86
  # @param id [Integer] the Nfse id
72
- # @return [Array] an array of Emites::Entities::NfseStatusTransition
87
+ # @return Emites::Entities::Collection a collection of Emites::Entities::NfseStatusTransition
73
88
  def history(id)
74
89
  http.get("/nfse/#{id}/history") do |response|
75
90
  respond_with_collection(response, Entities::NfseStatusTransition)
@@ -168,6 +183,30 @@ module Emites
168
183
  end
169
184
 
170
185
  notify :create, :update, :cancel, :destroy
186
+
187
+ private
188
+
189
+ PERMITTED_PARAMS = %W(
190
+ status
191
+ emitter_id
192
+ nfse_key
193
+ emission_date_lte
194
+ emission_date_gte
195
+ number
196
+ nfse_number
197
+ amount_lte
198
+ amount_gte
199
+ taker_social_reason
200
+ taker_cpf
201
+ taker_cnpj
202
+ is_complete
203
+ serie,
204
+ page
205
+ )
206
+
207
+ def filter(params)
208
+ Emites::Params.new(params).permit(PERMITTED_PARAMS)
209
+ end
171
210
  end
172
211
  end
173
- end
212
+ end
@@ -7,17 +7,22 @@ module Emites
7
7
  class Response < SimpleDelegator
8
8
 
9
9
  def resolve!(&block)
10
- if success? || redirected?
10
+ timeout! if timed_out?
11
+ if(success? || redirected?)
11
12
  block_given? ? yield(self) : self
12
- elsif timed_out?
13
- timeout!
14
13
  else
15
14
  error!
16
15
  end
17
16
  end
18
17
 
19
18
  def redirected?
20
- response_code && response_code >= 300 && response_code < 400
19
+ (300..308).include?(self.code)
20
+ end
21
+
22
+ def parsed_body
23
+ MultiJson.load(body)
24
+ rescue MultiJson::ParseError
25
+ {}
21
26
  end
22
27
 
23
28
  private
@@ -30,9 +35,9 @@ module Emites
30
35
  raise RequestError.new(
31
36
  code: code,
32
37
  message: status_message,
33
- body: (MultiJson.load(body) rescue {})
38
+ body: parsed_body
34
39
  )
35
40
  end
36
41
 
37
42
  end
38
- end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Emites
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -25,4 +25,4 @@ describe Emites::Entities::Base do
25
25
  })
26
26
  end
27
27
  end
28
- end
28
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emites::Entities::Collection do
4
+ let(:json) { File.read('spec/fixtures/nfse_list.json') }
5
+ let(:response) { double(parsed_body: MultiJson.load(json)) }
6
+
7
+ subject { described_class.new(response, Emites::Entities::Nfse) }
8
+
9
+ describe '#build' do
10
+ it 'returns self' do
11
+ expect(subject.build).to eq(subject)
12
+ end
13
+
14
+ it 'builds a nfse collection' do
15
+ expect(subject.build.count).to eq(3)
16
+ expect(subject.build.first.class).to eq(Emites::Entities::Nfse)
17
+ end
18
+ end
19
+
20
+ describe '#next_page' do
21
+ it 'returns next page (3)' do
22
+ expect(subject.next_page).to eq(3)
23
+ end
24
+
25
+ context 'when there is no next page' do
26
+ let(:response) { double(parsed_body: {}) }
27
+
28
+ it 'returns nil' do
29
+ expect(subject.next_page).to be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '#previous_page' do
35
+ it 'returns previous page (1)' do
36
+ expect(subject.previous_page).to eq(1)
37
+ end
38
+
39
+ context 'when there is no previous page' do
40
+ let(:response) { double(parsed_body: {}) }
41
+
42
+ it 'returns nil' do
43
+ expect(subject.previous_page).to be_nil
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#total' do
49
+ it 'returns one thousand' do
50
+ expect(subject.total).to eq(1000)
51
+ end
52
+ end
53
+
54
+ describe "#to_a" do
55
+ it 'returns the collection as an array' do
56
+ expect(subject.to_a).to be_a(Array)
57
+ expect { subject.to_a.clear }.not_to change { subject.count }
58
+ end
59
+ end
60
+ end
61
+
@@ -128,7 +128,7 @@ describe Emites::Entities::Nfse do
128
128
 
129
129
  it_behaves_like "entity_attributes", [
130
130
  :id, :emitter_id, :taker, :rps_situation, :serie,
131
- :number, :rps_type, :emission_date,
131
+ :number, :nfse_number, :rps_type, :emission_date,
132
132
  :emission_date_nfse, :operation_nature, :other_informations,
133
133
  :competence, :special_regime, :status,
134
134
  :description, :send_nfse_taker, :service_values,
@@ -145,4 +145,4 @@ describe Emites::Entities::Nfse do
145
145
  end
146
146
  end
147
147
 
148
- end
148
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Emites::Params do
4
+ let(:permitted_params) { [:a, :b, :c] }
5
+ subject { Emites::Params.new(permitted_params) }
6
+
7
+ describe '#permit' do
8
+
9
+ it 'filters a hash with string keys' do
10
+ expect(Emites::Params.new({'a' => 1, 'b' => 2, 'd' => 3 }).permit(permitted_params)).to eq({ a: 1, b: 2 })
11
+ end
12
+
13
+ it 'filters a hash with symbol keys' do
14
+ expect(Emites::Params.new(a: 1, b: 2, d: 3).permit(permitted_params)).to eq({ a: 1, b: 2 })
15
+ end
16
+ end
17
+ end
@@ -39,20 +39,6 @@ describe Emites::Resources::Base do
39
39
  allow(Emites.configuration).to receive(:url).and_return("http://requestb.in")
40
40
  end
41
41
 
42
- describe "#parsed_body" do
43
- before do
44
- allow(http).to receive(:send_request).and_return(request)
45
- end
46
-
47
- it "does not raise an error" do
48
- expect{ subject.parseable }.not_to raise_error
49
- end
50
-
51
- it "response returns an empty hash" do
52
- expect(subject.parseable).to eq({})
53
- end
54
- end
55
-
56
42
  describe ".notify" do
57
43
  before do
58
44
  allow(http).to receive(:send_request).and_return(request)
@@ -93,4 +79,4 @@ describe Emites::Resources::Base do
93
79
  expect { subject.timeout }.to raise_error(Emites::RequestTimeout)
94
80
  end
95
81
  end
96
- end
82
+ end