module Cekresi

Constants

USER_AGENTS
VERSION

Public Class Methods

fetch(tracking_number) click to toggle source
# File lib/cekresi.rb, line 16
def self.fetch(tracking_number)
  url = "http://cekresi.com/?noresi=#{tracking_number}"
  session = Capybara::Session.new(:poltergeist)
  session.driver.headers = {'User-Agent' => USER_AGENTS.sample}
  session.visit(url)
  session.find("#cekresi").click
  begin
    container_result = session.find(:xpath, "//div[@id='results']")
    if container_result
      if container_result.find(".top_title")
        result = Cekresi::Parser.parse(container_result)
      else
        result = {status: :not_found, message: container_result.text}
      end
    else
      result = {status: :not_found, message: 'Sorry your search not found'}
    end
  rescue Capybara::ElementNotFound
    result = {status: :not_found, message: 'There is problem from cekresi.com, please try again'}
  end

  Capybara.reset_sessions!
  session.driver.quit
  return result
end