my_data 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +32 -0
  5. data/Gemfile +15 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +39 -0
  8. data/Rakefile +12 -0
  9. data/bin/console +15 -0
  10. data/bin/setup +8 -0
  11. data/lib/my_data.rb +24 -0
  12. data/lib/my_data/client.rb +42 -0
  13. data/lib/my_data/resource.rb +191 -0
  14. data/lib/my_data/resources.rb +10 -0
  15. data/lib/my_data/resources/ecls.rb +7 -0
  16. data/lib/my_data/resources/ecls/expenses_classification_type.rb +7 -0
  17. data/lib/my_data/resources/ecls/invoice_expenses_classification_type.rb +7 -0
  18. data/lib/my_data/resources/ecls/invoices_expenses_classification_detail_type.rb +7 -0
  19. data/lib/my_data/resources/error_type.rb +8 -0
  20. data/lib/my_data/resources/icls.rb +7 -0
  21. data/lib/my_data/resources/icls/income_classification_type.rb +7 -0
  22. data/lib/my_data/resources/icls/invoice_income_classification_type.rb +7 -0
  23. data/lib/my_data/resources/icls/invoices_income_classification_detail_type.rb +7 -0
  24. data/lib/my_data/resources/inv.rb +14 -0
  25. data/lib/my_data/resources/inv/aade_book_invoice_type.rb +7 -0
  26. data/lib/my_data/resources/inv/address_type.rb +7 -0
  27. data/lib/my_data/resources/inv/invoice_header_type.rb +7 -0
  28. data/lib/my_data/resources/inv/invoice_row_type.rb +7 -0
  29. data/lib/my_data/resources/inv/invoice_summary_type.rb +7 -0
  30. data/lib/my_data/resources/inv/invoices_doc.rb +17 -0
  31. data/lib/my_data/resources/inv/party_type.rb +7 -0
  32. data/lib/my_data/resources/inv/payment_method_detail_type.rb +7 -0
  33. data/lib/my_data/resources/inv/ship_type.rb +7 -0
  34. data/lib/my_data/resources/inv/tax_totals_type.rb +7 -0
  35. data/lib/my_data/resources/response_type.rb +19 -0
  36. data/lib/my_data/resources/responses_doc.rb +8 -0
  37. data/lib/my_data/type_caster.rb +58 -0
  38. data/lib/my_data/version.rb +5 -0
  39. data/lib/my_data/xml_generator.rb +80 -0
  40. data/lib/my_data/xml_parser.rb +96 -0
  41. data/lib/my_data/xsd.rb +7 -0
  42. data/lib/my_data/xsd/complex_type.rb +24 -0
  43. data/lib/my_data/xsd/docs/InvoicesDoc-v1.0.2.xsd +1088 -0
  44. data/lib/my_data/xsd/docs/RequestedProviderDoc-v1.0.2.xsd +44 -0
  45. data/lib/my_data/xsd/docs/expensesClassification-v1.0.2.xsd +193 -0
  46. data/lib/my_data/xsd/docs/incomeClassification-v1.0.2.xsd +141 -0
  47. data/lib/my_data/xsd/docs/requestDoc-v1.0.2.xsd +66 -0
  48. data/lib/my_data/xsd/docs/requestedInvoicesDoc-v1.0.2.xsd +66 -0
  49. data/lib/my_data/xsd/docs/response-v1.0.2.xsd +80 -0
  50. data/lib/my_data/xsd/element.rb +49 -0
  51. data/lib/my_data/xsd/resource_generator.rb +37 -0
  52. data/lib/my_data/xsd/structure.rb +80 -0
  53. data/my_data.gemspec +36 -0
  54. metadata +153 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4eb6195b397b23da4c0756c94382ac9a245ea48ff07c7bcff7ce75d318750f55
