Class: AmazonSellerCentral::Mechanizer

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_seller_central/mechanizer.rb

Defined Under Namespace

Classes: AgentResetError, CapchaPresentError, FormNotFoundError, LinkNotFoundError

Constant Summary collapse

MASQUERADE_AGENTS =
['Mac Safari', 'Mac FireFox', 'Linux Firefox', 'Windows IE 9']
VERIF_PAGE_PATTERN =

constants for the verification page if logged in from a new device

/What is the ZIP Code/
VERIF_PAGE_FORM_NAME =
'ap_dcq_form'
VERIF_PAGE_FIELD_NAME =
'dcq_question_subjective_1'
VERIF_PAGE_ZIP_CODE =
'20706'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMechanizer

Returns a new instance of Mechanizer.



14
15
16
# File 'lib/amazon_seller_central/mechanizer.rb', line 14

def initialize
  @logged_in = false
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



12
13
14
# File 'lib/amazon_seller_central/mechanizer.rb', line 12

def agent
  @agent
end

Instance Method Details

Raises:



91
92
93
94
95
96
# File 'lib/amazon_seller_central/mechanizer.rb', line 91

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

#last_pageObject



30
31
32
# File 'lib/amazon_seller_central/mechanizer.rb', line 30

def last_page
  agent.current_page
end

#login_emailObject



18
19
20
# File 'lib/amazon_seller_central/mechanizer.rb', line 18

def 
  AmazonSellerCentral.configuration.
end

#login_passwordObject



22
23
24
# File 'lib/amazon_seller_central/mechanizer.rb', line 22

def 
  AmazonSellerCentral.configuration.
end

#login_to_seller_centralObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/amazon_seller_central/mechanizer.rb', line 34

def 
  return true if @logged_in
  tries = 3
  begin
    tries -= 1
    page = agent.get('https://sellercentral.amazon.com/gp/homepage.html')
    form = page.form_with(:name => 'signinWidget')

    raise FormNotFoundError unless form

    begin
      form['username']    = 
      form['password']    = 
      p = form.submit

      if p =~ /better protect/ # capcha!
        raise CapchaPresentError.new("Holy CAPCHA Batman!")
      end

      @logged_in = !!( p.body =~ /Logout/ )

      unless p.body =~ /Manage Inventory/
        marketplace_id = Mechanize::Form::SelectList.new((p / '#sc-mkt-switcher-select').first)
                           .options
                           .select { |option| option.text == 'www.amazon.com' }
                           .first
                           .value

        # The dropdown that selects the subsite uses javascript to change the page location,
        # so we need to query the url directly.
        p = agent.get('https://sellercentral.amazon.com/gp/utilities/set-rainier-prefs.html?ie=UTF8&url=&marketplaceID=' + marketplace_id)
      end

    rescue StandardError => e
      File.open("/tmp/seller_central_#{Time.now.to_i}.html","wb") do |f|
        f.write page.body
      end
      raise e
    end

    # New device verification
    if p.body =~ VERIF_PAGE_PATTERN
      form = p.form_with(:name => VERIF_PAGE_FORM_NAME)
      form[VERIF_PAGE_FIELD_NAME] = VERIF_PAGE_ZIP_CODE
      p = form.submit # This raises a response code error, :-(
    end

  rescue Mechanize::ResponseCodeError, FormNotFoundError
    if tries > 0
      sleep 10
      retry if tries > 0
    end
    raise
  end
  true
end

#reset!Object



98
99
100
101
102
# File 'lib/amazon_seller_central/mechanizer.rb', line 98

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