fakturan_nu 1.0.0 → 1.0.1

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: f81bdfd634d37068875813a2c43c877ed859b4fb
4
- data.tar.gz: 666f324bd953f0a55e9e6af4e8bbf70ad7405036
3
+ metadata.gz: bb9d1638af2349a26125ce41a43cdcd176c9c49b
4
+ data.tar.gz: 84d469e7d0c44d3cac4a38dfda966275b7efd8d1
5
5
  SHA512:
6
- metadata.gz: c3591f8e8cb7cc5604029feec9e3a91b9e42a09fe302a97f8fe89aec1086abad8d30dafe9adee1163d330aeb7868210a3f9cc293af5ca9540b666f105e89194a
7
- data.tar.gz: 4bd0783fff70fe74a037ee80f5a4e8df43873b6d4bb35e249775255d6cc42f789ec613386bd231d47625cd9be45c2d306d0c3d43f19b77b2c4c8d1c31738df59
6
+ metadata.gz: cc1cfd64e2e9d34e17897abf430f65623341447bd5801d0fbff3efdb1bc93582b234219e714e292db05ea922996cc635f7a9c2dd957f18a4edc345f980ce3060
7
+ data.tar.gz: 6850b0e9310bc6df2ef5843908e089e3c30c7b93638fea2b101216d9776a98f9dc0063430de1aa7ca65c7a33081f8ec9c42a94db65a0fb97a076b95465939ef5
data/README.md CHANGED
@@ -52,14 +52,20 @@ product.save
52
52
 
53
53
  ### Invoices
54
54
 
55
- For creating invoices, a date and a client is required:
55
+ For creating invoices, a client is required. It can be an existing client (by using ```client_id```), or a new one (by using ```client```):
56
+
56
57
  ```ruby
57
- invoice = Fakturan::Invoice.create(date: Date.today, client: { company: "Acme Inc" })
58
+ invoice = Fakturan::Invoice.create(client: { company: "Acme Inc" })
58
59
  # POST "https://fakturan.nu/api/v2/invoices" # Will create a new client + invoice
59
60
 
60
- invoice = Fakturan::Invoice.create(date: Date.today, client_id: 1)
61
+ invoice = Fakturan::Invoice.create(client_id: 1)
61
62
  # POST "https://fakturan.nu/api/v2/invoices" # Will create a new invoice for client with id: 1
62
63
 
64
+ ```
65
+ Example with items/rows:
66
+ ```ruby
67
+ invoice = Fakturan::Invoice.create(client_id: 1, rows: [{ product_name: "Shoes", product_unit: "pairs", amount: "1", product_price: "500"}])
68
+ # POST "https://fakturan.nu/api/v2/invoices" # Will create a new invoice for client with id: 1
63
69
  ```
64
70
 
65
71
  ### Available resources and properties
@@ -113,7 +119,7 @@ These errors are all descendents of ```Fakturan::Error``` and should be caught i
113
119
 
