Skip to content

Commit 3826796

Browse files
Add faraday-encoding to correctly set the response body encoding (#65)
* Fix response encoding * rubocop Co-authored-by: Chris Nelson <git@chrisnelson.io>
1 parent 9f9b29f commit 3826796

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

Gemfile.lock

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,36 @@ PATH
44
xpm_ruby (0.1.0)
55
activesupport
66
builder
7-
dry-types
7+
dry-types (~> 0.15.0)
88
faraday
9+
faraday-encoding
910
ox (~> 2.13)
1011

1112
GEM
1213
remote: https://rubygems.org/
1314
specs:
14-
activesupport (6.0.2.2)
15+
activesupport (6.1.4)
1516
concurrent-ruby (~> 1.0, >= 1.0.2)
16-
i18n (>= 0.7, < 2)
17-
minitest (~> 5.1)
18-
tzinfo (~> 1.1)
19-
zeitwerk (~> 2.2)
17+
i18n (>= 1.6, < 2)
18+
minitest (>= 5.1)
19+
tzinfo (~> 2.0)
20+
zeitwerk (~> 2.3)
2021
ast (2.4.0)
2122
builder (3.2.4)
2223
byebug (11.1.3)
2324
coderay (1.1.2)
24-
concurrent-ruby (1.1.6)
25+
concurrent-ruby (1.1.9)
2526
diff-lcs (1.3)
26-
dry-configurable (0.11.5)
27+
dry-configurable (0.12.1)
2728
concurrent-ruby (~> 1.0)
28-
dry-core (~> 0.4, >= 0.4.7)
29-
dry-equalizer (~> 0.2)
30-
dry-container (0.7.2)
29+
dry-core (~> 0.5, >= 0.5.0)
30+
dry-container (0.8.0)
3131
concurrent-ruby (~> 1.0)
3232
dry-configurable (~> 0.1, >= 0.1.3)
33-
dry-core (0.4.9)
33+
dry-core (0.7.1)
3434
concurrent-ruby (~> 1.0)
3535
dry-equalizer (0.3.0)
36-
dry-inflector (0.2.0)
36+
dry-inflector (0.2.1)
3737
dry-logic (0.6.1)
3838
concurrent-ruby (~> 1.0)
3939
dry-core (~> 0.2)
@@ -45,15 +45,34 @@ GEM
4545
dry-equalizer (~> 0.2, >= 0.2.2)
4646
dry-inflector (~> 0.1, >= 0.1.2)
4747
dry-logic (~> 0.5, >= 0.5)
48-
faraday (1.0.1)
48+
faraday (1.6.0)
49+
faraday-em_http (~> 1.0)
50+
faraday-em_synchrony (~> 1.0)
51+
faraday-excon (~> 1.1)
52+
faraday-httpclient (~> 1.0.1)
53+
faraday-net_http (~> 1.0)
54+
faraday-net_http_persistent (~> 1.1)
55+
faraday-patron (~> 1.0)
56+
faraday-rack (~> 1.0)
4957
multipart-post (>= 1.2, < 3)
50-
i18n (1.8.2)
58+
ruby2_keywords (>= 0.0.4)
59+
faraday-em_http (1.0.0)
60+
faraday-em_synchrony (1.0.0)
61+
faraday-encoding (0.0.5)
62+
faraday
63+
faraday-excon (1.1.0)
64+
faraday-httpclient (1.0.1)
65+
faraday-net_http (1.0.1)
66+
faraday-net_http_persistent (1.2.0)
67+
faraday-patron (1.0.0)
68+
faraday-rack (1.0.0)
69+
i18n (1.8.10)
5170
concurrent-ruby (~> 1.0)
5271
jaro_winkler (1.5.4)
5372
method_source (1.0.0)
54-
minitest (5.14.0)
73+
minitest (5.14.4)
5574
multipart-post (2.1.1)
56-
ox (2.13.2)
75+
ox (2.14.5)
5776
parallel (1.19.1)
5877
parser (2.7.1.1)
5978
ast (~> 2.4.0)
@@ -90,12 +109,12 @@ GEM
90109
rubocop-rspec (1.38.1)
91110
rubocop (>= 0.68.1)
92111
ruby-progressbar (1.10.1)
93-
thread_safe (0.3.6)
94-
tzinfo (1.2.7)
95-
thread_safe (~> 0.1)
112+
ruby2_keywords (0.0.5)
113+
tzinfo (2.0.4)
114+
concurrent-ruby (~> 1.0)
96115
unicode-display_width (1.7.0)
97116
vcr (5.1.0)
98-
zeitwerk (2.3.0)
117+
zeitwerk (2.4.2)
99118

100119
PLATFORMS
101120
ruby

lib/xpm_ruby/connection.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "faraday"
2+
require "faraday/encoding"
23
require "base64"
34

45
module XpmRuby
@@ -12,8 +13,7 @@ def initialize(access_token:, xero_tenant_id:, url: nil)
1213
end
1314

1415
def get(endpoint:, params: nil)
15-
faraday_connection = Faraday.new(url)
16-
response = faraday_connection.get(endpoint, params, headers)
16+
response = build_connection(url: url).get(endpoint, params, headers)
1717
handle_response(response)
1818
rescue Faraday::ConnectionFailed => error
1919
raise ConnectionFailed.new(error.message)
@@ -22,8 +22,7 @@ def get(endpoint:, params: nil)
2222
end
2323

2424
def post(endpoint:, data:)
25-
faraday_connection = Faraday.new(url)
26-
response = faraday_connection.post(endpoint, data, headers)
25+
response = build_connection(url: url).post(endpoint, data, headers)
2726
handle_response(response)
2827
rescue Faraday::ConnectionFailed => error
2928
raise ConnectionFailed.new(error.message)
@@ -32,8 +31,7 @@ def post(endpoint:, data:)
3231
end
3332

3433
def put(endpoint:, data:)
35-
faraday_connection = Faraday.new(url)
36-
response = faraday_connection.put(endpoint, data, headers)
34+
response = build_connection(url: url).put(endpoint, data, headers)
3735
handle_response(response)
3836
rescue Faraday::ConnectionFailed => error
3937
raise ConnectionFailed.new(error.message)
@@ -42,8 +40,7 @@ def put(endpoint:, data:)
4240
end
4341

4442
def delete(endpoint:, id:)
45-
faraday_connection = Faraday.new(url)
46-
response = faraday_connection.delete("#{endpoint}/#{id}", nil, headers)
43+
response = build_connection(url: url).delete("#{endpoint}/#{id}", nil, headers)
4744
handle_response(response)
4845
rescue Faraday::ConnectionFailed => error
4946
raise ConnectionFailed.new(error.message)
@@ -53,6 +50,15 @@ def delete(endpoint:, id:)
5350

5451
private
5552

53+
def build_connection(url:, adapter: Faraday.default_adapter)
54+
Faraday.new(url) do |connection|
55+
# use Faraday::Encoding middleware to correct the response encoding.
56+
connection.response(:encoding)
57+
58+
connection.adapter(adapter)
59+
end
60+
end
61+
5662
def headers
5763
{ "Authorization" => @authorization, "xero-tenant-id" => @xero_tenant_id, "content_type" => "application/xml" }
5864
end

xpm_ruby.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ Gem::Specification.new do |spec|
3434

3535
spec.add_development_dependency("byebug", "~> 11")
3636
spec.add_development_dependency("pry-byebug", "~> 3")
37+
spec.add_development_dependency("vcr")
3738

3839
spec.add_runtime_dependency("faraday")
40+
spec.add_runtime_dependency("faraday-encoding")
3941
spec.add_runtime_dependency("ox", "~> 2.13")
4042

41-
spec.add_development_dependency("vcr")
42-
4343
spec.add_runtime_dependency("activesupport")
4444
spec.add_runtime_dependency("builder")
4545

46-
spec.add_runtime_dependency("dry-types")
46+
spec.add_runtime_dependency("dry-types", "~> 0.15.0")
4747
end

0 commit comments

Comments
 (0)