Class: Storify::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/storify/element.rb

Overview

todo: split logic into separate source types

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#authorObject (readonly)

Returns the value of attribute author.



6
7
8
# File 'lib/storify/element.rb', line 6

def author
  @author
end

#descObject

Returns the value of attribute desc.



7
8
9
# File 'lib/storify/element.rb', line 7

def desc
  @desc
end

Returns the value of attribute link.



6
7
8
# File 'lib/storify/element.rb', line 6

def link
  @link
end

#publishedObject (readonly)

Returns the value of attribute published.



6
7
8
# File 'lib/storify/element.rb', line 6

def published
  @published
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/storify/element.rb', line 6

def source
  @source
end

#typeObject (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_sObject



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