Class: ZooniverseSocial::Posts

Inherits:
Object
  • Object
show all
Defined in:
lib/zooniverse_social/posts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePosts

Returns a new instance of Posts.



8
9
10
11
12
# File 'lib/zooniverse_social/posts.rb', line 8

def initialize
  @blog_updater = Updater.new 'https://public-api.wordpress.com', '/rest/v1.1/sites/36711287/posts'
  @daily_updater = Updater.new 'https://public-api.wordpress.com', '/rest/v1.1/sites/57182749/posts'
  update
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/zooniverse_social/posts.rb', line 6

def data
  @data
end

Instance Method Details

#_update(updater) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zooniverse_social/posts.rb', line 20

def _update(updater)
  response = updater.update number: 3, fields: 'ID,URL,title,excerpt,date'
  response.fetch('posts', []).collect do |post|
    {
      id: post['ID'],
      title: post['title'],
      excerpt: clean_excerpt(post['excerpt']),
      created_at: post['date'],
      link: post['URL']
    }
  end
end

#clean_excerpt(text) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/zooniverse_social/posts.rb', line 33

def clean_excerpt(text)
  CGI.unescapeHTML (text || '')
    .gsub(' ', '')
    .gsub('[…]', '')
    .gsub('<p>', '')
    .gsub('</p>', '')
    .strip
end

#updateObject



14
15
16
17
18
# File 'lib/zooniverse_social/posts.rb', line 14

def update
  blog_data = _update @blog_updater
  daily_data = _update @daily_updater
  @data = (blog_data + daily_data).sort{ |a, b| b[:created_at] <=> a[:created_at] }.take 3
end