qiita_trend 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b970140f428667c8c6c00f3604d41122bd0f79472cb161b07652d523e4e34cdc
4
- data.tar.gz: 7b2ade2e8b47322c4a75ff083ff1aa8ebcc54f3d91d505878f1041a214a48fed
3
+ metadata.gz: fa41464be0966877b547ee5d68effeeda6ea9e5151068dc1d2f16b0a5285f566
4
+ data.tar.gz: 5ffb824f9d816c58f5000eac1585081e9acf5daa2d3e670793e1b50e561e8866
5
5
  SHA512:
6
- metadata.gz: '001872aee63ed8b15019a022fdec168414d0bafc0edb8bdb0dfd3b13abc9af749e49bf513780e87828e31ce52bea9be7478c4f152490edc491d3e41fef14bf16'
7
- data.tar.gz: 4ea2f4ab5c792e03b41b18bdccf387da77196ab86f0964edf5b8896d73b85b7090d1b763f4565b11e1d05994cb1a0dfdbb5b452211fdd8feec9a428df70679ca
6
+ metadata.gz: e8238cc259394bee8cffd6bc5838928d1f729a1190f4a6794f46f186f024f890cf917bdbd433dd51cb95410bb5be38280db44d820336088fc2ad12d2dbceb1ad
7
+ data.tar.gz: 31649078cc8dac92dd22fa3f6c2986f62151561fbb86a3cfabae4339288e561fc880fd7e95675592108658e6d79486625d2a9829abc10e32046919d3113d87e8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qiita_trend (0.2.1)
4
+ qiita_trend (0.2.2)
5
5
  mechanize
6
6
  nokogiri (~> 1.9)
7
7
 
@@ -35,7 +35,7 @@ GEM
35
35
  mime-types-data (3.2019.0331)
36
36
  mini_portile2 (2.4.0)
37
37
  net-http-digest_auth (1.4.1)
38
- net-http-persistent (3.0.1)
38
+ net-http-persistent (3.1.0)
39
39
  connection_pool (~> 2.2)
40
40
  nokogiri (1.10.3)
41
41
  mini_portile2 (~> 2.4.0)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiitaTrend
4
+ class << self
5
+ def configure
6
+ yield(configuration)
7
+ end
8
+
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+ end
13
+
14
+ class Configuration
15
+ attr_accessor :user_name, :password
16
+
17
+ def initialize
18
+ @user_name = 'set user name'
19
+ @password = 'set password'
20
+ end
21
+ end
22
+ end
@@ -4,38 +4,43 @@ require 'mechanize'
4
4
 
5
5
  module QiitaTrend
6
6
  class Page
7
- attr_reader :html
7
+ attr_reader :target, :html, :cache
8
8
 
9
9
  QIITA_URI = 'https://qiita.com/'
10
+ QIITA_LOGIN_URI = 'https://qiita.com/login'
10
11
 
11
- def initialize(ua)
12
- @html = create_html(ua)
13
- end
12
+ def initialize(trend_type = TrendType::DAILY, date = nil)
13
+ @target = Target.new(trend_type, date)
14
+ @cache = Cache.new(target.cache)
14
15
 
15
- private
16
+ # 指定されたキャッシュファイルが存在しない場合は処理を終了
17
+ unless date.nil?
18
+ raise StandardError, '指定されたキャッシュファイルが存在しません' unless @cache.cached?
19
+ end
16
20
 
17
- def create_html(ua)
18
21
  # キャッシュが存在する場合はキャッシュから取得
19
- cache = QiitaTrend::Cache.new(target_trend + '.html')
20
- return cache.load_cache if cache.cached?
21
-
22
- # キャッシュが存在しない場合はキャッシュを作成しページ情報を取得する
23
- agent = Mechanize.new
24
- agent.user_agent_alias = ua
25
- page = agent.get QIITA_URI
26
- cache.create_cache(page.body)
22
+ @html = @cache.cached? ? @cache.load_cache : create_html(@target)
27
23
 
28
- page.body
24
+ # キャッシュが存在しない時はキャッシュを作成する
25
+ @cache.create_cache(@html) unless @cache.cached?
29
26
  end
30
27
 
