class QiitaTrend::Trend
Qiitaのトレンドの機能を提供する
Attributes
data[R]
@return [Array] トレンドデータ
trend_type[R]
Public Class Methods
new(trend_type = TrendType::NORMAL, date = nil)
click to toggle source
コンストラクタ
@param [TrendType] trend_type
トレンドタイプ @param [String] date 「YYYYMMDD05」,「YYYYMMDD17」形式のどちらか @raise [LoginFailureError] ログインに失敗した時に発生する @raise [NotExistsCacheError] 存在しないキャッシュファイルを指定した時に発生する
# 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 = "//script[@data-component-name=\"#{data_component_name(trend_type)}\"]" trends_data = JSON.parse(parsed_html.xpath(xpath_str)[0].text) @data = get_data(trends_data, trend_type) end
Public Instance Methods
items()
click to toggle source
Qiitaの対象のトレンドをすべて取得
@return [Array] Qiitaの対象のトレンドすべて
# 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_items()
click to toggle source
Qiitaの対象のトレンドからNEWのものだけ取得 トレンドタイプがPERSONALの場合はNEWの概念が無いのでnilである
@return [Array] Qiitaの対象のトレンドからNEWのものだけ
# 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
Private Instance Methods
data_component_name(trend_type)
click to toggle source
QiitaのトレンドのFeed名を取得する
@param [TrendType] trend_type
トレンドタイプ @return [String] トレンドタイプによるFeed名
# File lib/qiita_trend/trend.rb, line 66 def data_component_name(trend_type) trend_type == TrendType::PERSONAL ? 'HomePersonalizedFeed' : 'HomeArticleTrendFeed' end
get_data(trends_data, trend_type)
click to toggle source
Qiitaのトレンドのデータを取得する
@param [Hash] trends_data トレンドデータ @param [TrendType] trend_type
トレンドタイプ @return [Array] トレンドタイプによるトレンドデータ
# File lib/qiita_trend/trend.rb, line 75 def get_data(trends_data, trend_type) trend_type == TrendType::PERSONAL ? trends_data['personalizedFeed']['edges'] : trends_data['trend']['edges'] end
user_image(url)
click to toggle source
ユーザーの画像のURLを取得する URLデコードしクエリーパラメータを排除する
@return [String] ユーザーの画像のURL
# File lib/qiita_trend/trend.rb, line 83 def user_image(url) # URLデコード unescape_url = CGI.unescape(url) # クエリパラーメーを除いた形で返す parse_url = URI.parse(unescape_url) "#{parse_url.scheme}://#{parse_url.host}#{parse_url.path}" end