Sha256: 06ec21cb0dba1ad6bbdf2a0793bee4eb790ac89a13df3af4276d49100d0b9411

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 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')

      form['email']    = login_email
      form['password'] = login_password
      form.submit
      last_page.body =~ /Welcome! You are signed in as/
    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

8 entries across 8 versions & 1 rubygems

Version Path
amazon_seller_central-0.2.11 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.10 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.9 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.8 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.7 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.6 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.5 lib/amazon_seller_central/mechanizer.rb
amazon_seller_central-0.2.4 lib/amazon_seller_central/mechanizer.rb