Module: HawatelSearchJobs::Api::Indeed

Extended by:
Helpers::Base
Defined in:
lib/hawatel_search_jobs/api/indeed.rb

Constant Summary collapse

DEFAULT =
{
    :keywords => '',
    :location => '',
    :company  => ''
}
RESULT_LIMIT =
25

Class Method Summary collapse

Class Method Details

.page(args) ⇒ Hash<OpenStruct>

Show next page of results

Examples:

page({:query_key => result.key, :page => 2}

Parameters:

  • args (Hash)

Options Hash (args):

  • :page (Integer)

    page number (default 0)

  • :query_key (String)

    url from last query

Returns:

  • (Hash<OpenStruct>)


80
81
82
83
84
85
86
87
88
89
# File 'lib/hawatel_search_jobs/api/indeed.rb', line 80

def page(args)
  args[:page] = 0 if args[:page].nil?
  page_size = args[:settings][:page_size].to_s.empty? ? RESULT_LIMIT : args[:settings][:page_size].to_i
  page_size = RESULT_LIMIT if page_size <= 0 || page_size > 25

  if args[:query_key]
    url = args[:query_key].gsub(/&start=.*/, '') << "&start=#{args[:page]*page_size}"
    search({:settings => args[:settings], :query_key => url})
  end
end

.search(args) ⇒ Hash<OpenStruct>

Search jobs based on specified keywords or location

Examples:

search(:settings => HawatelSearchJobs.indeed,:query => {:keywords => 'ruby'})
search(:query_key => 'http://api.../ads/apisearch?publisher=12323&q=ruby')

Parameters:

  • args (Hash)

Options Hash (args):

  • :query_key (String)

    full url from last query, option deliverd by page() method

  • :query (Hash)

    search criteria

    • :keywords (String) keywords for search

    • :location (String) specified location for search criteria (default all countries)

  • :settings (Hash)
    • :api (String) hostname or ip address api server

    • :publisher (String) authentication string

Returns:

  • (Hash<OpenStruct>)


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
# File 'lib/hawatel_search_jobs/api/indeed.rb', line 35

def search(args)
  args[:query] = DEFAULT.merge(args[:query]) if args[:query]
  args[:page_size] = args[:settings][:page_size].to_s.empty? ? RESULT_LIMIT : args[:settings][:page_size].to_i
  args[:page_size] = RESULT_LIMIT if args[:page_size] <= 0 || args[:page_size] > 25

  if args[:query_key].to_s.empty?
    url_request = build_url(args)
  else
    url_request = args[:query_key]
  end

  if url_request
    attributes  = Hash.new
    response   = send_request(url_request)
    result     = JSON(response.body)
    if response.code == '200' && result['error']
      attributes[:code] = 501
      attributes[:msg]  = result['error']
      return OpenStruct.new(attributes)
    else
      attributes[:code] = response.code
      attributes[:msg]  = response.message
      return OpenStruct.new(attributes) if response.code != '200'
    end
    attributes[:totalResults] = result['totalResults']
    attributes[:page] = result['pageNumber']
    attributes[:last] = paging_info(args[:page_size], result['totalResults'])
    attributes[:key]  = url_request
    attributes[:jobs] = parse_raw_data(result)
    OpenStruct.new(attributes)
  else
    OpenStruct.new({:code => 501, :msg => 'lack of api or publisher setting'})
  end
end