Class: Arrogance::PostHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/arrogance/tools.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, tag_set) ⇒ PostHandler

Returns a new instance of PostHandler.



217
218
219
220
# File 'lib/arrogance/tools.rb', line 217

def initialize(url, tag_set)
  self.url = url      
  init(tag_set)        
end

Instance Attribute Details

#postsObject

Returns the value of attribute posts.



215
216
217
# File 'lib/arrogance/tools.rb', line 215

def posts
  @posts
end

#urlObject

Returns the value of attribute url.



215
216
217
# File 'lib/arrogance/tools.rb', line 215

def url
  @url
end

Instance Method Details

#build_post(item, idx, tag_set) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/arrogance/tools.rb', line 237

def build_post(item, idx, tag_set)
  hash = {}
  unless tag_set.title.nil?
    hash[:title] = item.xpath(tag_set.title).inner_text.to_s  
  end 
  unless tag_set.author.nil?
    if tag_set.author == 'none'
      hash[:author] == 'Unknown'
    else
      if tag_set.author == '//author//name'
        if item.xpath(tag_set.author).length == 1
          hash[:author] = item.xpath(tag_set.author).inner_text.to_s
        else
          hash[:author] = item.xpath(tag_set.author)[idx].inner_text.to_s
        end
      else
        hash[:author] = item.xpath(tag_set.author).inner_text.to_s
      end
    end
  end
  unless tag_set.description.nil?      
    hash[:description] = item.xpath(tag_set.description).inner_text.to_s
  end
  unless tag_set.date.nil?
    hash[:date] = item.xpath(tag_set.date).inner_text.to_s
    if hash[:date].nil? || hash[:date].empty?
      if (m = /[0-9]\{4, 4\}-[0-9]\{2,2\}-[0-9]\{2, 2\}/.match(item.xpath('id').to_s).to_s)
        hash[:date] = /,.*?:/.match(item.xpath('id').to_s).to_s.gsub(/(,|:)/, '')
      end
    end
  end    
  unless tag_set.link.nil?
    if tag_set.link == 'manual'
      item.xpath('link').each do |link|
        if /rel=/.match(link.to_s)
          if /type=/.match(link.to_s)
            hash[:link] = /("|')http.*?("|')/.match(link.to_s).to_s.gsub(/('|")/, '')
            break
          elsif /href=/.match(link.to_s) && /alternate/.match(link.to_s)
            hash[:link] = /("|')http.*?("|')/.match(link.to_s).to_s.gsub(/("|')/, '')
            break
          end
        end
      end
      if hash[:link].nil? || hash[:link].empty?
        item.xpath('link').each do |link|
          if /href=/.match(link.to_s)
            hash[:link] = /("|').*?("|')/.match(link.to_s).to_s.gsub(/("|')/, '')
            break
          end
        end
      end
    else
      hash[:link] = item.xpath(tag_set.link).inner_text.to_s
      if hash[:link].nil? || hash[:link].empty?
        item.xpath('link').each do |link|
          if /type=("|')text\/html("|')/.match(link.to_s)
            if /rel=("|')alternate("|') /.match(link.to_s)
              hash[:link] = /("|').*?("|')/.match(link.to_s).to_s
              break
            end
          end
        end
      end
    end
  end
  [:link, :date, :description, :author, :title].each do |i|
    if !hash[i]
      hash[i] = ''
    end
  end
  return hash
end

#init(tag_set) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/arrogance/tools.rb', line 222

def init(tag_set)
  doc, ary = Nokogiri::XML(open(self.url)).remove_namespaces!, []
  unless doc.xpath('//entry').empty?
    doc.xpath('//entry').each_with_index do |i, idx|
      ary << build_post(i, idx, tag_set)
    end
  end
  unless doc.xpath('//item').empty?
    doc.xpath('//item').each_with_index do |i, idx|
      ary << build_post(i, idx, tag_set)
    end
  end
  self.posts = ary
end