faraday-xml 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 777d8a0d7887223960c5ec14c5be37e7ceb50a8c7cd0abf33a070330f8c57a6d
4
+ data.tar.gz: de298ca9f99ff381b232996351d33bcbc72ff8343d3ee8f8ab24cfdaf0f15c32
5
+ SHA512:
6
+ metadata.gz: 1933ead1a4cf3a3de25e773c2e3787df00962ae2039a0182051b2dea3fee6abe029901ca3129b66fdf23129f65b309731061a8f3cd2af860e6c134e24e3dfe69
7
+ data.tar.gz: 8dd8bcb901bbdcede8df0e077ffc20a1db75d2b9cb78cc973b9a5dbcb26ccd64b7721792c4ee91f2cda4753402a633e32fe16cd7706b15de584e697f0bdd0735
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ * Initial release.
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Benjamin Fleischer
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,54 @@
1
+ # Faraday XML
2
+
3
+ [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/gemhome/faraday-xml/ci)](https://github.com/gemhome/faraday-xml/actions?query=branch%3Amain)
4
+ [![Gem](https://img.shields.io/gem/v/faraday-xml.svg?style=flat-square)](https://rubygems.org/gems/faraday-xml)
5
+ [![License](https://img.shields.io/github/license/gemhome/faraday-xml.svg?style=flat-square)](LICENSE.md)
6
+
7
+ Faraday XML Middleware.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'faraday-xml'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```shell
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```shell
26
+ gem install faraday-xml
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ require 'faraday/xml'
33
+
34
+ # TODO
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies.
40
+
41
+ Then, run `bin/test` to run the tests.
42
+
43
+ To install this gem onto your local machine, run `rake build`.
44
+
45
+ To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run `rake release`.
46
+ See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/gemhome/faraday-xml).
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module XML
5
+ # Parse response bodies as XML
6
+ class Response < Faraday::Middleware
7
+ def initialize(app = nil, parser_options: nil, content_type: /\bxml$/, preserve_raw: false)
8
+ super(app)
9
+ @parser_options = parser_options
10
+ @content_types = Array(content_type)
11
+ @preserve_raw = preserve_raw
12
+ end
13
+
14
+ # @param env [Faraday::Env] the environment of the response being processed.
15
+ def on_complete(env)
16
+ process_response(env) if parse_response?(env)
17
+ end
18
+
19
+ private
20
+
21
+ def process_response(env)
22
+ env[:raw_body] = env[:body] if @preserve_raw
23
+ env[:body] = parse(env[:body])
24
+ rescue StandardError, SyntaxError => e
25
+ raise Faraday::ParsingError.new(e, env[:response])
26
+ end
27
+
28
+ def parse(body)
29
+ return nil if body.strip.empty?
30
+
31
+ parser.call(body, @parser_options || {})
32
+ end
33
+
34
+ def test_parser
35
+ parse("<success>true</success>")
36
+ end
37
+
38
+ def parser # rubocop:disable Metrics/MethodLength
39
+ @parser ||= nil
40
+ if @parser.nil?
41
+ @parser = set_parser
42
+ @parser && test_parser
43
+ end
44
+ @parser or raise 'Missing dependencies ActiveSupport::XmlMini or MultiXml'
45
+ end
46
+
47
+ def set_parser
48
+ @parser ||=
49
+ begin
50
+ require 'multi_xml'
51
+ lambda do |xml, options|
52
+ ::MultiXml.parse(xml, options)
53
+ end
54
+ rescue LoadError # rubocop:disable Lint/SuppressedException
55
+ end
56
+ @parser ||=
57
+ begin
58
+ require 'active_support'
59
+ # require 'active_support/isolated_execution_state'
60
+ require 'active_support/xml_mini'
61
+ require 'active_support/core_ext/hash/conversions'
62
+ require 'active_support/core_ext/array/conversions'
63
+ lambda do |xml, options|
64
+ disallowed_types = options[:disallowed_types]
65
+ Hash.from_xml(xml, disallowed_types)
66
+ end
67
+ rescue LoadError # rubocop:disable Lint/SuppressedException
68
+ end
69
+ end
70
+
71
+ def parse_response?(env)
72
+ process_response_type?(env) &&
73
+ env[:body].respond_to?(:to_str)
74
+ end
75
+
76
+ def process_response_type?(env)
77
+ type = response_type(env)
78
+ @content_types.empty? || @content_types.any? do |pattern|
79
+ pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern
80
+ end
81
+ end
82
+
83
+ def response_type(env)
84
+ type = env[:response_headers][CONTENT_TYPE].to_s
85
+ type = type.split(';', 2).first if type.index(';')
86
+ type
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module XML
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'xml/response'
4
+ require_relative 'xml/version'
5
+
6
+ module Faraday
7
+ # The Faraday::XML middleware main module
8
+ module XML
9
+ # Load middleware with
10
+ # conn.response Faraday::XML::Response
11
+ # or
12
+ # conn.response :xml
13
+ Faraday::Response.register_middleware(xml: Faraday::XML::Response)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Fleischer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.10'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '13.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.21.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.21.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 1.31.0
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 1.31.0
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-packaging
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.5.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.5.0
117
+ - !ruby/object:Gem::Dependency
118
+ name: rubocop-performance
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '1.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '1.0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rubocop-rspec
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '2.0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '2.0'
145
+ description: 'Faraday XML Middleware.
146
+
147
+ '
148
+ email:
149
+ - github@benjaminfleischer.com
150
+ executables: []
151
+ extensions: []
152
+ extra_rdoc_files: []
153
+ files:
154
+ - CHANGELOG.md
155
+ - LICENSE.md
156
+ - README.md
157
+ - lib/faraday/xml.rb
158
+ - lib/faraday/xml/response.rb
159
+ - lib/faraday/xml/version.rb
160
+ homepage: https://github.com/gemhome/faraday-xml
161
+ licenses:
162
+ - MIT
163
+ metadata:
164
+ bug_tracker_uri: https://github.com/gemhome/faraday-xml/issues
165
+ changelog_uri: https://github.com/gemhome/faraday-xml/blob/v0.0.1/CHANGELOG.md
166
+ documentation_uri: http://www.rubydoc.info/gems/faraday-xml/0.0.1
167
+ homepage_uri: https://github.com/gemhome/faraday-xml
168
+ rubygems_mfa_required: 'true'
169
+ source_code_uri: https://github.com/gemhome/faraday-xml
170
+ wiki_uri: https://github.com/gemhome/faraday-xml/wiki
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '2.6'
180
+ - - "<"
181
+ - !ruby/object:Gem::Version
182
+ version: '4'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubygems_version: 3.3.7
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Faraday XML Middleware
193
+ test_files: []