qiita_trend 0.2.7 → 0.3.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: 9430a869aefef938378e0c222578100dbd2b0d741e9dc72456785a9af315233d
4
- data.tar.gz: 3f338bddc0525275dc6238a16ff33cf872f732afe11a1b945ab7f0618149317a
3
+ metadata.gz: 86c478970ed3fc2c6b1d85799568243708a808abcf4bff7ecaf580a4746777ff
4
+ data.tar.gz: 1e8d0f15b5fbf4827b6f00e713cbdeb282ca454f0580341df52d2e43f1b14c79
5
5
  SHA512:
6
- metadata.gz: 642c8ebfc58b9816b694d18ce5d32cd4c37c390c2fde42b559eb20cd732c262f7cb9b7c3caee82e3924a87bfe1d5ebef08383befa88bd61ca533f70137a28205
7
- data.tar.gz: 35415ff58aa1923a2a2cfbde6607b0e2a9dafa21a1ccfcf70977f709d109574b677ff1ca5632dbc543fa6114b77859b611efc4fe7ad2bf8846681830af22e911
6
+ metadata.gz: 23fd963cf5d79abf44f61411207aff1fed090e70a4a0b7478e0456f19bfef324fe723134562afdf0115b41d70a507f72eddfbb7410009e8561d7d02959417923
7
+ data.tar.gz: 64c83098b9e44e58b1b3108d935e940095b7a9f58b628bff412e8c1d33a0098edc78826140e4775c84226629771ccddf5a4459914c14f4185eaadf4f3c1ee8e7
@@ -6,54 +6,59 @@ version: 2
6
6
  jobs:
7
7
  build:
8
8
  docker:
9
- # specify the version you desire here
10
- - image: circleci/ruby:2.6.0-node-browsers
9
+ - image: circleci/ruby:2.6.0
11
10
  environment:
12
- BUNDLER_VERSION: 2.0.1
13
-
14
- # Specify service dependencies here if necessary
15
- # CircleCI maintains a library of pre-built images
16
- # documented at https://circleci.com/docs/2.0/circleci-images/
17
- # - image: circleci/postgres:9.4
11
+ BUNDLE_PATH: vendor/bundle
18
12
 
19
13
  working_directory: ~/repo
20
14
 
21
15
  steps:
22
16
  - checkout
23
- # Download and cache dependencies
17
+
18
+ # Install Bundler
19
+ # ref:https://discuss.circleci.com/t/using-bundler-2-0-during-ci-fails/27411
20
+ - run:
21
+ name: Install Bundler
22
+ command: |
23
+ echo 'export BUNDLER_VERSION=$(cat Gemfile.lock | tail -1 | tr -d " ")' >> $BASH_ENV
24
+ source $BASH_ENV
25
+ gem install bundler
26
+
27
+ # Which version of bundler?
28
+ - run:
29
+ name: Which bundler?
30
+ command: bundle -v
31
+
32
+ # Restore bundle cache
33
+ # Read about caching dependencies: https://circleci.com/docs/2.0/caching/
24
34
  - restore_cache:
25
35
  keys:
26
36
  - v1-dependencies-{{ checksum "Gemfile.lock" }}
27
37
  # fallback to using the latest cache if no exact match is found
28
38
  - v1-dependencies-
29
- # install Bundler!
30
- # ref:https://discuss.circleci.com/t/using-bundler-2-0-during-ci-fails/27411
31
- - run:
32
- name: install bundler
33
- command: |
34
- sudo gem update --system
35
- sudo gem uninstall bundler
36
- sudo rm /usr/local/bin/bundle
37
- sudo rm /usr/local/bin/bundler
38
- sudo gem install bundler
39
- # install gem!
39
+
40
+ # Install gem
40
41
  - run:
41
- name: install gem
42
+ name: Install gem
42
43
  command: |
43
44
  # jobs=4は並列処理をして高速化するための設定(4つのjobで実行するって意味)
44
45
  bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
46
+
47
+ # Store bundle cache for Ruby dependencies
45
48
  - save_cache:
46
49
  paths:
47
50
  - ./vendor/bundle
48
51
  key: v1-dependencies-{{ checksum "Gemfile.lock" }}
49
- # run rubocop!
52
+
53
+ # Run RuboCop
50
54
  - run:
51
- name: run rubocop
55
+ name: Run RuboCop
52
56
  command: |
53
57
  bundle exec rubocop
