class CricosScrape::ContactImporter

Constants

CONTACT_URL
STATES_CODE

Attributes

agent[R]
page[R]

Public Class Methods

new(agent) click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 10
def initialize(agent)
  @agent = agent
end

Public Instance Methods

run() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 14
def run
  contacts = []

  for state in STATES_CODE
    @page = agent.get(url_for(state))
    if exist_contacts_of_state?
      @table_contains_contact = @page.at('#ctl00_cphDefaultPage_tabContainer_sheetDetails_cricosContactDetails_pnlContactLists table').children

      number_of_rows_per_contact = 18
      start_contact_row = 3
      end_contact_row = @table_contains_contact.count - number_of_rows_per_contact

      for i in (start_contact_row..end_contact_row).step(number_of_rows_per_contact)
        @row_index = i

        contact                = Contact.new
        contact.type_of_course = find_type_of_course
        contact.name           = find_name
        contact.organisation   = find_organisation
        contact.postal_address = find_postal_address
        contact.telephone      = find_telephone
        contact.facsimile      = find_facsimile
        contact.email          = find_email

        contacts << contact
      end
    end
  end

  contacts
end

Private Instance Methods

australia_postcode_regex() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 101
def australia_postcode_regex
  '\d{4}'
end
australia_states_code_regex() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 97
def australia_states_code_regex
  'ACT|NSW|NT|QLD|SA|TAS|VIC|WA'
end
exist_contacts_of_state?() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 54
def exist_contacts_of_state?
  !!@page.at('#__tab_ctl00_cphDefaultPage_tabContainer_sheetDetails')
end
extract_suburb_and_state_and_postcode_from(line) click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 93
def extract_suburb_and_state_and_postcode_from(line)
  line.scan(/^(.*)\s(#{australia_states_code_regex})\s(#{australia_postcode_regex})$/).first
end
find_email() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 115
def find_email
  email_row = @table_contains_contact[@row_index+14].children
  find_value_of_field(email_row[3])
end
find_facsimile() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 110
def find_facsimile
  facsimile_row = @table_contains_contact[@row_index+12].children
  find_value_of_field(facsimile_row[3])
end
find_name() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 66
def find_name
  name_row = @table_contains_contact[@row_index+4].children
  find_value_of_field(name_row[3]).empty? ? find_value_of_field(name_row[2]) : find_value_of_field(name_row[3])
end
find_organisation() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 71
def find_organisation
  organisation_row = @table_contains_contact[@row_index+6].children
  find_value_of_field(organisation_row[3])
end
find_postal_address() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 76
def find_postal_address
  address = Address.new

  address_row = @table_contains_contact[@row_index+8].children
  postal_address_cell = address_row[3].children

  # delete <br>
  lines = postal_address_cell - postal_address_cell.css('br')
  address.address_line_1 = find_value_of_field(lines[0])

  if line2 = find_value_of_field(lines[1])
    address.suburb, address.state, address.postcode = extract_suburb_and_state_and_postcode_from(line2)
  end

  address
end
find_telephone() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 105
def find_telephone
  telephone_row = @table_contains_contact[@row_index+10].children
  find_value_of_field(telephone_row[3])
end
find_type_of_course() click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 62
def find_type_of_course
  find_value_of_field(@table_contains_contact[@row_index])
end
find_value_of_field(field) click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 58
def find_value_of_field(field)
  field.nil? ? nil : field.text.strip
end
url_for(state_code) click to toggle source
# File lib/cricos_scrape/importer/contact_importer.rb, line 50
def url_for(state_code)
  "#{CONTACT_URL}?StateCode=#{state_code}"
end