facturapi_ruby 1.2.0 → 1.3.0

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
  SHA256:
3
- metadata.gz: 44bc1c3980c79bd5b1536125672fc3b8279e195e9bcae12cd61d4a6e3c128e30
4
- data.tar.gz: 0e1674d9d285d8b0a75893a1b3d9c3129e1530c35498231763fada9154810586
3
+ metadata.gz: b58fed997b3fb3da051f518146bc46b1aa982b874f771e0890870b8578dabb61
4
+ data.tar.gz: a6ad129cffa87123e86e36e7b38a53c99e9e30ba3a5b024d77c530e21847ec7f
5
5
  SHA512:
6
- metadata.gz: 7b935fdc31cb3d4508ef0e866b1d45fe8037df41522a80784ff59147c2416345da4c2937c169d4e066c94adcb3d788615c49efa56caaecba50bda959a869a785
7
- data.tar.gz: 3c18c219b7c344af1788a58785cc4c258342ba00988af859498f93b2f874e052f95397c2cdff7f3054f06c6a4435812e811643da025cd1fe71c1bb6744ce7a94
6
+ metadata.gz: f7f2b0e62be8433c3cd944fa693829fe0ff495901cc38a8ef73dd8e0d27689f4f8d7b022ae71877a15972dd4d2f628f96655e90e68ec69cb0b6f31440cb65989
7
+ data.tar.gz: b7d7e814f06a68fbc3a3b779e9dda13d5869183570ce1c72e3a011a281473300d9f86178b6389a4719bbbdf9e5e3108885754d896fa2104009107d41752a3348
@@ -1,9 +1,10 @@
1
1
  module FacturapiRuby
2
2
  class Configuration
3
- attr_accessor :api_key
3
+ attr_accessor :api_key, :debug_output
4
4
 
5
5
  def initialize
6
6
  @api_key = ''
7
+ @debug_output = true
7
8
  end
8
9
  end
9
10
  end
@@ -17,38 +17,50 @@ module FacturapiRuby
17
17
  BASE_URL = 'https://www.facturapi.io/v1'
18
18
 
19
19
  def get_file(options)
20
- uri = URI(BASE_URL + options[:endpoint])
21
-
22
- http = Net::HTTP.new(uri.host, uri.port)
23
- http.use_ssl = true
24
- request = Net::HTTP::Get.new(uri.request_uri)
25
- request.basic_auth(FacturapiRuby.configuration.api_key, '')
20
+ response = request(Net::HTTP::Get, options)
26
21
 
27
- response = http.request(request)
28
22
  file = Tempfile.open(['my', options[:file_ext]])
29
23
  file.binmode
30
24
  file.write(response.body)
31
25
 
32
- if response.code == '200'
26
+ if response.code.start_with? '20'
33
27
  file
34
28
  else
35
29
  raise FacturapiRubyError.new(JSON.parse(response.body))
36
30
  end
37
31
  end
38
32
 
33
+ def get(options)
34
+ response = request(Net::HTTP::Get, options)
35
+
36
+ json_response(response)
37
+ end
38
+
39
39
  def post(options)
40
- uri = URI(BASE_URL + options[:endpoint])
40
+ response = request(Net::HTTP::Post, options) do |request|
41
+ request.body = options[:api_options].to_json
42
+ end
43
+
44
+ json_response(response)
45
+ end
41
46
 
42
- http = Net::HTTP.new(uri.host, uri.port)
43
- http.use_ssl = true
47
+ def put(options)
48
+ response = request(Net::HTTP::Put, options) do |request|
49
+ request.body = options[:api_options].to_json
50
+ end
44
51
 
45
- request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
46
- request.body = options[:api_options].to_json
47
- request.basic_auth(FacturapiRuby.configuration.api_key, '')
52
+ json_response(response)
53
+ end
48
54
 
49
- http.set_debug_output($stdout)
55
+ def delete(options)
56
+ response = request(Net::HTTP::Delete, options)
50
57
 
51
- response = http.request(request)
58
+ json_response(response)
59
+ end
60
+
61
+ private
62
+
63
+ def json_response(response)
52
64
  if response.code.start_with? '20'
53
65
  JSON.parse(response.body)
54
66
  else
@@ -56,24 +68,22 @@ module FacturapiRuby
56
68
  end
57
69
  end
58
70
 
59
- def put(options)
71
+ def request(request_method, options)
60
72
  uri = URI(BASE_URL + options[:endpoint])
61
73
 
62
74
  http = Net::HTTP.new(uri.host, uri.port)
63
75
  http.use_ssl = true
64
76
 
65
- request = Net::HTTP::Put.new(uri.request_uri, 'Content-Type' => 'application/json')
66
- request.body = options[:api_options].to_json
77
+ request = request_method.new(uri.request_uri, 'Content-Type' => 'application/json')
78
+ # request.body = options[:api_options].to_json
79
+
80
+ yield(request) if block_given?
81
+
67
82
  request.basic_auth(FacturapiRuby.configuration.api_key, '')
68
83
 
69
- http.set_debug_output($stdout)
84
+ http.set_debug_output($stdout) if FacturapiRuby.configuration.debug_output
70
85
 
71
- response = http.request(request)
72
- if response.code.start_with? '20'
73
- JSON.parse(response.body)
74
- else
75
- raise FacturapiRubyError.new(JSON.parse(response.body))
76
- end
86
+ http.request(request)
77
87
  end
78
88
  end
79
89
  end
@@ -9,6 +9,20 @@ module FacturapiRuby
9
9
  api_options: options
10
10
  )
11
11
  end
12
+
13
+ def cancel(invoice_id, options={})
14
+ HttpClient.delete(
15
+ endpoint: "/invoices/#{invoice_id}",
16
+ api_options: options
17
+ )
18
+ end
19
+
20
+ def get(invoice_id, options={})
21
+ HttpClient.get(
22
+ endpoint: "/invoices/#{invoice_id}",
23
+ api_options: options
24
+ )
25
+ end
12
26
  end
13
27
  end
14
28
  end
@@ -1,3 +1,3 @@
1
1
  module FacturapiRuby
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facturapi_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Llamas Velasco
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-11 00:00:00.000000000 Z
11
+ date: 2019-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler