sendregning 0.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sendregning
4
+ class InvoiceParser
5
+ class << self
6
+ def parse(response, invoice = Sendregning::Invoice.new)
7
+ new(response, invoice).parse
8
+ end
9
+ end
10
+
11
+ def initialize(response, invoice)
12
+ @response = response
13
+ @invoice = invoice
14
+ end
15
+
16
+ def parse
17
+ attributes = @response["invoices"]["invoice"]
18
+
19
+ # Flatten optional and shipment attributes
20
+ %w[optional shipment].each do |section|
21
+ if attributes.key?(section)
22
+ attributes = attributes.merge(attributes[section])
23
+ attributes.delete(section)
24
+ end
25
+ end
26
+
27
+ lines = attributes["lines"]["line"]
28
+ attributes.delete("lines")
29
+
30
+ @invoice.update(stringify_hash(attributes))
31
+ @invoice.lines = lines.map do |l|
32
+ Sendregning::Line.new(stringify_hash(l))
33
+ end
34
+ @invoice
35
+ end
36
+
37
+ protected
38
+
39
+ def stringify_hash(hash)
40
+ new_hash = {}
41
+ hash.each do |key, value|
42
+ new_hash[key] = value.to_s
43
+ end
44
+ new_hash
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sendregning
4
+ class InvoiceSerializer < Sendregning::XmlSerializer
5
+ def build
6
+ builder.invoices do |invoices|
7
+ invoices.invoice do |invoice|
8
+ invoice.name item.name
9
+ invoice.zip item.zip
10
+ invoice.city item.city
11
+
12
+ lines(invoice)
13
+ optional_attributes(invoice)
14
+ shipment_attributes(invoice)
15
+ end
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def lines(invoice)
22
+ if item.lines.any?
23
+ invoice.lines do |line_builder|
24
+ item.lines.each { |l| l.to_xml(builder: line_builder) }
25
+ end
26
+ end
27
+ end
28
+
29
+ def optional_attributes(invoice)
30
+ if item.optional.any?
31
+ invoice.optional do |optional|
32
+ item.optional.each do |key, value|
33
+ key = key.to_sym
34
+ if value.is_a?(Date) || value.is_a?(Time)
35
+ value = value.strftime("%d.%m.%y")
36
+ end
37
+ optional.tag! key, value
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def shipment_attributes(invoice)
44
+ if item.shipment.any?
45
+ invoice.shipment do |shipment|
46
+ shipment.text! item.shipment_mode
47
+ item.shipment.each do |key, values|
48
+ key = key.to_sym
49
+ next if key == :shipment
50
+
51
+ shipment.tag! key do |emails|
52
+ Array(values).each { |v| emails.email v }
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,59 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Sendregning
4
+ class Line
5
+ # Quantity, as decimal. Example: '7,5'
6
+ attr_accessor :qty
7
+
8
+ # Product code. String, max length: 9
9
+ attr_accessor :prodCode
10
+
11
+ # Description. String, max length: 75
12
+ attr_accessor :desc
13
+
14
+ # Price per unit in decimal. Example: '250,00'
15
+ attr_accessor :unitPrice
16
+
17
+ # Discount in percentage as decimal. Optional, defaults to 0
18
+ attr_accessor :discount
19
+
20
+ # Tax rate. Valid rates are: '0', '8', '13' and '25'. Default: '25'
21
+ attr_accessor :tax
22
+
23
+ attr_accessor :itemNo, :lineTaxAmount, :lineTotal
24
+
25
+ def initialize(new_attributes = {})
26
+ self.attributes = new_attributes
27
+ end
28
+
29
+ # Renders line to XML
30
+ def to_xml(options = {})
31
+ LineSerializer.build(self, options)
32
+ end
33
+
34
+ protected
2
35
 
3
- class Line
4
-
5
- # Quantity, as decimal. Example: '7,5'
6
- attr_accessor :qty
7
-
8
- # Product code. String, max length: 9
9
- attr_accessor :prodCode
10
-
11
- # Description. String, max length: 75
12
- attr_accessor :desc
13
-
14
- # Price per unit in decimal. Example: '250,00'
15
- attr_accessor :unitPrice
16
-
17
- # Discount in percentage as decimal. Optional, defaults to 0
18
- attr_accessor :discount
19
-
20
- # Tax rate. Valid rates are: '0', '8', '13' and '25'. Default: '25'
21
- attr_accessor :tax
22
-
23
- attr_accessor :itemNo, :lineTaxAmount, :lineTotal
24
-
25
- def initialize(new_attributes={})
26
- self.attributes = new_attributes
27
- end
28
-
29
- # Renders line to XML
30
- def to_xml(options={})
31
- if options[:builder]
32
- xml = options[:builder]
33
- else
34
- xml = Builder::XmlMarkup.new(:indent=>2)
35
- xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
36
- end
37
- xml.line do |line|
38
- line.qty self.qty if @qty
39
- line.prodCode self.prodCode if @prodCode
40
- line.desc self.desc if @desc
41
- line.unitPrice self.unitPrice if @unitPrice
42
- line.discount self.discount if @discount
43
- line.tax self.tax if @tax
44
- end
45
- end
46
-
47
- protected
48
-
49
- def attributes=(attributes={})
50
- attributes.each do |key, value|
51
- set_method = "#{key.to_s}=".to_sym
52
- self.send(set_method, value) if self.respond_to?(set_method)
53
- end
54
- attributes
55
- end
56
-
57
- end
58
-
59
- end
36
+ def attributes=(attributes = {})
37
+ attributes.each do |key, value|
38
+ set_method = "#{key}=".to_sym
39
+ send(set_method, value) if respond_to?(set_method)
40
+ end
41
+ attributes
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sendregning
4
+ class LineSerializer < Sendregning::XmlSerializer
5
+ def build
6
+ builder.line do |line|
7
+ line.qty item.qty if item.qty
8
+ line.prodCode item.prodCode if item.prodCode
9
+ line.desc item.desc if item.desc
10
+ line.unitPrice item.unitPrice if item.unitPrice
11
+ line.discount item.discount if item.discount
12
+ line.tax item.tax if item.tax
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sendregning
4
+ VERSION = "0.2.2" unless Sendregning.const_defined?("VERSION")
5
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sendregning
4
+ class XmlSerializer
5
+ class << self
6
+ def build(item, options = {})
7
+ builder = options[:builder] || xml_builder
8
+ new(item, builder).build
9
+ end
10
+
11
+ protected
12
+
13
+ def xml_builder
14
+ xml = Builder::XmlMarkup.new(indent: 2)
15
+ xml.instruct! :xml, version: "1.0", encoding: "UTF-8"
16
+ xml
17
+ end
18
+ end
19
+
20
+ def initialize(item, builder)
21
+ @item = item
22
+ @builder = builder
23
+ end
24
+
25
+ def build
26
+ raise("XmlSerializer is an abstract class, subclass and overwrite " \
27
+ "the build method")
28
+ end
29
+
30
+ protected
31
+
32
+ attr_reader :builder
33
+
34
+ attr_reader :item
35
+ end
36
+ end
@@ -1,63 +1,29 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
4
+ require "sendregning/version"
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{sendregning}
8
- s.version = "0.1.1"
7
+ s.name = "sendregning"
8
+ s.version = Sendregning::VERSION
9
+ s.summary = "Ruby client for the SendRegning Web Service"
10
+ s.description = "Ruby client for the SendRegning Web Service"
11
+ s.authors = ["Inge Jørgensen"]
12
+ s.email = "inge@elektronaut.no"
13
+ s.homepage = "https://github.com/elektronaut/sendregning"
14
+ s.license = "MIT"
15
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.3")
9
16
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Inge J\303\270rgensen"]
12
- s.date = %q{2011-01-04}
13
- s.description = %q{Ruby client for the SendRegning Web Service}
14
- s.email = %q{inge@manualdesign.no}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "lib/sendregning.rb",
27
- "lib/sendregning/client.rb",
28
- "lib/sendregning/invoice.rb",
29
- "lib/sendregning/line.rb",
30
- "sendregning.gemspec",
31
- "test/helper.rb",
32
- "test/test_sendregning.rb"
33
- ]
34
- s.homepage = %q{http://github.com/elektronaut/sendregning}
35
- s.rdoc_options = ["--charset=UTF-8"]
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map do |f|
20
+ File.basename(f)
21
+ end
36
22
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.3.7}
38
- s.summary = %q{Ruby client for the SendRegning Web Service}
39
- s.test_files = [
40
- "test/helper.rb",
41
- "test/test_sendregning.rb"
42
- ]
43
23
 