31
- def target_trend
32
- if Time.now.hour >= 5 && Time.now.hour < 17
33
- Date.today.strftime('%Y%m%d') + '05'
34
- elsif Time.now.hour >= 17
35
- Date.today.strftime('%Y%m%d') + '17'
36
- elsif Time.now.hour < 5
37
- (Date.today - 1).strftime('%Y%m%d') + '17'
28
+ private
29
+
30
+ def create_html(target)
31
+ agent = Mechanize.new
32
+ agent.user_agent_alias = 'Mac Safari'
33
+
34
+ # ログイン処理
35
+ if target.need_login
36
+ form = agent.get(QIITA_LOGIN_URI).forms.first
37
+ form['identity'] = QiitaTrend.configuration.user_name
38
+ form['password'] = QiitaTrend.configuration.password
39
+ logged_page = form.submit
40
+ raise StandardError, 'ログインに失敗しました(ユーザー名とパスワードでログインできることを確認してください)' if logged_page.title.include?('Login')
38
41
  end
42
+
43
+ agent.get(target.url).body
39
44
  end
40
45
  end
41
46
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiitaTrend
4
+ class Target
5
+ attr_reader :type, :url, :need_login, :cache
6
+
7
+ def initialize(trend_type = TrendType::DAILY, date = nil)
8
+ @type = trend_type
9
+ @url = trend_url(trend_type)
10
+ @need_login = trend_type != TrendType::DAILY
11
+ @cache = cache_name(trend_type, date)
12
+ end
13
+
14
+ private
15
+
16
+ def trend_url(type)
17
+ case type
18
+ when TrendType::DAILY then 'https://qiita.com/'
19
+ when TrendType::WEEKLY then 'https://qiita.com/?scope=weekly'
20
+ when TrendType::MONTHLY then 'https://qiita.com/?scope=monthly'
21
+ end
22
+ end
23
+
24
+ def cache_name(type, date)
25
+ return "#{date}_#{type}.html" unless date.nil?
26
+
27
+ if Time.now.hour >= 5 && Time.now.hour < 17
28
+ "#{Date.today.strftime('%Y%m%d')}05_#{type}.html"
29
+ elsif Time.now.hour >= 17
30
+ "#{Date.today.strftime('%Y%m%d')}17_#{type}.html"
31
+ elsif Time.now.hour < 5
32
+ "#{(Date.today - 1).strftime('%Y%m%d')}17_#{type}.html"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -8,8 +8,8 @@ module QiitaTrend
8
8
  class Trend
9
9
  attr_reader :data
10
10
 
11
- def initialize(ua = 'Mac Safari')
12
- page = QiitaTrend::Page.new(ua)
11
+ def initialize(trend_type = TrendType::DAILY, date = nil)
12
+ page = Page.new(trend_type, date)
13
13
  parsed_html = Nokogiri::HTML.parse(page.html)
14
14
 
15
15
  trends_data = JSON.parse(parsed_html.xpath('//div[@data-hyperapp-app="Trend"]')[0]['data-hyperapp-props'])
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiitaTrend
4
+ class TrendType
5
+ DAILY = 'daily'
6
+ WEEKLY = 'weekly'
7
+ MONTHLY = 'monthly'
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QiitaTrend
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.2'
5
5
  end
data/lib/qiita_trend.rb CHANGED
@@ -4,6 +4,9 @@ require 'qiita_trend/version'
4
4
  require 'qiita_trend/trend'
5
5
  require 'qiita_trend/page'
6
6
  require 'qiita_trend/cache'
7
+ require 'qiita_trend/configuration'
8
+ require 'qiita_trend/trend_type'
9
+ require 'qiita_trend/target'
7
10
 
8
11
  module QiitaTrend
9
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiita_trend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - dodonki1223
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-29 00:00:00.000000000 Z
11
+ date: 2019-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -171,8 +171,11 @@ files:
171
171
  - bin/setup
172
172
  - lib/qiita_trend.rb
173
173
  - lib/qiita_trend/cache.rb
174
+ - lib/qiita_trend/configuration.rb
174
175
  - lib/qiita_trend/page.rb
176
+ - lib/qiita_trend/target.rb
175
177
  - lib/qiita_trend/trend.rb
178
+ - lib/qiita_trend/trend_type.rb
176
179
  - lib/qiita_trend/version.rb
177
180
  - qiita_trend.gemspec
178
181
  homepage: https://github.com/dodonki1223/qiita_trend