Class: Targetmy::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/targetmy/robot.rb

Constant Summary collapse

LOGIN_URL =
'https://account.my.com/login/'
CAMPAIGNS_URL =
'https://target.my.com/ads/campaigns/'
CAMPAIGNS_CREATE_URL =
'https://target.my.com/ads/create/'
ADV_TYPES_INDEXES =
{ site: 0, game: 1}
ADV_PROD_INDEXES =
{ teaser: 0, mobile: 1 }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Robot

Returns a new instance of Robot.



15
16
17
18
19
20
21
22
23
# File 'lib/targetmy/robot.rb', line 15

def initialize(opts)
  @driver = opts.has_key?(:driver) ? opts[:driver] : :chrome 
  @config_raw = opts.has_key?(:config) ? opts[:config] : [] 

  puts "create config"
  @config = Targetmy::Config.new @config_raw
  # OR
  # @config = OpenStruct.new(items: JSON.parse(config.to_json, object_class: OpenStruct))
end

Class Method Details

.create(opts) ⇒ Object



11
12
13
# File 'lib/targetmy/robot.rb', line 11

def self.create(opts)
  new(opts)
end

Instance Method Details

#create_campaignObject



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
90
91
92
93
94
95
# File 'lib/targetmy/robot.rb', line 35

def create_campaign
  puts "create campaign"
  # Watir::Wait.until { @browser.link(:css, ".campaign-toolbar .campaign-toolbar__create-button").present? }
  # @browser.link(:css, ".campaign-toolbar .campaign-toolbar__create-button").click
  # @browser.goto CAMPAIGNS_CREATE_URL
  begin
    @browser.goto CAMPAIGNS_URL

    @config.items.each do |config|
      # click to create_campaign_btn
      Watir::Wait.until { @browser.link(:css, ".campaign-toolbar .campaign-toolbar__create-button").present? }
      @browser.link(:css, ".campaign-toolbar .campaign-toolbar__create-button").click

      # select product type and set link to the advertised site
      Watir::Wait.until { @browser.div(:css, ".product-types.js-product-types-wrap").present? }
      @adv_type_index = ADV_TYPES_INDEXES[config.type.to_sym]
      @browser.div(css: ".product-types__item", index: @adv_type_index).click
      @browser.text_field(:css, ".base-setting-my__main-url__input.js-main-url-input").set(config.link)

      # setup adv_products
      Watir::Wait.until { @browser.div(:css, ".js-pacs-wrapper").present? }
      @adv_prod_index = nil
      config.adv_products.each do |prod|
        # select adv product
        if @adv_prod_index != ADV_PROD_INDEXES[prod.type.to_sym]
          @adv_prod_index = ADV_PROD_INDEXES[prod.type.to_sym]
          @browser.div(css: ".pac-item._gray-block", index: @adv_prod_index).click
        end

        # create ads
        Watir::Wait.until { @browser.div(:css, ".banner-form").present? }
        prod.ads.each do |ad|
          # set ad form fields
          @browser.text_field(:css, ".banner-form__input.js-text-value").set(ad.title)
          @browser.textarea(:css, ".banner-form__input.js-text-value.banner-form__input_text-area").set(ad.text)
          @browser.file_field(:css, ".banner-form__img-file.banner-form__input.js-image-value").set(ad.image)
          # handle crop image
          Watir::Wait.until { @browser.button(:css, ".image-cropper__save").present? }
          @browser.button(:css, ".image-cropper__save").click

          # create ad click
          Watir::Wait.until { @browser.button(:css, ".banner-form__save-button.js-banner-save").present? }
          Watir::Wait.until { sleep 1 }
          @browser.button(:css, ".banner-form__save-button.js-banner-save").click
        end

        # set another fields
        @browser.text_field(:css, ".js-campaign-name").set(prod.campaign_name)
        # click to the finish btn
        @browser.div(:css, ".create-page__main-button").click

        puts "finish"
      end

    end

  rescue Exception => e
    @browser.close
    raise e
  end
end

#login(email, password) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/targetmy/robot.rb', line 97

def (email, password)
  puts "login"

  @browser.goto LOGIN_URL

  @browser.text_field(:id, "email").set(email)
  @browser.text_field(:id, "password").set(password)
  @browser.form(:id,'login_form').submit

  if @browser.div(css: ".formMsg.js_form_msg.js_form_msg").present?
    puts "Wrong login or password!"
    @browser.close
  end

  true
end

#start(email, password) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/targetmy/robot.rb', line 25

def start(email, password)
  puts "start"

  @browser = Watir::Browser.new @driver

  if (email, password)
    create_campaign
  end
end