Class: QiitaTrend::Trend

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_trend/trend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trend_type = TrendType::DAILY, date = nil) ⇒ Trend

Returns a new instance of Trend.



11
12
13
14
15
16
17
# File 'lib/qiita_trend/trend.rb', line 11

def initialize(trend_type = TrendType::DAILY, date = nil)
  page = Page.new(trend_type, date)
  parsed_html = Nokogiri::HTML.parse(page.html)

  trends_data = JSON.parse(parsed_html.xpath('//div[@data-hyperapp-app="Trend"]')[0]['data-hyperapp-props'])
  @data = trends_data['trend']['edges']
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/qiita_trend/trend.rb', line 9

def data
  @data
end

Instance Method Details

#itemsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/qiita_trend/trend.rb', line 19

def items
  @data.each_with_object([]) do |trend, value|
    result = {}
    result['title'] = trend['node']['title']
    result['user_name'] = trend['node']['author']['urlName']
    result['user_image'] = trend['node']['author']['profileImageUrl']
    result['user_page'] = "#{Page::QIITA_URI}#{trend['node']['author']['urlName']}"
    result['article'] = "#{Page::QIITA_URI}#{trend['node']['author']['urlName']}/items/#{trend['node']['uuid']}"
    result['created_at'] = trend['node']['createdAt']
    result['likes_count'] = trend['node']['likesCount']
    result['is_new_arrival'] = trend['isNewArrival']
    value << result
  end
end

#new_itemsObject



34
35
36
37
38
# File 'lib/qiita_trend/trend.rb', line 34

def new_items
  items.select do |trend|
    trend['is_new_arrival'] == true
  end
end