54
- # run tests!
58
+
59
+ # Run tests
55
60
  - run:
56
- name: run tests
61
+ name: Run tests
57
62
  command: |
58
63
  TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
59
64
  bundle exec rspec \
@@ -62,7 +67,14 @@ jobs:
62
67
  --out test_results/rspec.xml \
63
68
  --format progress \
64
69
  $TEST_FILES
65
- # collect reports
70
+
71
+ # Create document
72
+ - run:
73
+ name: Create document
74
+ command: |
75
+ bundle exec yard
76
+
77
+ # Collect Reports
66
78
  - store_test_results:
67
79
  path: test_results
68
80
  - store_artifacts:
@@ -73,14 +85,68 @@ jobs:
73
85
  # カバレッジの結果をcoverage-resultsディレクトリに吐き出す
74
86
  path: coverage
75
87
  destination: coverage-results
76
- # deploy RubyGems
77
- - deploy:
88
+ - store_artifacts:
89
+ # ドキュメントの結果をyard-resultsディレクトリに吐き出す
90
+ path: ./doc
91
+ destination: yard-results
92
+
93
+ deploy:
94
+ docker:
95
+ - image: circleci/ruby:2.6.0
96
+
97
+ steps:
98
+ - checkout
99
+
100
+ # Install Bundler
101
+ # ref:https://discuss.circleci.com/t/using-bundler-2-0-during-ci-fails/27411
102
+ - run:
103
+ name: install Bundler
78
104
  command: |
79
- if [ "${CIRCLE_BRANCH}" == "master" ]; then
80
- mkdir ~/.gem
81
- curl -u dodonki1223:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
82
- git config user.name dodonki1223
83
- git config user.email $RUBYGEMS_EMAIL
84
- bundle exec rake build
85
- bundle exec rake release
86
- fi
105
+ echo 'export BUNDLER_VERSION=$(cat Gemfile.lock | tail -1 | tr -d " ")' >> $BASH_ENV
106
+ source $BASH_ENV
107
+ gem install bundler
108
+
109
+ # Which version of bundler?
110
+ - run:
111
+ name: Which bundler?
112
+ command: bundle -v
113
+
114
+ # Restore bundle cache
115
+ # Read about caching dependencies: https://circleci.com/docs/2.0/caching/
116
+ - restore_cache:
117
+ keys:
118
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
119
+ - v1-dependencies-
120
+
121
+ # Install gem
122
+ - run:
123
+ name: Install gem
124
+ command: bundle check --path vendor/bundle || bundle install
125
+
126
+ # Store bundle cache for Ruby dependencies
127
+ - save_cache:
128
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
129
+ paths:
130
+ - vendor/bundle
131
+
132
+ # Deploy RubyGems
133
+ - run:
134
+ name: Deploy RubyGems
135
+ command: |
136
+ curl -u dodonki1223:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
137
+ git config user.name dodonki1223
138
+ git config user.email $RUBYGEMS_EMAIL
139
+ bundle exec rake build
140
+ bundle exec rake release
141
+
142
+ workflows:
143
+ version: 2
144
+ build-and-deploy:
145
+ jobs:
146
+ - build
147
+ - deploy:
148
+ requires:
149
+ - build
150
+ filters:
151
+ branches:
152
+ only: master # masterブランチの時だけDeployJobを実行するようにする
data/.gitignore CHANGED
@@ -6,9 +6,11 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /vendor/
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
12
13
 
13
14
  /cache/
14
- /spec/vcr/
15
+ /spec/vcr/
16
+ /vendor/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qiita_trend (0.2.7)
4
+ qiita_trend (0.3.2)
5
5
  mechanize
6
6
  nokogiri (~> 1.9)
7
7
 
@@ -39,7 +39,7 @@ GEM
39
39
  net-http-digest_auth (1.4.1)
40
40
  net-http-persistent (3.1.0)
41
41
  connection_pool (~> 2.2)
42
- nokogiri (1.10.3)
42
+ nokogiri (1.10.4)
43
43
  mini_portile2 (~> 2.4.0)
44
44
  ntlm-http (0.1.1)
45
45
  parallel (1.17.0)
@@ -89,6 +89,7 @@ GEM
89
89
  crack (>= 0.3.2)
90
90
  hashdiff (>= 0.4.0, < 2.0.0)
91
91
  webrobots (0.1.2)
92
+ yard (0.9.20)
92
93
 
93
94
  PLATFORMS
