Class: EngagingNetworks::Scrape::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/engaging_networks/scrape/client.rb

Constant Summary collapse

HOST =
'www.e-activist.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/engaging_networks/scrape/client.rb', line 8

def initialize(username:, password:)
  self.username = username
  self.password = password
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/engaging_networks/scrape/client.rb', line 4

def password
  @password
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/engaging_networks/scrape/client.rb', line 4

def username
  @username
end

Instance Method Details

#agentObject



56
57
58
59
60
# File 'lib/engaging_networks/scrape/client.rb', line 56

def agent
  @agent ||= ::Mechanize.new do |agent|
    agent.user_agent = 'Engaging Networks Ruby Gem'
  end
end

#build_form(id) ⇒ Object



51
52
53
54
# File 'lib/engaging_networks/scrape/client.rb', line 51

def build_form(id)
  # TODO some sort of API for building campaign forms
  "/ea-account/auth/action.menu.do?v=c:showBuild&ea-account.campaign.id=#{id}"
end

#create_campaign(name:, description:, ajax_enabled: true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/engaging_networks/scrape/client.rb', line 22

def create_campaign(name:, description:, ajax_enabled: true)
  

  campaign_id = nil

  agent.get("https://#{HOST}/ea-account/auth/") do |page|
    new_campaign_form = agent.click(page.link_with(:text => /Create a Campaign/))

    new_campaign_result = new_campaign_form.form_with(name: 'campaignForm') do |campaign|
      campaign.set_fields({'_f_0' => name,
                          '_f_1' => description,
                          '_f_2' => '2', # data capture type
                          #'_f_3' => '3', # new campaign option is not show on new campaign form.
                          'dc_subtype' => 'DCF',
                          'dc_mem_val' => '',
                          '_f_12' => ''
                          })

      campaign.radiobutton_with(:name => '_f_4').value = ajax_enabled ? 'true' : 'false'

    end.submit

    campaign_list = agent.click(new_campaign_result.link_with(:text => /Manage Campaigns/))
    campaign_form = campaign_list.form_with(action: 'https://www.e-activist.com/ea-account/auth/campaign.jsp')
    campaign_id = campaign_form.field_with(name: 'ea.campaign.id').value
  end
  campaign_id
end

#loginObject



13
14
15
16
17
18
19
20
# File 'lib/engaging_networks/scrape/client.rb', line 13

def 
  agent.get("https://#{HOST}/ea-account/index.jsp") do |page|

    page.form_with(name: 'accountLF') do ||
      .set_fields('_f_0' => username, '_f_1' => password)
    end.submit
  end
end