Class: QiitaTrend::Page

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

Overview

Qiitaのページをスクレイピングしページ情報を取得する機能を提供する

Constant Summary collapse

QIITA_URI =

QiitaのTOPページURL

'https://qiita.com/'
QIITA_LOGIN_URI =

QiitaのログインページURL

'https://qiita.com/login'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

コンストラクタ

Parameters:

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

    トレンドタイプ

  • date (String) (defaults to: nil)

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

Raises:

  • (LoginFailureError)

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

  • (NotExistsCacheError)

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/qiita_trend/page.rb', line 26

def initialize(trend_type = TrendType::DAILY, date = nil)
  @target = Target.new(trend_type, date)
  save_cache_directory = QiitaTrend.configuration.cache_directory.nil? ? Cache::DEFAULT_CACHE_DIRECTORY : QiitaTrend.configuration.cache_directory
  @cache = Cache.new(target.cache, save_cache_directory)

  # 指定されたキャッシュファイルが存在しない場合は処理を終了
  unless date.nil?
    raise Error::NotExistsCacheError, @cache unless @cache.cached?
  end

  # キャッシュが存在する場合はキャッシュから取得
  @html = @cache.cached? ? @cache.load_cache : create_html(@target)

  # キャッシュが存在しない時はキャッシュを作成する
  @cache.create_cache(@html) unless @cache.cached?
end

Instance Attribute Details

#cacheCache (readonly)

Returns Cacheクラス.

Returns:

  • (Cache)

    Cacheクラス



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

def cache
  @cache
end

#htmlString (readonly)

Returns Qiitaページのスクレイピング結果.

Returns:

  • (String)

    Qiitaページのスクレイピング結果



11
12
13
# File 'lib/qiita_trend/page.rb', line 11

def html
  @html
end

#targetTrendType (readonly)

Returns トレンドタイプ(TrendType::DAILY,TrendType::WEEKLY,TrendType::MONTHLY).

Returns:

  • (TrendType)

    トレンドタイプ(TrendType::DAILY,TrendType::WEEKLY,TrendType::MONTHLY)



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

def target
  @target
end