44
- if s.respond_to? :specification_version then
45
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
- s.specification_version = 3
24
+ s.add_runtime_dependency "builder", "~> 3.1"
25
+ s.add_runtime_dependency "httmultiparty", "~> 0.3"
26
+ s.add_runtime_dependency "httparty", "~> 0.13"
47
27
 
48
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
- s.add_runtime_dependency(%q<httparty>, [">= 0.6.1"])
50
- s.add_runtime_dependency(%q<builder>, [">= 2.1.2"])
51
- s.add_runtime_dependency(%q<multipart-post>, [">= 1.0.1"])
52
- else
53
- s.add_dependency(%q<httparty>, [">= 0.6.1"])
54
- s.add_dependency(%q<builder>, [">= 2.1.2"])
55
- s.add_dependency(%q<multipart-post>, [">= 1.0.1"])
56
- end
57
- else
58
- s.add_dependency(%q<httparty>, [">= 0.6.1"])
59
- s.add_dependency(%q<builder>, [">= 2.1.2"])
60
- s.add_dependency(%q<multipart-post>, [">= 1.0.1"])
61
- end
28
+ s.add_development_dependency "rake"
62
29
  end
63
-
metadata CHANGED
@@ -1,128 +1,115 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sendregning
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
11
5
  platform: ruby
12
- authors:
13
- - "Inge J\xC3\xB8rgensen"
6
+ authors:
7
+ - Inge Jørgensen
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-01-04 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: httparty
11
+ date: 2020-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: builder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
23
21
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 5
30
- segments:
31
- - 0
32
- - 6
33
- - 1
34
- version: 0.6.1
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httmultiparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
35
34
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: builder
39
35
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 15
46
- segments:
47
- - 2
48
- - 1
49
- - 2
50
- version: 2.1.2
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
51
48
  type: :runtime
52
- version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: multipart-post
55
49
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
59
  - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 21
62
- segments:
63
- - 1
64
- - 0
65
- - 1
66
- version: 1.0.1
67
- type: :runtime
68
- version_requirements: *id003
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
69
  description: Ruby client for the SendRegning Web Service
70
- email: inge@manualdesign.no
70
+ email: inge@elektronaut.no
71
71
  executables: []
72
-
73
72
  extensions: []
74
-
75
- extra_rdoc_files:
76
- - LICENSE
77
- - README.rdoc
78
- files:
79
- - .document
80
- - .gitignore
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rubocop.yml"
77
+ - ".rubocop_todo.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
81
80
  - LICENSE
82
- - README.rdoc
83
- - Rakefile
84
- - VERSION
81
+ - README.md
85
82
  - lib/sendregning.rb
86
83
  - lib/sendregning/client.rb
87
84
  - lib/sendregning/invoice.rb
85
+ - lib/sendregning/invoice_parser.rb
86
+ - lib/sendregning/invoice_serializer.rb
88
87
  - lib/sendregning/line.rb
88
+ - lib/sendregning/line_serializer.rb
89
+ - lib/sendregning/version.rb
90
+ - lib/sendregning/xml_serializer.rb
89
91
  - sendregning.gemspec
90
- - test/helper.rb
91
- - test/test_sendregning.rb
92
- has_rdoc: true
93
- homepage: http://github.com/elektronaut/sendregning
94
- licenses: []
95
-
92
+ homepage: https://github.com/elektronaut/sendregning
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
96
  post_install_message:
97
- rdoc_options:
98
- - --charset=UTF-8
99
- require_paths:
97
+ rdoc_options: []
98
+ require_paths:
100
99
  - lib
101
- required_ruby_version: !ruby/object:Gem::Requirement
102
- none: false
103
- requirements:
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
104
102
  - - ">="
105
- - !ruby/object:Gem::Version
106
- hash: 3
107
- segments:
108
- - 0
109
- version: "0"
110
- required_rubygems_version: !ruby/object:Gem::Requirement
111
- none: false
112
- requirements:
103
+ - !ruby/object:Gem::Version
104
+ version: 1.9.3
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
113
107
  - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- version: "0"
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
119
110
  requirements: []
120
-
121
- rubyforge_project:
122
- rubygems_version: 1.3.7
111
+ rubygems_version: 3.1.2
123
112
  signing_key:
124
- specification_version: 3
113
+ specification_version: 4
125
114
  summary: Ruby client for the SendRegning Web Service
126
- test_files:
127
- - test/helper.rb
128
- - test/test_sendregning.rb
115
+ test_files: []