Sha256: c66e76136366e4c072e28ca6d939547944414cf4f871da1a49257f3e28058c58

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

module AmazonAuth
  class Client
    INITIAL_ENTRY_URL = 'https://www.amazon.co.jp/'

    def initialize(options = {})
      @url = options.fetch(:url) { INITIAL_ENTRY_URL }
      @login = options.fetch(:login) do
        if (amazon_username_code = ENV['AMAZON_USERNAME_CODE']).present?
          Converter.decode(amazon_username_code)
        else
          raise('AMAZON_USERNAME_CODE is required.')
        end
      end
      @password = options.fetch(:password) do
        if (amazon_password_code = ENV['AMAZON_PASSWORD_CODE']).present?
          Converter.decode(amazon_password_code)
        else
          raise('AMAZON_PASSWORD_CODE is required.')
        end
      end
    end

    def sign_in
      @session = Capybara::Session.new(:selenium)
      @session.visit @url
      @session.within('#nav-tools') do
        @session.click_on 'サインイン'
      end

      fill_in_with_stroke('ap_email', @login)
      fill_in_with_stroke('ap_password', @password)
      @session.click_on('signInSubmit')

      while image_recognition_displayed? do
        retry_sign_in
      end

      @session.first('.nav-line-2').click
      @session
    end

    def retry_sign_in
      fill_in_with_stroke('ap_password', @password)
      input = ask "Got the prompt. Read characters from the image: "
      fill_in_with_stroke('auth-captcha-guess', input)
      @session.click_on('signInSubmit')
    end

    def fill_in_with_stroke(dom_id, value)
      sleep_s
      element = @session.first("##{dom_id}")
      value.split(//u).each do |char|
        element.send_keys(char)
        sleep rand
      end
    end

    def image_recognition_displayed?
      @session.has_content?('お客様のアカウントを強力に保護するため') || @session.has_content?('問題が発生しました')
    end

    def driver
      @session.driver
    end

    def sleep_s(sec = 2)
      sleep sec
    end

    # Hide instance variables of credentials on console
    def inspect
      to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amazon_auth-0.1.2 lib/amazon_auth/client.rb
amazon_auth-0.1.1 lib/amazon_auth/client.rb