Class: JMAFeed::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/jma_feed/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_result) ⇒ Result

Returns a new instance of Result.



4
5
6
# File 'lib/jma_feed/result.rb', line 4

def initialize(http_result)
  @http_result = http_result
end

Instance Attribute Details

#http_resultObject (readonly)

Returns the value of attribute http_result.



2
3
4
# File 'lib/jma_feed/result.rb', line 2

def http_result
  @http_result
end

Instance Method Details

#bodyObject



12
13
14
# File 'lib/jma_feed/result.rb', line 12

def body
  http_result.body
end

#codeObject



8
9
10
# File 'lib/jma_feed/result.rb', line 8

def code
  http_result.code
end

#docObject



20
21
22
# File 'lib/jma_feed/result.rb', line 20

def doc
  @doc ||= Nokogiri::XML(body)
end

#entriesObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jma_feed/result.rb', line 52

def entries
  @entries ||= doc.xpath('/atom:feed/atom:entry', namespace).map do |entry|
    JMAFeed::ResultEntry.new(
      title: entry.xpath('atom:title', namespace).text,
      link: entry.xpath('atom:link/@href', namespace).text,
      id: entry.xpath('atom:id', namespace).text,
      updated: entry.xpath('atom:updated', namespace).text,
      author: entry.xpath('atom:author/atom:name', namespace).text,
      content: entry.xpath('atom:content', namespace).text,
    )
  end
end

#idObject



40
41
42
# File 'lib/jma_feed/result.rb', line 40

def id
  text('id')
end

#last_modifiedObject



16
17
18
# File 'lib/jma_feed/result.rb', line 16

def last_modified
  DateTime.parse(http_result["Last-Modified"])
end

#namespaceObject



24
25
26
# File 'lib/jma_feed/result.rb', line 24

def namespace
  {atom: doc.root.namespace.href}
end


48
49
50
# File 'lib/jma_feed/result.rb', line 48

def related
  text('link[@rel="related"]/@href')
end

#subtitleObject



36
37
38
# File 'lib/jma_feed/result.rb', line 36

def subtitle
  text('subtitle')
end

#text(name) ⇒ Object



28
29
30
# File 'lib/jma_feed/result.rb', line 28

def text(name)
  doc.xpath("/atom:feed/atom:#{name}", namespace).text
end

#titleObject



32
33
34
# File 'lib/jma_feed/result.rb', line 32

def title
  text('title')
end

#updatedObject



44
45
46
# File 'lib/jma_feed/result.rb', line 44

def updated
  DateTime.parse(text('updated'))
end