Class: DailyNotices

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/daily_notices.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath = '', url_base: 'http:/127.0.0.1/', identifier: '', dx_xslt: '', rss_xslt: '', target_page: :recordset, target_xslt: '', title: 'daily notices', log: nil, debug: false) ⇒ DailyNotices

Returns a new instance of DailyNotices.



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
46
47
48
49
50
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
# File 'lib/daily_notices.rb', line 17

def initialize(filepath='', url_base: 'http:/127.0.0.1/', identifier: '',
                      dx_xslt: '', rss_xslt: '', target_page: :recordset,
                      target_xslt: '', title: 'daily notices', log: nil,
                      debug: false)

  @filepath, @url_base, @dx_xslt, @rss_xslt, @target_page, @target_xslt,  \
        @identifier, @log, @debug = filepath, url_base, dx_xslt, rss_xslt, \
        target_page, target_xslt, identifier, log, debug


  # note: card is intended for storing meta data in JSON format

  @schema = 'items[title, identifier, image, bio, location, website,' +
      ' banner_image]/item(title, description, card, time, link)'
  @default_key = 'uid'

  if dx_xslt.nil? then

    subdir = File.basename filepath
    dir = url_base[/http:\/\/[^\/]+\/(.*)/,1]

    dxxsltfilename = "dx#{Time.now.to_i.to_s}.xsl"
    dxxsltfilepath = '/' + [dir, subdir, dxxsltfilename].join('/')
    FileX.write File.join(filepath, dxxsltfilename), \
                                           DxSliml.new(dx: @schema).to_xslt

    @dx_xslt = dxxsltfilepath
  end

  if rss_xslt.nil? then

    subdir = File.basename filepath
    dir = url_base[/http:\/\/[^\/]+\/(.*)/,1]

    rssxsltfilename = "rssx#{Time.now.to_i.to_s}.xsl"
    rssxsltfilepath = '/' + [dir, subdir, rssxsltfilename].join('/')
    FileX.write File.join(filepath, rssxsltfilename), \
                                           RssSliml.new().to_xslt

    @rss_xslt = rssxsltfilepath
  end

  @day = Time.now.day
  @title = title
  new_day()

  # open the Dynarex file or create a new Dynarex file

  @rssfile = File.join(@filepath, 'rss.xml')

  if FileX.exists? @rssfile then
    @rss = RSScreator.new @rssfile, dx_xslt: @rss_xslt,
        custom_fields: ['topic'], log: @log
  else

    @rss = RSScreator.new @rssfile, dx_xslt: @rss_xslt,
        custom_fields: ['topic'], log: @log

    @rss.xslt = @rss_xslt
    @rss.title = @title || identifier.capitalize + ' daily notices'
    @rss.description = 'Generated using the daily_notices gem'
    @rss.link = @url_base
  end

  # :recordset or : record
  @target_page = target_page
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/daily_notices.rb', line 15

def description
  @description
end

#dx_xsltObject

Returns the value of attribute dx_xslt.



15
16
17
# File 'lib/daily_notices.rb', line 15

def dx_xslt
  @dx_xslt
end

Returns the value of attribute link.



15
16
17
# File 'lib/daily_notices.rb', line 15

def link
  @link
end

#rss_xsltObject

Returns the value of attribute rss_xslt.



15
16
17
# File 'lib/daily_notices.rb', line 15

def rss_xslt
  @rss_xslt
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/daily_notices.rb', line 15

def title
  @title
end

Instance Method Details

#create(id: Time.now.to_i.to_s, item: {time: Time.now.strftime('%H:%M %p - %d %b %Y'), title: nil}) ⇒ Object Also known as: add



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/daily_notices.rb', line 85

def create(id: Time.now.to_i.to_s,
          item: {time: Time.now.strftime('%H:%M %p - %d %b %Y'),
                 title: nil})

  @log.info 'daily_notices/create: item: ' + item.inspect if @log
  h = item

  new_day() if @day != Time.now.day

  if @dx.all.any? and
      @dx.all.first.description == CGI.unescape(h[:description].to_s) then

    return :duplicate

  end

  h[:link] ||= create_link(id)
  h[:title] ||= h[:description]
      .split(/\n/,2).first.gsub(/\<\/?\w+[^>]*>/,'')[0..140]
  h[:time] ||= Time.now.strftime('%H:%M %p ยท %b %d %Y')


  #@dx.create({description: description, time: time}, id: id)
  puts 'before @dx.create' if @debug
  @log.info 'daily_notices/create: before' if @log

  # deep clone the Hash object
  h3 = Marshal.load( Marshal.dump(h) )
  h[:card] = h[:card].to_json if h[:card] and h[:card].is_a? Hash

  @dx.create(h, id: id)
  @log.info 'daily_notices/create: after' if @log
  #puts 'after @dx.create' if @debug

  @dx.save @indexpath

  render_html_files(id)

  # Add it to the RSS document

  if h3[:card] and h3[:card].is_a? Hash then

    card = h3[:card]
    h2 = card[card.keys.first]

    content = case card.keys.first
    when :summary_large_image
     "\n<h1>%s</h1><p>%s</p>" % [h2[:title], h2[:desc]]
    end

    h3[:title] += h2[:title] if h3[:title].empty?
    h3[:description] += content

  end

  @log.debug 'daily_notices/create: h3: ' + h3.inspect if @log
  @rss.add(item: h3, id: id)
  @rss.save @rssfile
  # open up the RSS file and fill in the title and description fields

  on_add(@indexpath, id)

  return true

end

#delete(id) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/daily_notices.rb', line 154

def delete(id)

  [@dx, @rss].each {|x| x.delete(id.to_s); x.save}

  archive_path = Time.at(id.to_i).strftime("%Y/%b/%-d").downcase
  indexpath = File.join(@filepath, archive_path, id.to_s)

  FileX.rm_rf indexpath

  id.to_s + ' deleted'

end

#image=(val) ⇒ Object



175
176
177
178
179
# File 'lib/daily_notices.rb', line 175

def image=(val)
  image_url, target_url = val.split
  @rss.image_url = image_url
  @rss.image_target_url = target_url
end

#on_add(xmlpath, id) {|xmlpath, id| ... } ⇒ Object

If you wish override this method or use it in block form to add a

notifier, callback routine or webhook, whenever a new record is added.

Yields:

  • (xmlpath, id)


201
202
203
204
205
# File 'lib/daily_notices.rb', line 201

def on_add(xmlpath, id)

  yield(xmlpath, id) if block_given?

end

#saveObject



207
208
209
# File 'lib/daily_notices.rb', line 207

def save()
  @rss.save @rssfile
end

#to_dxObject



211
212
213
# File 'lib/daily_notices.rb', line 211

def to_dx()
  Dynarex.new @dx.to_xml
end