Class: Weneedfeed::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/weneedfeed/item.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description_selector:, image_selector:, link_selector:, node:, time_selector:, title_selector:, url:) ⇒ Item

Returns a new instance of Item.

Parameters:

  • description_selector (String, nil)
  • image_selector (String, nil)
  • link_selector (String, nil)
  • node (Nokogiri::Node)
  • time_selector (String)
  • title_selector (String)
  • url (String)


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?

Parameters:

  • string (String)

Returns:

  • (Time, nil)


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

#descriptionString?

Returns:

  • (String, nil)


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

#guidString

Returns:

  • (String)


61
62
63
# File 'lib/weneedfeed/item.rb', line 61

def guid
  link || hashed_title_and_description
end

#image_mime_typeString?

Returns:

  • (String, nil)


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_urlString?

Returns:

  • (String, nil)


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_urlObject



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

Returns:

  • (String, nil)


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

Returns:

  • (Nokogiri::Node, nil)


106
107
108
# File 'lib/weneedfeed/item.rb', line 106

def link_node
  @node.at(@link_selector)
end

Returns:

  • (String, nil)


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

#timeTime?

Returns:

  • (Time, nil)


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

#titleString?

Returns:

  • (String, nil)


129
130
131
# File 'lib/weneedfeed/item.rb', line 129

def title
  @node.at(@title_selector)&.inner_text
end