Class: Weneedfeed::Item
- Inherits:
-
Object
- Object
- Weneedfeed::Item
- Defined in:
- lib/weneedfeed/item.rb
Class Method Summary collapse
Instance Method Summary collapse
- #description ⇒ String?
- #guid ⇒ String
- #image_mime_type ⇒ String?
- #image_path_or_url ⇒ String?
- #image_url ⇒ Object
-
#initialize(description_selector:, image_selector:, link_selector:, node:, time_selector:, title_selector:, url:) ⇒ Item
constructor
A new instance of Item.
- #link ⇒ String?
- #link_node ⇒ Nokogiri::Node?
- #link_path_or_url ⇒ String?
- #time ⇒ Time?
- #title ⇒ String?
Constructor Details
#initialize(description_selector:, image_selector:, link_selector:, node:, time_selector:, title_selector:, url:) ⇒ Item
Returns a new instance of Item.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/weneedfeed/item.rb', line 35 def initialize( description_selector:, image_selector:, link_selector:, node:, time_selector:, title_selector:, url: ) @description_selector = description_selector @image_selector = image_selector @link_selector = link_selector @node = node @time_selector = time_selector @title_selector = title_selector @url = url end |
Class Method Details
.parse_time(string) ⇒ Time?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/weneedfeed/item.rb', line 12 def parse_time(string) ::Time.strptime(string, '%Y年%m月%d日') rescue ::ArgumentError begin time = ::Time.strptime(string, '%m月%d日') time -= 60 * 60 * 24 * 365 if time > Time.now time rescue ArgumentError begin ::Time.parse(string) rescue ArgumentError, RangeError end end end |
Instance Method Details
#description ⇒ String?
54 55 56 57 58 |
# File 'lib/weneedfeed/item.rb', line 54 def description return unless @description_selector @node.at(@description_selector)&.inner_html end |
#guid ⇒ String
61 62 63 |
# File 'lib/weneedfeed/item.rb', line 61 def guid link || hashed_title_and_description end |
#image_mime_type ⇒ String?
66 67 68 69 70 |
# File 'lib/weneedfeed/item.rb', line 66 def image_mime_type return unless image_url ::Marcel::Magic.by_path(image_url)&.type end |
#image_path_or_url ⇒ String?
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/weneedfeed/item.rb', line 73 def image_path_or_url return unless @image_selector node = @node.at(@image_selector) return unless node if node.name == 'img' node['src'] else node.content end end |
#image_url ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/weneedfeed/item.rb', line 86 def image_url return unless image_path_or_url ::Addressable::URI.join( @url, image_path_or_url ).normalize.to_s end |
#link ⇒ String?
96 97 98 99 100 101 102 103 |
# File 'lib/weneedfeed/item.rb', line 96 def link return unless link_path_or_url ::Addressable::URI.join( @url, link_path_or_url ).normalize.to_s end |
#link_node ⇒ Nokogiri::Node?
106 107 108 |
# File 'lib/weneedfeed/item.rb', line 106 def link_node @node.at(@link_selector) end |
#link_path_or_url ⇒ String?
111 112 113 114 115 116 |
# File 'lib/weneedfeed/item.rb', line 111 def link_path_or_url node = link_node return unless node node['href'] || node.inner_text end |
#time ⇒ Time?
119 120 121 122 123 124 125 126 |
# File 'lib/weneedfeed/item.rb', line 119 def time return unless @time_selector string = time_string return unless string self.class.parse_time(string) end |
#title ⇒ String?
129 130 131 |
# File 'lib/weneedfeed/item.rb', line 129 def title @node.at(@title_selector)&.inner_text end |