4
+ data.tar.gz: 2443d4836cecf9d08657e998a39ca73cdcdc58d131463b805be2c84a4429ed53
5
+ SHA512:
6
+ metadata.gz: e3922efa99513f4222db05df50762d502252a99c0159ec11ae0ea35c2ceb5bd89612a54dac86d22e5ccaeca33f573aa58b7f011a00f7da09e65482bdd7e6693d
7
+ data.tar.gz: 0135fb723a5e2f759bd5cab73c91c31ef1ef3a47303e111a02593d9589403dc07756e9353e3f3de11becbe84c08ccad4031e06ed61fd9a03ded8c666c2b994aa
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .DS_Store
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ Metrics/ModuleLength:
2
+ Max: 200
3
+
4
+ Metrics/MethodLength:
5
+ Max: 15
6
+
7
+ Metrics/BlockLength:
8
+ ExcludedMethods:
9
+ - class_methods
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ Style/SymbolArray:
15
+ EnforcedStyle: brackets
16
+
17
+ Style/WordArray:
18
+ EnforcedStyle: brackets
19
+
20
+ Style/StringLiterals:
21
+ Enabled: true
22
+ EnforcedStyle: double_quotes
23
+
24
+ Style/ClassAndModuleChildren:
25
+ Enabled: false
26
+
27
+ Style/StringLiteralsInInterpolation:
28
+ Enabled: true
29
+ EnforcedStyle: double_quotes
30
+
31
+ Layout/LineLength:
32
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in my_data.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ group :development do
13
+ gem "pry"
14
+ gem "rubocop", "~> 0.80"
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Michalis Zamparas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # MyData
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/my_data`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'my_data'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install my_data
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mikezaby/my_data.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: [:spec, :rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "my_data"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/my_data.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+ require "active_support/all"
5
+ require "faraday"
6
+ require "nokogiri"
7
+
8
+ require_relative "my_data/version"
9
+
10
+ module MyData
11
+ class Error < StandardError; end
12
+
13
+ def self.root
14
+ File.expand_path '../..', __FILE__
15
+ end
16
+
17
+ autoload :Client, "my_data/client"
18
+ autoload :Resource, "my_data/resource"
19
+ autoload :Resources, "my_data/resources"
20
+ autoload :TypeCaster, "my_data/type_caster"
21
+ autoload :XmlGenerator, "my_data/xml_generator"
22
+ autoload :XmlParser, "my_data/xml_parser"
23
+ autoload :Xsd, "my_data/xsd"
24
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyData
4
+ class Client
5
+ BASE_URL = "https://mydata-dev.azure-api.net"
6
+
7
+ def initialize(user_id:, subscription_key:)
8
+ @user_id = user_id
9
+ @subscription_key = subscription_key
10
+ end
11
+
12
+ def connection
13
+ @connection ||= Faraday.new(BASE_URL) do |conn|
14
+ conn.headers = headers
15
+ end
16
+ end
17
+
18
+ def send_invoice(invoice_doc)
19
+ response = connection.post("SendInvoices", invoice_doc)
20
+
21
+ response_parser(response.body)
22
+ end
23
+
24
+ private
25
+
26
+ def response_parser(response)
27
+ fixed_xml = response.sub(/^.+/, "").sub("</string>", "").strip
28
+ .gsub("&lt;", "<").gsub("&gt;", ">")
29
+
30
+ Hash.from_xml(fixed_xml)
31
+ end
32
+
33
+ def headers
34
+ @headers ||= {
35
+ "Content-Type" => "application/xml",
36
+ "Accept" => "application/xml",
37
+ "aade-user-id" => @user_id,
38
+ "Ocp-Apim-Subscription-Key" => @subscription_key
39
+ }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,191 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyData
4
+ module Resource
5
+ extend ActiveSupport::Concern
6
+ include ActiveModel::Validations
7
+ include ActiveModel::Serializers::JSON
8
+
9
+ ALLOWED_ATTRIBUTE_OPTS = [
10
+ :class_name,
11
+ :collection,
12
+ :collection_element_name
13
+ ].freeze
14
+
15
+ included do
16
+ @attributes = []
17
+ @mappings = {}
18
+ @container = {
19
+ name: name.demodulize.camelize,
20
+ attributes: {}
21
+ }
22
+ end
23
+
24
+ class_methods do
25
+ attr_reader :attributes, :mappings, :container
26
+
27
+ def container_tag(name, opts = {})
28
+ @container[:name] = name
29
+ @container[:attributes] = opts
30
+ end
31
+
32
+ def xsd_structure
33
+ MyData::Xsd::Structure.resource_attributes(name).each do |attrs|
34
+ e_name, type, opts = attrs
35
+ required = opts.delete :required
36
+
37
+ attribute(e_name, type, opts)
38
+
39
+ validates_presence_of e_name if required
40
+ end
41
+ end
42
+
43
+ # @param name [String] the name of the attribute
44
+ # @param type [Symbol] the type of the attribute (:string, :integer, etc)
45
+ # @param opts [Hash] options for custom parsing
46
+ # @option class_name [String] the name of the resource
47
+ # @option collection [Boolean] when the required attribute is a collection
48
+ # @option collection_element_name [String] when we set collection_element_name
49
+ # we nested the elements of collection with that name
50
+ def attribute(name, type, opts = {})
51
+ name = name.to_s
52
+
53
+ raise "Wrong type: #{name}: #{type}" unless MyData::TypeCaster.valid_type?(type)
54
+
55
+ if opts.any? { |k, _| !ALLOWED_ATTRIBUTE_OPTS.include?(k) }
56
+ raise "Option not supported: #{name}: #{opts.keys - ALLOWED_ATTRIBUTE_OPTS}"
57
+ end
58
+
59
+ @attributes.push(name)
60
+ attr_mappings(name, type, opts)
61
+
62
+ attr_reader name
63
+
64
+ define_attr_setter name, collection: !!opts[:collection]
65
+ end
66
+
67
+ def inspect
68
+ format_attrs = attributes.map do |name|
69
+ mapping = mappings[name]
70
+ type = mapping[:type] == :resource ? mapping[:resource].name : mapping[:type]
71
+ type = mapping[:collection] ? "[#{type}]" : type
72
+
73
+ "#{name}: #{type}"
74
+ end
75
+
76
+ "#{name} #{format_attrs.join(", ")}"
77
+ end
78
+
79
+ def from_xml(doc_or_string)
80
+ doc = doc_or_string.is_a?(String) ? Nokogiri::XML(doc_or_string).child : doc_or_string
81
+ new(parse_xml(doc))
82
+ end
83
+
84
+ private
85
+
86
+ def attr_mappings(name, type, opts)
87
+ resource =
88
+ if type == :resource
89
+ klass_name = opts[:class_name].presence || name
90
+ "MyData::Resources::#{klass_name}".constantize
91
+ end
92
+
93
+ @mappings[name] = {
94
+ type: type,
95
+ resource: resource,
96
+ collection: opts[:collection],
97
+ collection_element_name: opts[:collection_element_name]
98
+ }
99
+ end
100
+
101
+ def define_attr_setter(name, collection: false)
102
+ define_method "#{name}=" do |value|
103
+ type, resource = self.class.mappings[name].values_at(:type, :resource)
104
+
105
+ type_casted_value =
106
+ if collection
107
+ value.map { |val| MyData::TypeCaster.cast(value: val, type: type, resource: resource) }
108
+ else
109
+ MyData::TypeCaster.cast(value: value, type: type, resource: resource)
110
+ end
111
+
112
+ instance_variable_set("@#{name}", type_casted_value)
113
+ end
114
+ end
115
+
116
+ def parse_xml(doc)
117
+ parser = MyData::XmlParser.new(doc, mappings, name)
118
+ parser.parse_xml
119
+ end
120
+ end
121
+
122
+ def initialize(attrs = {})
123
+ self.attributes = attrs
124
+ end
125
+
126
+ def attribute_names
127
+ @attribute_names ||= self.class.attributes
128
+ end
129
+
130
+ def attributes
131
+ attribute_names.reduce({}) do |hash, name|
132
+ hash.merge!(name => public_send(name))
133
+ end
134
+ end
135
+
136
+ def attributes=(attrs)
137
+ attrs.each do |key, value|
138
+ next unless attribute_names.include?(key.to_s)
139
+
140
+ public_send("#{key}=", value)
141
+ end
142
+
143
+ attributes
144
+ end
145
+
146
+ def resources
147
+ attributes.select { |k, _| self.class.mappings[k][:resource] }.to_h
148
+ end
149
+
150
+ def valid?
151
+ is_valid = super && resources.values.flatten.compact.map(&:valid?).all?
152
+
153
+ return true if is_valid
154
+
155
+ resources.compact.each do |k, resource|
156
+ rs = resource.is_a?(Array) ? resource : [resource]
157
+
158
+ next if rs.all?(&:valid?)
159
+
160
+ rs.each do |r|
161
+ errors.add(k, :invalid_resource, message: r.errors.full_messages.join(", "))
162
+ end
163
+ end
164
+
165
+ false
166
+ end
167
+
168
+ def inspect
169
+ "#{self.class}(#{as_json.to_s.gsub(/^{|}$/, "")})"
170
+ end
171
+
172
+ def serializable_hash(*args)
173
+ hash = super
174
+
175
+ if block_given?
176
+ hash.each { |key, value| hash[key] = yield(key, value) }
177
+ else
178
+ hash.each do |key, value|
179
+ hash[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
180
+ end
181
+ end
182
+
183
+ hash
184
+ end
185
+
186
+ def to_xml
187
+ generator = MyData::XmlGenerator.new(self)
188
+ generator.to_xml
189
+ end
190
+ end
191
+ end