Class: MyUniversalJobsMatchFeed

Inherits:
DailyNotices
  • Object
show all
Defined in:
lib/myuniversaljobsmatchfeed.rb

Instance Method Summary collapse

Constructor Details

#initialize(filepath = '', title: nil, where: nil, url_base: '', dx_xslt: '', rss_xslt: '', refreshrate: nil, target_xslt: '') ⇒ MyUniversalJobsMatchFeed

Returns a new instance of MyUniversalJobsMatchFeed.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/myuniversaljobsmatchfeed.rb', line 12

def initialize(filepath='', title: nil, where: nil, url_base: '', \
                dx_xslt: '', rss_xslt: '', refreshrate: nil, target_xslt: '')

  @schema = 'ujm[title,tags]/item(job_id, title, description, ' + \
                 'posting_date, company, location, industries, ' + \
                 'job_type, salary, hours_of_work, job_reference_code,' + \
                 'career_level, education_level, application_methods,' + \
                 'years_of_experience)'
  @default_key = 'job_id'
      
  super(filepath, url_base: url_base, dx_xslt: dx_xslt, \
        rss_xslt: rss_xslt, target_page: :record, target_xslt: target_xslt)

  @title, @where = title, where
  
  self.title = "My Universal Jobmatch feed for %s in %s" % [title, where]
  self.description = 'Universal Jobmatch data fetched ' + \
                                               'from jobsearch.direct.gov.uk'
  
  # set the time last updated in the hidden scratch file if refreshrate set
  
  @datafile = File.join(@filepath, '.myuniversaljobsmatchfeed')
  @refreshrate = refreshrate
        
    
  if @refesh_rate then
    
    @h =  File.exists?(@datafile) ? Kvx.new(File.read(@datafile)).to_h : \
                                                 {nextrefresh: Time.now.to_s}
  end
  
  @ujm = MyUniversalJobsMatch.new filepath: @filepath

end

Instance Method Details

#on_changeObject

override this method for your own custom notifier, callback, or webhook etc.



101
102
103
104
105
# File 'lib/myuniversaljobsmatchfeed.rb', line 101

def on_change()

  yield() if block_given?
  
end

#startObject



47
48
49
# File 'lib/myuniversaljobsmatchfeed.rb', line 47

def start()
  loop { self.update; sleep((@refreshrate || 1) * 60) }
end

#updateObject



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
96
97
# File 'lib/myuniversaljobsmatchfeed.rb', line 51

def update()

  return if @refreshrate and (Time.parse(@h[:nextrefresh]) > Time.now)

  results = @ujm.search(title: @title, where: @where)
  results_filepath = File.join(@filepath, 'results.xml')
  
  capture = ->(result) do
    vacancy = @ujm.query result[:job_id]
    
    self.add(vacancy, title: vacancy[:title], \
           description: vacancy[:description], id: vacancy[:job_id]) do |kvx|
      kvx.summary[:title] = vacancy[:title]
    end      
  end
  
  if File.exists? results_filepath then
    
    prev_results = Dynarex.new(results_filepath)
    new_results = results.to_a - prev_results.to_a
          
    if new_results.any? then
      
      new_results.each(&capture)
              
      results.save results_filepath
      on_change()
      
    end
    
  else
    
    results.all.each(&capture)
            
    results.save results_filepath
    on_change()      
    
  end

  if @refreshrate then
    
    @h = {nextrefresh: (Time.now + @refreshrate * 60).to_s}
    File.write @datafile, Kvx.new(@h)
    
  end

end