Skip to content

Commit

Permalink
change request to use always use JSON (#195)
Browse files Browse the repository at this point in the history
* change request to use always use JSON

* fix.

* fix test.

* fixed mock.

* bump version.

* rubocop
  • Loading branch information
zeyuwu authored Jan 4, 2022
1 parent 0c1808b commit 84c8e64
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
12 changes: 3 additions & 9 deletions lib/wovnrb/api_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def prepare_request(body)

def gzip_request(html_body)
api_params = build_api_params(html_body)
compressed_body = compress_request_data(api_params)
compressed_body = compress_request_data(api_params.to_json)
request = Net::HTTP::Post.new(request_path(html_body), {
'Accept-Encoding' => 'gzip',
'Content-Type' => 'application/octet-stream',
'Content-Type' => 'application/json',
'Content-Encoding' => 'gzip',
'Content-Length' => compressed_body.bytesize.to_s,
'X-Request-Id' => @uuid
Expand Down Expand Up @@ -122,13 +122,7 @@ def build_api_params(body)
end

def compress_request_data(data_hash)
encoded_data_components = data_hash.map do |key, value|
"#{key}=#{CGI.escape(value)}"
end

gzip = Zlib::GzipWriter.new(StringIO.new)
gzip << encoded_data_components.join('&')
gzip.close.string
ActiveSupport::Gzip.compress(data_hash)
end

def api_uri
Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def settings
# Load Rails config.wovnrb
#
def load_settings
if Object.const_defined?('Rails') && Rails.configuration.respond_to?(:wovnrb)
if Object.const_defined?(:Rails) && Rails.configuration.respond_to?(:wovnrb)
@config_loaded = true
update_settings(Rails.configuration.wovnrb)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wovnrb
VERSION = '3.4.1'.freeze
VERSION = '3.5.0'.freeze
end
9 changes: 4 additions & 5 deletions test/lib/api_translator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def stub_translation_api_request(store, original_html, translated_html, response
'Accept' => '*/*',
'Accept-Encoding' => 'gzip',
'Content-Length' => compressed_data.bytesize,
'Content-Type' => 'application/octet-stream',
'Content-Type' => 'application/json',
'Content-Encoding' => 'gzip',
'User-Agent' => 'Ruby'
}
stub_response_json = "{\"body\":\"#{translated_html.gsub("\n", '\n')}\"}"
Expand Down Expand Up @@ -157,13 +158,11 @@ def generate_data(original_html)
'custom_lang_aliases' => '{"ja":"Japanese"}'
}

data.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join('&')
data.to_json
end

def compress(string)
gzip = Zlib::GzipWriter.new(StringIO.new)
gzip << string
gzip.close.string
ActiveSupport::Gzip.compress(string)
end
end
end

0 comments on commit 84c8e64

Please sign in to comment.