94
95
  ruby
@@ -104,6 +105,7 @@ DEPENDENCIES
104
105
  simplecov
105
106
  vcr
106
107
  webmock
108
+ yard
107
109
 
108
110
  BUNDLED WITH
109
111
  2.0.2
data/README.md CHANGED
@@ -14,37 +14,23 @@ Qiitaのトレンドを10秒で取得することができます
14
14
  - 過去のトレンドを取得することができます(**あくまでもキャッシュファイルから復元するためキャッシュファイルの無い過去のトレンドは取得することができません**)
15
15
  - いいね数は性質上取得した時の時間に依存します。どうしても現在時刻のいいね数が欲しい場合はキャシュファイルを手動で削除してもう一度実行してください
16
16
 
17
- ## インストール
17
+ ## ドキュメント
18
18
 
19
- Add this line to your application's Gemfile:
19
+ [RubyDoc.info](https://rubydoc.info/github/dodonki1223/qiita_trend)
20
20
 
21
- ```ruby
22
- gem 'qiita_trend'
23
- ```
24
-
25
- And then execute:
26
-
27
- $ bundle
28
-
29
- Or install it yourself as:
21
+ ## 使用方法
30
22
 
31
- $ gem install qiita_trend
23
+ `gem install qiita_trend`を実行する
32
24
 
33
- ## 使用方法
25
+ ```shell
26
+ $ gem install qiita_trend
27
+ ```
34
28
 
35
29
  ### QiitaのDailyのトレンドを10秒で取得する
36
30
 
37
- ターミナルでコマンド`gem install qiita_trend`、`ruby -r qiita_trend -e "pp QiitaTrend::Trend.new.items"`を実行することでQiitaのトレンドを10秒で取得できます
31
+ ターミナルで`ruby -r qiita_trend -e "pp QiitaTrend::Trend.new.items"`を実行することでQiitaのトレンドを10秒(**gem install qiita_trendも含めて**)で取得できます
38
32
 
39
33
  ```shell
40
- $ gem install qiita_trend
41
- Fetching qiita_trend-0.2.3.gem
42
- Successfully installed qiita_trend-0.2.3
43
- Parsing documentation for qiita_trend-0.2.3
44
- Installing ri documentation for qiita_trend-0.2.3
45
- Done installing documentation for qiita_trend after 0 seconds
46
- 1 gem installed
47
-
48
34
  $ ruby -r qiita_trend -e "pp QiitaTrend::Trend.new.items"
49
35
  [{"title"=>"2行でwebpack.config.jsで補完を効かせる方法",
50
36
  "user_name"=>"akameco",
@@ -66,11 +52,7 @@ $ ruby -r qiita_trend -e "pp QiitaTrend::Trend.new.items"
66
52
  ...
67
53
  ```
68
54
 
69
- ### ワンライナーで使ってみる
70
-
71
- 先に`gem install qiita_trend`を実行しておくこと
72
-
73
- #### Dailyのトレンドの簡易表示
55
+ ### Dailyのトレンドの簡易表示
74
56
 
75
57
  index + タイトル名 + いいね数 + ユーザー名
76
58
 
@@ -85,7 +67,7 @@ $ ruby -r qiita_trend -e "QiitaTrend::Trend.new.items.each_with_index {|t, i| pu
85
67
  [5]畳み込みニューラルネットワークは何を見ているか(69) - okn-yu
86
68
  ```
87
69
 
88
- #### Dailyのトレンドのタイトル一覧
70
+ ### Dailyのトレンドのタイトル一覧
89
71
 
90
72
  ```shell
91
73
  $ ruby -r qiita_trend -e "QiitaTrend::Trend.new.items.each {|t| puts t['title']}"
@@ -98,7 +80,7 @@ KAGGLEでどこから手を付けていいか分からず学ぶことが多す
98
80
  畳み込みニューラルネットワークは何を見ているか
99
81
  ```
100
82
 
101
- #### Dailyのトレンドのうち`new`がついているものをブラウザで一括で開く
83
+ ### Dailyのトレンドのうち`new`がついているものをブラウザで一括で開く
102
84
 
103
85
  ```shell
104
86
  $ ruby -r qiita_trend -e "QiitaTrend::Trend.new.new_items.each {|t| system('open ' + t['article'])}"
@@ -129,6 +111,7 @@ p daily_trend.new_items
129
111
  ### Weekly、Monthlyのトレンドを取得する
130
112
 
131
113
  WeeklyとMonthlyのトレンドを取得する時はQiitaにログインしている必要があるため、ログイン出来るユーザーとパスワードの設定が必要です
114
+ ログインできないユーザー名とパスワードを指定している時は`LoginFailureError`の例外が発生します
132
115
 
133
116
  ```ruby
134
117
  # Qiitaにログインするためのユーザーとパスワードの設定をする
@@ -148,6 +131,25 @@ p monthly_trend.items
148
131
  p monthly_trend.new_items
149
132
  ```
150
133
 
134
+ ### キャッシュファイルからトレンドを取得する
135
+
136
+ キャッシュファイルが存在しない場合は`NotExistsCacheError`の例外が発生します
137
+ WeeklyもMonthlyの取得方法もDailyと同様です
138
+
139
+ ```ruby
140
+ # 2019年8月8日5時更新分のDailyのトレンドを取得する
141
+ daily_trend_05 = QiitaTrend::Trend.new(QiitaTrend::TrendType::DAILY, '2019080805')
142
+
143
+ p daily_trend_05.items
144
+ p daily_trend_05.new_items
145
+
146
+ # 2019年8月8日17時更新分のDailyのトレンドを取得する
147
+ daily_trend_17 = QiitaTrend::Trend.new(QiitaTrend::TrendType::DAILY, '2019080817')
148
+
149
+ p daily_trend_17.items
150
+ p daily_trend_17.new_items
151
+ ```
152
+
151
153
  ### itemsメソッド、new_itemsメソッドについて
152
154
 
153
155
  itemsメソッド、new_itemsメソッドは`Array`を返します
@@ -175,16 +177,57 @@ irb(main):001:0> pp QiitaTrend::Trend.new.items[0]
175
177
  "is_new_arrival"=>false}