114
120
  ```ruby
115
121
  begin
116
- invoice = Fakturan::Invoice.create!
122
+ invoice = Fakturan::Invoice.create!(invoice_params)
117
123
  rescue Fakturan::Error => error
118
124
  render plain: error.message, status: error.status
119
125
  end
data/fakturan_nu.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'fakturan_nu'
4
- s.version = '1.0.0'
4
+ s.version = '1.0.1'
5
5
  s.date = '2014-12-17'
6
6
  s.summary = 'A ruby client for the Fakturan.nu - API'
7
7
  s.description = 'A ruby client for the Fakturan.nu - API. Fakturan.nu is a webbapp for billing.'
data/lib/fakturan_nu.rb CHANGED
@@ -4,6 +4,7 @@ require 'spyke'
4
4
  require 'faraday_middleware'
5
5
  require 'multi_json'
6
6
  require 'fakturan_nu/spyke_extensions'
7
+ require 'fakturan_nu/middleware/logger'
7
8
  require 'fakturan_nu/middleware/parse_json'
8
9
  require 'fakturan_nu/middleware/raise_error'
9
10
 
@@ -20,13 +21,12 @@ module Fakturan
20
21
  @use_sandbox = false
21
22
  @api_version = 2
22
23
 
23
- mattr_accessor :accept_language
24
+ mattr_accessor :wire_dump
24
25
 
25
26
  def self.setup username = nil, pass = nil
26
27
  #self.parse_json_times = true
27
28
 
28
29
  @username, @pass = username, pass
29
- self.accept_language = 'en-US'
30
30
 
31
31
  @connection = Faraday.new(url: build_url) do |connection|
32
32
  # Request
@@ -37,6 +37,9 @@ module Fakturan
37
37
  connection.use Fakturan::Response::ParseJSON
38
38
  connection.use Fakturan::Response::RaiseError
39
39
 
40
+ # Logger
41
+ connection.use Fakturan::Logger, ::Logger.new(STDOUT), :bodies => true
42
+
40
43
  # Adapter should be last
41
44
  connection.adapter Faraday.default_adapter
42
45
  end
@@ -60,7 +63,7 @@ module Fakturan
60
63
  end
61
64
 
62
65
  def self.use_sandbox=(true_or_false)
63
- @use_sandbox = true_or_false
66
+ @use_sandbox = self.wire_dump = true_or_false
64
67
  rebuild_url
65
68
  end
66
69
 
@@ -0,0 +1,65 @@
1
+ require 'forwardable'
2
+
3
+ module Fakturan
4
+ class Logger < Faraday::Response::Middleware
5
+ extend Forwardable
6
+
7
+ DEFAULT_OPTIONS = { :bodies => false }
8
+
9
+ def initialize(app, logger = nil, options = {})
10
+ super(app)
11
+ @logger = logger || begin
12
+ require 'logger'
13
+ ::Logger.new(STDOUT)
14
+ end
15
+ @options = DEFAULT_OPTIONS.merge(options)
16
+ end
17
+
18
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
19
+
20
+ #ErrorStatuses = 400...600
21
+
22
+ def call(env)
23
+ if Fakturan.wire_dump
24
+ info "#{env.method} #{env.url.to_s}"
25
+ debug('request') { dump_headers env.request_headers }
26
+ debug('request') { dump_body(env[:body]) } if env[:body] && log_body?(:request)
27
+ end
28
+ super
29
+ end
30
+
31
+ def on_complete(env)
32
+ if Fakturan.wire_dump
33
+ info('Status') { env.status.to_s }
34
+ #debug('response') { dump_headers env.response_headers }
35
+ debug('response') { dump_body env[:body] } if env[:body] && log_body?(:response)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def dump_headers(headers)
42
+ headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
43
+ end
44
+
45
+ def dump_body(body)
46
+ if body.respond_to?(:to_str)
47
+ body.to_str
48
+ else
49
+ pretty_inspect(body)
50
+ end
51
+ end
52
+
53
+ def pretty_inspect(body)
54
+ require 'pp' unless body.respond_to?(:pretty_inspect)
55
+ body.pretty_inspect
56
+ end
57
+
58
+ def log_body?(type)
59
+ case @options[:bodies]
60
+ when Hash then @options[:bodies][type]
61
+ else @options[:bodies]
62
+ end
63
+ end
64
+ end
65
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakturan_nu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Bourque Olivegren
@@ -194,6 +194,7 @@ files:
194
194
  - lib/fakturan_nu/client.rb
195
195
  - lib/fakturan_nu/error.rb
196
196
  - lib/fakturan_nu/invoice.rb
197
+ - lib/fakturan_nu/middleware/logger.rb
197
198
  - lib/fakturan_nu/middleware/parse_json.rb
198
199
  - lib/fakturan_nu/middleware/raise_error.rb
199
200
  - lib/fakturan_nu/product.rb