Class: Storify::Element
- Inherits:
-
Object
- Object
- Storify::Element
- Defined in:
- lib/storify/element.rb
Overview
todo: split logic into separate source types
Instance Attribute Summary collapse
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#desc ⇒ Object
Returns the value of attribute desc.
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#published ⇒ Object
readonly
Returns the value of attribute published.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(content) ⇒ Element
constructor
A new instance of Element.
- #to_s ⇒ Object
Constructor Details
#initialize(content) ⇒ Element
Returns a new instance of Element.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/storify/element.rb', line 9 def initialize(content) @type = content['type'] @source = content['source']['name'] @link = content['permalink'] @published = DateTime.parse(content['posted_at']) case @type when 'image' @desc = content['data'][@type]['caption'] if content['data'].has_key?('oembed') @author = content['data']['oembed']['author_name'] else @author = content['attribution']['name'] end when 'text' @desc = content['data']['text'] doc = Nokogiri::HTML(@desc) @desc = doc.xpath("//text()").to_s else @desc = content['data'][@type]['description'] @author = content['source']['username'] @author = "@" + @author if @source.downcase == 'twitter' end end |
Instance Attribute Details
#author ⇒ Object (readonly)
Returns the value of attribute author.
6 7 8 |
# File 'lib/storify/element.rb', line 6 def @author end |
#desc ⇒ Object
Returns the value of attribute desc.
7 8 9 |
# File 'lib/storify/element.rb', line 7 def desc @desc end |
#link ⇒ Object (readonly)
Returns the value of attribute link.
6 7 8 |
# File 'lib/storify/element.rb', line 6 def link @link end |
#published ⇒ Object (readonly)
Returns the value of attribute published.
6 7 8 |
# File 'lib/storify/element.rb', line 6 def published @published end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/storify/element.rb', line 6 def source @source end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/storify/element.rb', line 6 def type @type end |
Instance Method Details
#to_s ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/storify/element.rb', line 36 def to_s out = '' case @source.downcase when 'storify' out << "\n#{@desc}\n" out << ('-' * @desc.length) + "\n\n" if @desc.length < 50 when 'twitter' out << "[#{@published.to_date.to_s}] #{@author}: #{@link}\n" else out << "#{@author} [#{@published.to_date.to_s}]: #{@link}\n" end out end |