Sha256: f59fb17ca07d25b4b22944240456030c6cc348209b87127e5c5912e4ea1c015d

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'mechanize'
module AmazonSellerCentral
  class Mechanizer
    MASQUERADE_AGENTS = ['Mac Safari', 'Mac FireFox', 'Linux Firefox']

    attr_reader :agent

    def login_email
      AmazonSellerCentral.configuration.login_email
    end

    def login_password
      AmazonSellerCentral.configuration.login_password
    end

    def agent
      @agent ||= Mechanize.new {|ag| ag.user_agent_alias = MASQUERADE_AGENTS[rand(MASQUERADE_AGENTS.size)] }
    end

    def last_page
      agent.current_page
    end

    def login_to_seller_central
      page = agent.get('https://sellercentral.amazon.com/')
      form = page.form_with(:name => 'signIn')

      begin
        form['email']    = login_email
        form['password'] = login_password
        form.submit
        last_page.body =~ /Welcome! You are signed in as/
      rescue
        File.open("/tmp/seller_central_#{Time.now.to_i}.html","w") do |f|
          f.write page.body
        end
        raise
      end
    end

    def follow_link_with(options)
      raise AgentResetError unless last_page
      link = last_page.link_with(options)
      raise LinkNotFoundError unless link
      agent.click(link)
    end

    def reset!
      @agent     = nil
      #@last_page = nil
    end

    class LinkNotFoundError < StandardError; end
    class AgentResetError < StandardError; end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
amazon_seller_central-0.2.16 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.15 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.14 lib/amazon_seller_central/mechanizer.rb