176
178
  ```
177
179
 
178
- | key | 内容 | 備考 |
179
- |:--------------:|:-------------------------:|:---------------------:|
180
- | title | 記事タイトル | |
181
- | user_name | ユーザー名 | |
182
- | user_image | ユーザー画像URL | |
183
- | user_page | ユーザーページ | |
184
- | article | 記事のURL | |
185
- | created_at | 記事作成日 | |
186
- | likes_count | いいね数 | 数値が入ります |
187
- | is_new_arrival | 「NEW」のついている記事か | TrueかFalseが入ります |
180
+ <table>
181
+ <thead>
182
+ <tr>
183
+ <th>key</th>
184
+ <th>内容</th>
185
+ <th>備考</th>
186
+ </tr>
187
+ </thead>
188
+ <tbody>
189
+ <tr>
190
+ <td>title</td>
191
+ <td>記事タイトル</td>
192
+ <td></td>
193
+ </tr>
194
+ <tr>
195
+ <td>user_name</td>
196
+ <td>ユーザー名</td>
197
+ <td></td>
198
+ </tr>
199
+ <tr>
200
+ <td>user_image</td>
201
+ <td>ユーザー画像URL</td>
202
+ <td></td>
203
+ </tr>
204
+ <tr>
205
+ <td>user_page</td>
206
+ <td>ユーザーページ</td>
207
+ <td></td>
208
+ </tr>
209
+ <tr>
210
+ <td>article</td>
211
+ <td>記事のURL</td>
212
+ <td></td>
213
+ </tr>
214
+ <tr>
215
+ <td>created_at</td>
216
+ <td>記事作成日</td>
217
+ <td></td>
218
+ </tr>
219
+ <tr>
220
+ <td>likes_count</td>
221
+ <td>いいね数</td>
222
+ <td>数値が入ります</td>
223
+ </tr>
224
+ <tr>
225
+ <td>is_new_arrival</td>
226
+ <td>「NEW」のついている記事か</td>
227
+ <td>TrueかFalseが入ります</td>
228
+ </tr>
229
+ </tbody>
230
+ </table>
188
231
 
189
232
  ### キャシュファイルの出力先を変更する
190
233
 
@@ -7,6 +7,9 @@ require 'qiita_trend/cache'
7
7
  require 'qiita_trend/configuration'
8
8
  require 'qiita_trend/trend_type'
9
9
  require 'qiita_trend/target'
10
+ require 'qiita_trend/error/syntax_error'
11
+ require 'qiita_trend/error/login_failure_error'
12
+ require 'qiita_trend/error/not_exists_cache_error'
10
13
 
11
14
  module QiitaTrend
12
15
  end
@@ -1,14 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QiitaTrend
4
+ # Qiitaのトレンドをキャッシュする機能を提供する
4
5
  class Cache
5
- attr_reader :file_name, :directory, :full_path
6
+ # @return [String] ファイル名
7
+ attr_reader :file_name
8
+ # @return [String] キャッシュファイルを格納するディレクトリ(デフォルト値はDEFAULT_CACHE_DIRECTORYになります)
9
+ attr_reader :directory
10
+ # @return [String] キャッシュファイルへのフルパス
11
+ attr_reader :full_path
6
12
 
7
13
  # キャッシュファイルが格納されるデフォルトのディレクトリ
8
14
  DEFAULT_CACHE_DIRECTORY = Dir.home + '/qiita_cache/'
9
15
 
10
16
  # コンストラクタ
11
- # クラス内で使用するインスタンス変数をセットする
17
+ # Cacheクラスのインスタンスを返します
18
+ #
19
+ # @param [String] file_name ファイル名
20
+ # @param [String] directory キャッシュが格納する・されているディレクトリ
12
21
  def initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY)
13
22
  @file_name = file_name
14
23
  @directory = directory
@@ -16,7 +25,9 @@ module QiitaTrend
16
25
  end
17
26
 
18
27
  # キャッシュファイルを作成する
19
- # キャッシュフォルダが存在しない時は作成する
28
+ # ※キャッシュフォルダが存在しない場合は作成します
29
+ #
30
+ # @param [String] content 書き込む内容
20
31
  def create_cache(content)
21
32
  Dir.mkdir(@directory) unless Dir.exist?(@directory)
22
33
  File.open(@full_path, 'wb') do |file|
@@ -25,20 +36,18 @@ module QiitaTrend
25
36
  end
26
37
 
27
38
  # キャッシュファイルを読み込む
39
+ # File.openを使用しファイルを読み込みます
40
+ #
41
+ # @return [Object]
28
42
  def load_cache
29
43
  File.open(@full_path, 'r', &:read)
30
44
  end
31
45
 
32
- # キャッシュファイルが存在するか?
46
+ # キャッシュファイルが存在するかどうかを判定します
47
+ #
48
+ # @return [Boolean]
33
49
  def cached?
34
50
  File.exist?(@full_path)
35
51
  end
36
-
37
- # キャッシュファイルをクリアする
38
- def clear_cache
39
- File.delete(@full_path) if cached?
40
- rescue StandardError => e
41
- raise e.class, 'キャッシュファイルの削除に失敗しました'
42
- end
43
52
  end
44
53
  end
@@ -11,9 +11,17 @@ module QiitaTrend
11
11
  end
12
12
  end
13
13
 
14
+ # QiitaTrendのConfigの設定する機能を提供する
14
15
  class Configuration
15
- attr_accessor :user_name, :password, :cache_directory
16
+ # @return [String] Qiitaにログインするためのユーザー名
17
+ attr_accessor :user_name
18
+ # @return [String] Qiitaにログインするためのパスワード
19
+ attr_accessor :password
20
+ # @return [String] キャッシュファイルを保存するディレクトリ
21
+ attr_accessor :cache_directory
16
22
 
23
+ # コンストラクタ
24
+ # Configurationクラスのインスタンスを返します
17
25
  def initialize
18
26
  @user_name = nil
19
27
  @password = nil
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiitaTrend
4
+ module Error
5
+ # Qiitaへログイン失敗時に発生するエラークラス
6
+ class LoginFailureError < ::QiitaTrend::Error::SyntaxError
7
+ # エラーメッセージを返します
8
+ # ログインに失敗した時のユーザー名とパスワードを含んだ形でメッセージを返します
9
+ #
10
+ # @return [String] エラーメッセージ
11
+ def message
12
+ "Login failed, Please confilm user_name and password(user:#{user_name},password:#{password})"
13
+ end
14
+
15
+ # ログインに失敗した時のユーザー名を返します
16
+ #
17
+ # @return [String] ユーザー名(Configの設定したユーザー名)
18
+ def user_name
19
+ QiitaTrend.configuration.user_name
20
+ end
21
+
22
+ # ログインに失敗した時のパスワードを返します
23
+ #
24
+ # @return [String] パスワード(Configの設定したパスワード)
25
+ def password
26
+ QiitaTrend.configuration.password
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiitaTrend
4
+ module Error
5
+ # キャッシュファイルが存在しない時に発生するエラークラス
6
+ # キャッシュファイルからトレンドを取得する時、存在しないキャッシュファイルを指定した時に発生する
7
+ class NotExistsCacheError < ::QiitaTrend::Error::SyntaxError
8
+ # @return [Cache] Cacheクラス
9
+ attr_reader :cache
10
+
11
+ # コンストラクタ
12
+ #
13
+ # @param [Cache] cache キャッシュクラス
14
+ def initialize(cache)
15
+ @cache = cache
16
+ end
17
+
18
+ # エラーメッセージを返します
19
+ # 読み込もうとしたキャッシュファイルのフルパス含んだ形でメッセージを返します
20
+ #
21
+ # @return [String] エラーメッセージ
22
+ def message
23
+ "Does not exist cache file #{@cache.full_path}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiitaTrend
4
+ module Error
5
+ # qiita_trendのエラーの機能を提供する
6
+ class SyntaxError < ::StandardError
7
+ end
8
+ end
9
+ end
@@ -3,12 +3,26 @@
3
3
  require 'mechanize'
4
4
 
5
5
  module QiitaTrend
6
+ # Qiitaのページをスクレイピングしページ情報を取得する機能を提供する
6
7
  class Page
7
- attr_reader :target, :html, :cache
8
+ # @return [TrendType] トレンドタイプ(TrendType::DAILY,TrendType::WEEKLY,TrendType::MONTHLY)
9
+ attr_reader :target
10
+ # @return [String] Qiitaページのスクレイピング結果
11
+ attr_reader :html
12
+ # @return [Cache] Cacheクラス
13
+ attr_reader :cache
8
14
 
15
+ # QiitaのTOPページURL
9
16
  QIITA_URI = 'https://qiita.com/'
17
+ # QiitaのログインページURL
10
18
  QIITA_LOGIN_URI = 'https://qiita.com/login'
11
19
 
20
+ # コンストラクタ
21
+ #
22
+ # @param [TrendType] trend_type トレンドタイプ
23
+ # @param [String] date 「YYYYMMDD05」,「YYYYMMDD17」形式のどちらか
24
+ # @raise [LoginFailureError] ログインに失敗した時に発生する
25
+ # @raise [NotExistsCacheError] 存在しないキャッシュファイルを指定した時に発生する
12
26
  def initialize(trend_type = TrendType::DAILY, date = nil)
13
27
  @target = Target.new(trend_type, date)
14
28
  save_cache_directory = QiitaTrend.configuration.cache_directory.nil? ? Cache::DEFAULT_CACHE_DIRECTORY : QiitaTrend.configuration.cache_directory
@@ -16,7 +30,7 @@ module QiitaTrend
16
30
 
17
31
  # 指定されたキャッシュファイルが存在しない場合は処理を終了
18
32
  unless date.nil?
19
- raise StandardError, '指定されたキャッシュファイルが存在しません' unless @cache.cached?
33
+ raise Error::NotExistsCacheError, @cache unless @cache.cached?
20
34
  end
21
35
 
22
36
  # キャッシュが存在する場合はキャッシュから取得
@@ -28,20 +42,30 @@ module QiitaTrend
28
42
 
29
43
  private
30
44
 
45
+ # Qiitaのページをスクレイピングした結果を取得します
46
+ #
47
+ # @param [Target] target Targetクラス
48
+ # @raise [LoginFailureError] ログインに失敗した時に発生する
49
+ # @return [String] Qiitaをスクレイピングした結果
31
50
  def create_html(target)
32
51
  agent = Mechanize.new
33
52
  agent.user_agent_alias = 'Mac Safari'
53
+ login_qiita(agent) if target.need_login
54
+ agent.get(target.url).body
55
+ end
34
56
 
35
- # ログイン処理
36
- if target.need_login
37
- form = agent.get(QIITA_LOGIN_URI).forms.first
38
- form['identity'] = QiitaTrend.configuration.user_name
39
- form['password'] = QiitaTrend.configuration.password
40
- logged_page = form.submit
41
- raise StandardError, 'ログインに失敗しました(ユーザー名とパスワードでログインできることを確認してください)' if logged_page.title.include?('Login')
42
- end
57
+ # Qiitaへログインする
58
+ #
59
+ # @param [Mechanize] agent Mechanizeクラス
60
+ # @raise [LoginFailureError] ログインに失敗した時に発生する
61
+ def login_qiita(agent)
62
+ form = agent.get(QIITA_LOGIN_URI).forms.first
63
+ form['identity'] = QiitaTrend.configuration.user_name
64
+ form['password'] = QiitaTrend.configuration.password
65
+ logged_page = form.submit
43
66
 
44
- agent.get(target.url).body
67
+ # ページのタイトルにLoginが含まれていたらログイン失敗とする
68
+ raise Error::LoginFailureError if logged_page.title.include?('Login')
45
69
  end
46
70
  end
47
71
  end
@@ -1,9 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QiitaTrend
4
+ # Qiitaの対象のトレンドの機能を提供する
4
5
  class Target
5
- attr_reader :type, :url, :need_login, :cache
6
+ # @return [TrendType] トレンドタイプ(TrendType::DAILY,TrendType::WEEKLY,TrendType::MONTHLY)
7
+ attr_reader :type
8
+ # @return [String] トレンドを取得するQiitaのページURL
9
+ attr_reader :url
10
+ # @return [Boolean] Qiitaへのログインが必要かどうか
11
+ attr_reader :need_login
12
+ # @return [String] キャッシュファイル名
13
+ attr_reader :cache
6
14
 
15
+ # コンストラクタ
16
+ #
17
+ # @param [TrendType] trend_type トレンドタイプ
18
+ # @param [String] date 「YYYYMMDD05」,「YYYYMMDD17」形式のどちらか
7
19
  def initialize(trend_type = TrendType::DAILY, date = nil)
8
20
  @type = trend_type
9
21
  @url = trend_url(trend_type)
@@ -13,6 +25,10 @@ module QiitaTrend
13
25
 
14
26
  private
15
27
 
28
+ # トレンドのURLを取得する
29
+ #
30
+ # @param [TrendType] type トレンドタイプ
31
+ # @return [String] トレンドを取得するQiitaのページURL
16
32
  def trend_url(type)
17
33
  case type
18
34
  when TrendType::DAILY then 'https://qiita.com/'
@@ -21,6 +37,12 @@ module QiitaTrend
21
37
  end
22
38
  end
23
39
 
40
+ # キャッシュ名を取得する
41
+ # キャシュ名はYYYYMMDD05_daily.html,YYYYMMDD17_weekly.htmlなどの形式になります
42
+ # dateが指定されていなかったら現在時刻からキャッシュファイル名を作成し指定されていたらそのdateからキャッシュ名を取得する
43
+ #
44
+ # @param [TrendType] type トレンドタイプ
45
+ # @param [String] date 「YYYYMMDD05」,「YYYYMMDD17」形式のどちらか
24
46
  def cache_name(type, date)
25
47
  return "#{date}_#{type}.html" unless date.nil?
26
48
 
@@ -3,11 +3,20 @@
3
3
  require 'nokogiri'
4
4
  require 'open-uri'
5
5
  require 'json'
6
+ require 'uri'
6
7
 
7
8
  module QiitaTrend
9
+ # Qiitaのトレンドの機能を提供する
8
10
  class Trend
11
+ # @return [Array] トレンドデータ
9
12
  attr_reader :data
10
13
 
14
+ # コンストラクタ
15
+ #
16
+ # @param [TrendType] trend_type トレンドタイプ
17
+ # @param [String] date 「YYYYMMDD05」,「YYYYMMDD17」形式のどちらか
18
+ # @raise [LoginFailureError] ログインに失敗した時に発生する
19
+ # @raise [NotExistsCacheError] 存在しないキャッシュファイルを指定した時に発生する
11
20
  def initialize(trend_type = TrendType::DAILY, date = nil)
12
21
  page = Page.new(trend_type, date)
13
22
  parsed_html = Nokogiri::HTML.parse(page.html)
@@ -16,12 +25,15 @@ module QiitaTrend
16
25
  @data = trends_data['trend']['edges']
17
26
  end
18
27
 
28
+ # Qiitaの対象のトレンドをすべて取得
29
+ #
30
+ # @return [Array] Qiitaの対象のトレンドすべて
19
31
  def items
20
32
  @data.each_with_object([]) do |trend, value|
21
33
  result = {}
22
34
  result['title'] = trend['node']['title']
23
35
  result['user_name'] = trend['node']['author']['urlName']
24
- result['user_image'] = trend['node']['author']['profileImageUrl']
36
+ result['user_image'] = user_image(trend['node']['author']['profileImageUrl'])
25
37
  result['user_page'] = "#{Page::QIITA_URI}#{trend['node']['author']['urlName']}"
26
38
  result['article'] = "#{Page::QIITA_URI}#{trend['node']['author']['urlName']}/items/#{trend['node']['uuid']}"
27
39
  result['created_at'] = trend['node']['createdAt']
@@ -31,10 +43,30 @@ module QiitaTrend
31
43
  end
32
44
  end
33
45
 
46
+ # Qiitaの対象のトレンドからNEWのものだけ取得
47
+ #
48
+ # @return [Array] Qiitaの対象のトレンドからNEWのものだけ
34
49
  def new_items
35
50
  items.select do |trend|
36
51
  trend['is_new_arrival'] == true
37
52
  end
38
53
  end
54
+
55
+ private
56
+
57
+ # ユーザーの画像のURLを取得する
58
+ # URLデコードしimgix(画像ファイルの配信向けに特化したCDNサービス)のURLを排除する
59
+ # クエリーパラメータも排除する
60
+ #
61
+ # @return [String] ユーザーの画像のURL
62
+ def user_image(url)
63
+ # URLデコード
64
+ unescape_url = CGI.unescape(url)
65
+ # imgixのURLからユーザーの画像のURLへ変換する
66
+ exclusion_imgix = unescape_url.gsub!('https://qiita-user-profile-images.imgix.net/', '')
67
+ # クエリパラーメーを除いた形で返す
68
+ parse_url = URI.parse(exclusion_imgix)
69
+ "#{parse_url.host}#{parse_url.path}"
70
+ end
39
71
  end
40
72
  end
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QiitaTrend
4
+ # トレンドタイプの機能を提供する
4
5
  class TrendType
6
+ # トレンドタイプ:Daily
5
7
  DAILY = 'daily'
8
+ # トレンドタイプ:Weekly
6
9
  WEEKLY = 'weekly'
10
+ # トレンドタイプ:Monthly
7
11
  MONTHLY = 'monthly'
8
12
  end
9
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QiitaTrend
4
- VERSION = '0.2.7'
4
+ VERSION = '0.3.2'
5
5
  end
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['dodonki1223']
11
11
  spec.email = ['make.an.effort.wish.come.true@gmail.com']
12
12
 
13
- spec.summary = 'Get Qiita Trend'
14
- spec.description = 'Get Qiita Trend'
13
+ spec.summary = 'Easy to get trend for Qiita in 10 seconds'
14
+ spec.description = 'Easy to get trend for Qiita in 10 seconds'
15
15
  spec.homepage = 'https://github.com/dodonki1223/qiita_trend'
16
16
  spec.license = 'MIT'
17
17
 
@@ -50,4 +50,5 @@ Gem::Specification.new do |spec|
50
50
  spec.add_development_dependency 'simplecov'
51
51
  spec.add_development_dependency 'vcr'
52
52
  spec.add_development_dependency 'webmock'
53
+ spec.add_development_dependency 'yard'
53
54
  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.7
4
+ version: 0.3.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-08-06 00:00:00.000000000 Z
11
+ date: 2019-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -164,7 +164,21 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
- description: Get Qiita Trend
167
+ - !ruby/object:Gem::Dependency
168
+ name: yard
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Easy to get trend for Qiita in 10 seconds
168
182
  email:
169
183
  - make.an.effort.wish.come.true@gmail.com
170
184
  executables: []
@@ -186,6 +200,9 @@ files:
186
200
  - lib/qiita_trend.rb
187
201
  - lib/qiita_trend/cache.rb
188
202
  - lib/qiita_trend/configuration.rb
203
+ - lib/qiita_trend/error/login_failure_error.rb
204
+ - lib/qiita_trend/error/not_exists_cache_error.rb
205
+ - lib/qiita_trend/error/syntax_error.rb
189
206
  - lib/qiita_trend/page.rb
190
207
  - lib/qiita_trend/target.rb
191
208
  - lib/qiita_trend/trend.rb
@@ -212,8 +229,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
229
  - !ruby/object:Gem::Version
213
230
  version: '0'
214
231
  requirements: []
215
- rubygems_version: 3.0.4
232
+ rubygems_version: 3.0.1
216
233
  signing_key:
217
234
  specification_version: 4
218
- summary: Get Qiita Trend
235
+ summary: Easy to get trend for Qiita in 10 seconds
219
236
  test_files: []