Sha256: 2fe20e747212ffbcaf3ae66bbfafaa4f6fbef9ab0005ad090077318f98472262

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 Bytes

Contents

require "banks_api/shinsei/js_parser"

module BanksApi
  module Shinsei
    class FaradayMiddleware < Faraday::Middleware
      def call(request_env)
        request_env.body.force_encoding(Encoding::ASCII_8BIT)
        request_env.request_headers["User-Agent"] = USER_AGENT

        @app.call(request_env).on_complete do |response_env|
          # #toutf8 converts half-width Katakana to full-width (???)
          # As recommended in the official Ruby documentation (see link below),
          # we'll use this instead.
          # https://docs.ruby-lang.org/ja/2.4.0/method/Kconv/m/toutf8.html
          response_env.body = NKF.nkf("-wxm0", response_env.body)

          if response_env.response_headers["content-type"]&.match?(/text\/html/)
            response_env.body = JsParser.data_for(response_env.body)
          end
        end
      end

      USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1;) PowerDirectBot/0.1".freeze
      private_constant :USER_AGENT
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
banks_api-shinsei-0.1.2 lib/banks_api/shinsei/faraday_middleware.rb