Class: QiitaTrend::Trend

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

Overview

Qiitaのトレンドの機能を提供する

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

コンストラクタ

Parameters:

  • trend_type (TrendType) (defaults to: TrendType::NORMAL)

    トレンドタイプ

  • date (String) (defaults to: nil)

    「YYYYMMDD05」,「YYYYMMDD17」形式のどちらか

Raises:

  • (LoginFailureError)

    ログインに失敗した時に発生する

  • (NotExistsCacheError)

    存在しないキャッシュファイルを指定した時に発生する



21
22
23
24
25
26
27
28
# File 'lib/qiita_trend/trend.rb', line 21

def initialize(trend_type = TrendType::NORMAL, date = nil)
  @trend_type = trend_type
  page = Page.new(trend_type, date)
  parsed_html = Nokogiri::HTML.parse(page.html)
  xpath_str = get_xpath(trend_type)
  trends_data = JSON.parse(parsed_html.xpath(xpath_str)[0].text)
  @data = get_data(trends_data, trend_type)
end

Instance Attribute Details

#dataArray (readonly)

Returns トレンドデータ.

Returns:

  • (Array)

    トレンドデータ



12
13
14
# File 'lib/qiita_trend/trend.rb', line 12

def data
  @data
end

#trend_typeObject (readonly)

Returns the value of attribute trend_type.



13
14
15
# File 'lib/qiita_trend/trend.rb', line 13

def trend_type
  @trend_type
end

Instance Method Details

#itemsArray

Qiitaの対象のトレンドをすべて取得

Returns:

  • (Array)

    Qiitaの対象のトレンドすべて



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/qiita_trend/trend.rb', line 33

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'] = 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['published_at'] = trend['node']['publishedAt']
    result['likes_count'] = trend['node']['likesCount']
    result['is_new_arrival'] = trend['isNewArrival']
    value << result
  end
end

#new_itemsArray

Qiitaの対象のトレンドからNEWのものだけ取得 トレンドタイプがPERSONALの場合はNEWの概念が無いのでnilである

Returns:

  • (Array)

    Qiitaの対象のトレンドからNEWのものだけ



52
53
54
55
56
57
58
# File 'lib/qiita_trend/trend.rb', line 52

def new_items
  return nil if @trend_type == TrendType::PERSONAL

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