hatenablog_publisher 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9da819e22994f68f8d47fb2af66cb776ab74866d09d0c6cc3970fbf8cb447ac6
4
+ data.tar.gz: 7fe584284a46990d727add2316a6c72920787ec4b48d6ccffa7ab8330e2a888f
5
+ SHA512:
6
+ metadata.gz: fc1c23cb700e7e4c2612dd185ec387266ed5736a590221afe0ecfd8ae91010dade2ebc0131f109b7401b083bebafd796419ad53d484b7e521b9d43ca4e94e1ca
7
+ data.tar.gz: '008ba7e176554d139f89a481b7ee2d8e4a9a175935b59284ec7d71c14d29d4ef27928594b562829166c7d5d8b7cdfa46833a0225722bc43f64be930a6e4c12f8'
@@ -0,0 +1,54 @@
1
+ ---
2
+ name: ci
3
+
4
+ on: [push, pull_request]
5
+
6
+ jobs:
7
+ test:
8
+ runs-on: ubuntu-latest
9
+ name: 'test'
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: 2.6
15
+ - uses: actions/cache@v1
16
+ with:
17
+ path: vendor/bundle
18
+ key: ${{ runner.os }}-gems-${{ hashFiles('./Gemfile.lock') }}
19
+ restore-keys: |
20
+ ${{ runner.os }}-gems-
21
+ - name: install gems
22
+ run: |
23
+ bundle config path vendor/bundle
24
+ bundle install --jobs 4 --retry 3
25
+ - name: test
26
+ env:
27
+ HATENABLOG_CONSUMER_KEY: dummy
28
+ HATENABLOG_CONSUMER_SECRET: dummy
29
+ HATENABLOG_ACCESS_TOKEN_SECRET: dummy
30
+ HATENABLOG_ACCESS_TOKEN: dummy
31
+ run: |
32
+ bundle exec rspec
33
+
34
+ rubocop:
35
+ runs-on: ubuntu-latest
36
+ name: 'rubocop'
37
+ steps:
38
+ - uses: actions/checkout@v2
39
+ - uses: ruby/setup-ruby@v1
40
+ with:
41
+ ruby-version: 2.6
42
+ - run: |
43
+ gem install rubocop
44
+
45
+ - name: reviewdog
46
+ env:
47
+ REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48
+ run: |
49
+ curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s
50
+ rubocop | ./bin/reviewdog -f=rubocop -reporter=github-pr-review
51
+ # - uses: reviewdog/action-rubocop@v1
52
+ # with:
53
+ # github_token: ${{ secrets.GITHUB_TOKEN }}
54
+ # reporter: github-pr-check
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,127 @@
1
+ require:
2
+
3
+ AllCops:
4
+ # Include:
5
+ Exclude:
6
+ - 'lib/**/*.rake'
7
+ - 'spec/**/*'
8
+ - 'bin/**/*'
9
+ - 'vendor/bundle/**/*'
10
+ - '*.gemspec'
11
+
12
+ Lint/AssignmentInCondition:
13
+ Enabled: false
14
+
15
+ Lint/UnusedMethodArgument:
16
+ Enabled: false
17
+
18
+ Lint/UselessAssignment:
19
+ Enabled: false
20
+
21
+ Lint/IneffectiveAccessModifier:
22
+ Enabled: false
23
+
24
+ Lint/AmbiguousBlockAssociation:
25
+ Enabled: false
26
+
27
+ Metrics/ClassLength:
28
+ Max: 100
29
+
30
+ Metrics/LineLength:
31
+ Max: 100
32
+
33
+ Metrics/MethodLength:
34
+ Max: 30
35
+
36
+ Metrics/BlockLength:
37
+ Max: 20
38
+ Exclude:
39
+ - 'lib/tasks/*'
40
+
41
+ Metrics/ModuleLength:
42
+ Max: 100
43
+
44
+ Metrics/CyclomaticComplexity:
45
+ Max: 6
46
+
47
+ Metrics/PerceivedComplexity:
48
+ Max: 7
49
+
50
+ Metrics/AbcSize:
51
+ Max: 20
52
+
53
+ Metrics/BlockNesting:
54
+ CountBlocks: false
55
+ Max: 3
56
+
57
+ Style/AndOr:
58
+ Enabled: true
59
+
60
+ Style/ClassAndModuleChildren:
61
+ Enabled: true
62
+
63
+ Style/Documentation:
64
+ Enabled: false
65
+
66
+ Style/NegatedIf:
67
+ Enabled: true
68
+
69
+ Style/TrailingCommaInArrayLiteral:
70
+ Enabled: true
71
+
72
+ Style/TrailingCommaInArguments:
73
+ Enabled: true
74
+
75
+ Naming/PredicateName:
76
+ NamePrefix:
77
+ - has_
78
+ - have_
79
+
80
+ Naming/AccessorMethodName:
81
+ Enabled: false
82
+
83
+ Style/FrozenStringLiteralComment:
84
+ Enabled: false
85
+
86
+ Style/AsciiComments:
87
+ Enabled: false
88
+
89
+ Style/DoubleNegation:
90
+ Enabled: true
91
+
92
+ Layout/SpaceBeforeFirstArg:
93
+ Enabled: false
94
+
95
+ Layout/MultilineOperationIndentation:
96
+ Enabled: true
97
+
98
+ Style/IdenticalConditionalBranches:
99
+ Enabled: true
100
+
101
+ Style/Lambda:
102
+ Enabled: true
103
+
104
+ Style/FormatString:
105
+ EnforcedStyle: sprintf
106
+
107
+ Style/IfInsideElse:
108
+ Enabled: true
109
+
110
+ Style/NumericLiterals:
111
+ Enabled: false
112
+
113
+ Style/NumericPredicate:
114
+ Enabled: true
115
+
116
+ Style/RegexpLiteral:
117
+ Enabled: true
118
+
119
+ Style/GuardClause:
120
+ Enabled: true
121
+
122
+ Metrics/ParameterLists:
123
+ Max: 4
124
+ CountKeywordArgs: true
125
+
126
+ Security/Eval:
127
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sawafuji.09@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hatenablog_publisher.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport'
7
+ gem 'awesome_print'
8
+ gem 'front_matter_parser'
9
+ gem 'mime-types'
10
+ gem 'oauth'
11
+ gem 'oga'
12
+ gem 'pry-byebug'
13
+ gem 'rake', '~> 12.0'
14
+ gem 'rspec', '~> 3.0'
15
+ gem 'rubocop'
16
+ gem 'sanitize'
17
+ gem 'xml-simple'
data/Gemfile.lock ADDED
@@ -0,0 +1,108 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hatenablog_publisher (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.0.3)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ zeitwerk (~> 2.2, >= 2.2.2)
15
+ ansi (1.5.0)
16
+ ast (2.4.0)
17
+ awesome_print (1.8.0)
18
+ byebug (11.1.3)
19
+ coderay (1.1.2)
20
+ concurrent-ruby (1.1.6)
21
+ crass (1.0.6)
22
+ diff-lcs (1.3)
23
+ front_matter_parser (0.2.1)
24
+ i18n (1.8.2)
25
+ concurrent-ruby (~> 1.0)
26
+ jaro_winkler (1.5.4)
27
+ method_source (1.0.0)
28
+ mime-types (3.3.1)
29
+ mime-types-data (~> 3.2015)
30
+ mime-types-data (3.2020.0425)
31
+ mini_portile2 (2.4.0)
32
+ minitest (5.14.0)
33
+ nokogiri (1.10.9)
34
+ mini_portile2 (~> 2.4.0)
35
+ nokogumbo (2.0.2)
36
+ nokogiri (~> 1.8, >= 1.8.4)
37
+ oauth (0.5.4)
38
+ oga (3.2)
39
+ ast
40
+ ruby-ll (~> 2.1)
41
+ parallel (1.19.1)
42
+ parser (2.7.1.2)
43
+ ast (~> 2.4.0)
44
+ pry (0.13.1)
45
+ coderay (~> 1.1)
46
+ method_source (~> 1.0)
47
+ pry-byebug (3.9.0)
48
+ byebug (~> 11.0)
49
+ pry (~> 0.13.0)
50
+ rainbow (3.0.0)
51
+ rake (12.3.3)
52
+ rexml (3.2.4)
53
+ rspec (3.9.0)
54
+ rspec-core (~> 3.9.0)
55
+ rspec-expectations (~> 3.9.0)
56
+ rspec-mocks (~> 3.9.0)
57
+ rspec-core (3.9.2)
58
+ rspec-support (~> 3.9.3)
59
+ rspec-expectations (3.9.1)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.9.0)
62
+ rspec-mocks (3.9.1)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.9.0)
65
+ rspec-support (3.9.3)
66
+ rubocop (0.82.0)
67
+ jaro_winkler (~> 1.5.1)
68
+ parallel (~> 1.10)
69
+ parser (>= 2.7.0.1)
70
+ rainbow (>= 2.2.2, < 4.0)
71
+ rexml
72
+ ruby-progressbar (~> 1.7)
73
+ unicode-display_width (>= 1.4.0, < 2.0)
74
+ ruby-ll (2.1.2)
75
+ ansi
76
+ ast
77
+ ruby-progressbar (1.10.1)
78
+ sanitize (5.1.0)
79
+ crass (~> 1.0.2)
80
+ nokogiri (>= 1.8.0)
81
+ nokogumbo (~> 2.0)
82
+ thread_safe (0.3.6)
83
+ tzinfo (1.2.7)
84
+ thread_safe (~> 0.1)
85
+ unicode-display_width (1.7.0)
86
+ xml-simple (1.1.5)
87
+ zeitwerk (2.3.0)
88
+
89
+ PLATFORMS
90
+ ruby
91
+
92
+ DEPENDENCIES
93
+ activesupport
94
+ awesome_print
95
+ front_matter_parser
96
+ hatenablog_publisher!
97
+ mime-types
98
+ oauth
99
+ oga
100
+ pry-byebug
101
+ rake (~> 12.0)
102
+ rspec (~> 3.0)
103
+ rubocop
104
+ sanitize
105
+ xml-simple
106
+
107
+ BUNDLED WITH
108
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 swfz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # HatenablogPublisher
2
+
3
+ Module to manage local markdown and images using Hatena Blog API and PhotoLife API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hatenablog_publisher'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hatenablog_publisher
20
+
21
+ ## Usage
22
+
23
+ - Patterns for writing CLI commands
24
+
25
+ ```
26
+ #!/usr/bin/env ruby
27
+ require 'hatenablog_publisher'
28
+ require 'active_support/core_ext/hash'
29
+ require 'optparse'
30
+
31
+ args = ARGV.getopts('',
32
+ 'draft',
33
+ 'user:',
34
+ 'site:',
35
+ 'ad_type:',
36
+ 'ad_file:',
37
+ 'filename:',
38
+ 'config:',
39
+ 'data_file:',
40
+ 'trace').symbolize_keys
41
+
42
+ HatenablogPublisher.publish(args)
43
+ ```
44
+
45
+ ### Option
46
+
47
+ #### required
48
+ - user
49
+ - posted by.
50
+
51
+ - site
52
+ - blog domain
53
+
54
+ - filename
55
+ - markdown file
56
+
57
+ #### optional
58
+
59
+ - ad_type
60
+ - image
61
+ - html
62
+
63
+ - ad_file
64
+ - YAML syntax
65
+
66
+ - config
67
+ - A file containing command line options
68
+ - YAML format
69
+
70
+ - data_file
71
+ - for article management
72
+ - JSON format
73
+
74
+ - trace
75
+ - Detailed Output
76
+
77
+ ### Ad Content
78
+
79
+ require `ad_type` and `ad_file`
80
+
81
+ - `ad_file`
82
+
83
+ Insert a pre-defined ad tag (assuming you are an Amazon associate) at the end of the article
84
+
85
+ e.g.)
86
+
87
+ ```
88
+ ---
89
+ AWS:
90
+ - name: Book Name
91
+ html: '<iframe ........'
92
+ image: '<a target.......'
93
+ - name: Book Name2
94
+ html: '<iframe ........'
95
+ image: '<a target.......'
96
+ Sample:
97
+ - name: Book Name3
98
+ html: '<iframe ........'
99
+ image: '<a target.......'
100
+ ```
101
+
102
+ ### Use Config File
103
+
104
+ default: hatenablog_publisher_config.yml
105
+
106
+ If you want to specify
107
+
108
+ ```
109
+ --config my_config.yml
110
+ ```
111
+
112
+ available ERB syntax
113
+
114
+ e.g)
115
+
116
+ ```
117
+ consumer_key: <%= ENV['HATENABLOG_CONSUMER_KEY'] %>
118
+ consumer_secret: <%= ENV['HATENABLOG_CONSUMER_SECRET'] %>
119
+ access_token: <%= ENV['HATENABLOG_ACCESS_TOKEN'] %>
120
+ access_token_secret: <%= ENV['HATENABLOG_ACCESS_TOKEN_SECRET'] %>
121
+ user: hoge
122
+ site: hoge.hatenablog.jp
123
+ ```
124
+
125
+ ### If you want to manage the data in a markdown file
126
+
127
+ In the first line of the markdown, write the following
128
+
129
+ front_matter format
130
+
131
+ - sample.md
132
+
133
+ ```
134
+ ---
135
+ title: sample markdown
136
+ category:
137
+ - Markdown
138
+ - Sample
139
+ ```
140
+
141
+ after published, data such as article ID will be added to the file.
142
+
143
+ ### If you want to manage your data in a custom data file
144
+
145
+ Data such as article and image IDs are recorded in markdown by default
146
+
147
+ If you are managing the article data with confidence, you can specify an article data management file to record the data
148
+
149
+ ```
150
+ hatenablog_publisher --data_file article_data.json
151
+ ```
152
+
153
+ It must be written in the following JSON format
154
+
155
+ e.g)
156
+
157
+ - data.json
158
+
159
+ ```
160
+ [
161
+ {
162
+ "title": "Article Title",
163
+ "category": [
164
+ "Sample",
165
+ "Markdown"
166
+ ]
167
+ },
168
+ {
169
+ "title": "Article Title2",
170
+ "category": [
171
+ "Sample",
172
+ "Markdown"
173
+ ]
174
+ },
175
+ .....
176
+ .....
177
+ .....
178
+ ]
179
+ ```
180
+
181
+ after published, data such as article ID will be added to the file.
182
+
183
+
184
+ ## Contributing
185
+
186
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hatenablog_publisher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/hatenablog_publisher/blob/master/CODE_OF_CONDUCT.md).
187
+
188
+
189
+ ## License
190
+
191
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
192
+
193
+ ## Code of Conduct
194
+
195
+ Everyone interacting in the HatenablogPublisher project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/hatenablog_publisher/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hatenablog_publisher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'hatenablog_publisher'
4
+ require 'optparse'
5
+ require 'active_support/core_ext/hash'
6
+
7
+ args = ARGV.getopts('',
8
+ 'draft',
9
+ 'user:',
10
+ 'site:',
11
+ 'ad_type:',
12
+ 'ad_file:',
13
+ 'filename:',
14
+ 'config:',
15
+ 'data_file:',
16
+ 'trace').symbolize_keys
17
+
18
+ HatenablogPublisher.publish(args)
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/hatenablog_publisher/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "hatenablog_publisher"
5
+ spec.version = HatenablogPublisher::VERSION
6
+ spec.authors = ["swfz"]
7
+ spec.email = ["sawafuji.09@gmail.com"]
8
+
9
+ spec.summary = %q{Gem that posts to the Hatena Blog API and PhotoLife API}
10
+ spec.homepage = "https://github.com/swfz/hatenablog_publisher"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/swfz/hatenablog_publisher"
16
+ spec.metadata["changelog_uri"] = "https://github.com/swfz/hatenablog_publisher"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,34 @@
1
+ require 'oauth'
2
+ require 'json'
3
+
4
+ module HatenablogPublisher
5
+ class Api
6
+ attr_reader :client, :header, :site
7
+
8
+ def initialize(site)
9
+ @site = site
10
+
11
+ @header = {
12
+ 'Accept' => 'application/xml',
13
+ 'Content-Type' => 'application/xml'
14
+ }
15
+
16
+ consumer = OAuth::Consumer.new(
17
+ ENV['HATENABLOG_CONSUMER_KEY'],
18
+ ENV['HATENABLOG_CONSUMER_SECRET'],
19
+ site: site,
20
+ timeout: 300
21
+ )
22
+
23
+ @client = OAuth::AccessToken.new(
24
+ consumer,
25
+ ENV['HATENABLOG_ACCESS_TOKEN'],
26
+ ENV['HATENABLOG_ACCESS_TOKEN_SECRET']
27
+ )
28
+ end
29
+
30
+ def request(path, body, method = :post)
31
+ @client.request(method, path, body, @header)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ require 'oauth'
2
+ require 'json'
3
+ require 'awesome_print'
4
+
5
+ module HatenablogPublisher
6
+ class Client
7
+ attr_reader :context
8
+
9
+ IMAGE_PATTERN = /[^`]!\[.*\]\((.*)\)/.freeze
10
+
11
+ def initialize(args)
12
+ @options = HatenablogPublisher::Options.create(args)
13
+ io = HatenablogPublisher::Io.new(@options)
14
+ @context = HatenablogPublisher::Context.new(io)
15
+ end
16
+
17
+ def publish
18
+ image_tags = @context.text.scan(IMAGE_PATTERN).flatten
19
+ photolife = HatenablogPublisher::Photolife.new
20
+ dirname = File.dirname(@options.filename)
21
+
22
+ image_tags.each do |tag|
23
+ next if @context.posted_image?(tag)
24
+
25
+ image = HatenablogPublisher::Image.new(File.join(dirname, tag))
26
+ image_hash = photolife.upload(image)
27
+ @context.add_image_context(image_hash, tag)
28
+ @context.reload_context
29
+ end
30
+
31
+ body = generate_body
32
+
33
+ entry = HatenablogPublisher::Entry.new(@context, @options)
34
+ entry_hash = entry.post_entry(body)
35
+ @context.add_entry_context(entry_hash)
36
+ @context.reload_context
37
+ end
38
+
39
+ def generate_body
40
+ generator = HatenablogPublisher::Generator::Body.new(@context, @options)
41
+ body = generator.generate
42
+
43
+ if @options.ad_type && @options.ad_file
44
+ category = HatenablogPublisher::FixedContent::Ad.new(@context.categories, @options)
45
+ body += category.format_body
46
+ end
47
+
48
+ body
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,60 @@
1
+ require 'yaml'
2
+ require 'front_matter_parser'
3
+ require 'active_support/core_ext/hash'
4
+
5
+ module HatenablogPublisher
6
+ class Context
7
+ attr_reader :title, :categories, :text, :hatena
8
+
9
+ def initialize(io)
10
+ @io = io
11
+
12
+ read_context
13
+ end
14
+
15
+ def reload_context
16
+ write_context
17
+ read_context
18
+ end
19
+
20
+ def add_image_context(image, tag)
21
+ syntax = "[#{image['syntax'].first}]"
22
+
23
+ @hatena ||= {}
24
+ @hatena[:image] ||= {}
25
+ @hatena[:image][tag.to_sym] = {
26
+ syntax: syntax,
27
+ id: image['id'].first,
28
+ image_url: image['imageurl'].first
29
+ }
30
+ end
31
+
32
+ def image_syntax(tag)
33
+ @hatena.dig(:image, tag.to_sym, :syntax)
34
+ end
35
+
36
+ def posted_image?(tag)
37
+ image_syntax(tag).present?
38
+ end
39
+
40
+ def add_entry_context(entry)
41
+ @hatena ||= {}
42
+ @hatena[:id] = entry['id'].first.split('-').last
43
+ end
44
+
45
+ private
46
+
47
+ def write_context
48
+ @io.write(title: @title, category: @categories, hatena: @hatena, text: @text)
49
+ end
50
+
51
+ def read_context
52
+ data, text = @io.read
53
+
54
+ @text = text
55
+ @categories = data[:category]
56
+ @title = data[:title]
57
+ @hatena = data[:hatena] || {}
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,69 @@
1
+ require 'oga'
2
+ require 'hatenablog_publisher/request_logger'
3
+
4
+ module HatenablogPublisher
5
+ class Entry
6
+ include HatenablogPublisher::RequestLogger
7
+
8
+ attr_reader :client, :context, :options
9
+
10
+ ENDPOINT = 'https://blog.hatena.ne.jp'.freeze
11
+
12
+ def initialize(context, options)
13
+ @client = HatenablogPublisher::Api.new(ENDPOINT)
14
+ @context = context
15
+ @options = options
16
+ end
17
+
18
+ def post_entry(body)
19
+ request_xml = format_request(body)
20
+ basename = File.basename(@options.filename)
21
+
22
+ res = with_logging_request(basename, request_xml) do
23
+ method = @context.hatena.dig(:id) ? :put : :post
24
+ @client.request(api_url, request_xml, method)
25
+ end
26
+
27
+ parse_response(res.body)
28
+ end
29
+
30
+ private
31
+
32
+ def api_url
33
+ id = @context.hatena.dig(:id) ? '/' + @context.hatena[:id] : ''
34
+ ap @options
35
+ "#{ENDPOINT}/#{@options.user}/#{@options.site}/atom/entry#{id}"
36
+ end
37
+
38
+ def parse_response(response_body)
39
+ XmlSimple.xml_in(response_body)
40
+ end
41
+
42
+ def categories
43
+ @context.categories.map do |c|
44
+ '<category term="' + c + '" />'
45
+ end.join
46
+ end
47
+
48
+ def format_request(body)
49
+ draft = @options.draft ? 'yes' : 'no'
50
+ body = <<~"XML"
51
+ <?xml version="1.0" encoding="utf-8"?>
52
+ <entry xmlns="http://www.w3.org/2005/Atom"
53
+ xmlns:app="http://www.w3.org/2007/app">
54
+ <title>#{@context.title}</title>
55
+ <author><name>#{@options.user}</name></author>
56
+ <content type="text/plain">
57
+ #{body}
58
+ </content>
59
+ <updated>#{Time.now.strftime('%Y-%m-%dT%H:%M%S')}</updated>
60
+ #{categories}
61
+ <app:control>
62
+ <app:draft>#{draft}</app:draft>
63
+ </app:control>
64
+ </entry>
65
+ XML
66
+ body
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,23 @@
1
+ module HatenablogPublisher
2
+ module FixedContent
3
+ class Ad
4
+ MAX_AD_SIZE = 3
5
+
6
+ def initialize(category, options)
7
+ @mapping = YAML.load_file(options.ad_file)
8
+ @options = options
9
+ @category = category
10
+ end
11
+
12
+ def format_body
13
+ "\n\n\n" + CGI.escapeHTML(sample_items.join("\n"))
14
+ end
15
+
16
+ def sample_items
17
+ @mapping.slice(*@category).map { |_, v| v }.flatten.sample(MAX_AD_SIZE).map do |r|
18
+ r[@options.ad_type]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ module HatenablogPublisher
2
+ module Generator
3
+ class Body
4
+ attr_reader :context, :options
5
+
6
+ IMAGE_PATTERN = /[^`]!\[.*\]\((.*)\)/.freeze
7
+
8
+ def initialize(context, options)
9
+ @context = context
10
+ @options = options
11
+ end
12
+
13
+ def generate
14
+ markdown = @context.text.dup
15
+
16
+ replaced_markdown = replace_image(markdown)
17
+
18
+ CGI.escapeHTML(replaced_markdown)
19
+ end
20
+
21
+ def replace_image(markdown)
22
+ markdown.gsub(IMAGE_PATTERN) do
23
+ "\n\n" + @context.image_syntax(Regexp.last_match(1))
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'base64'
2
+ require 'mime/types'
3
+
4
+ module HatenablogPublisher
5
+ class Image
6
+ attr_reader :filepath
7
+
8
+ def initialize(filepath)
9
+ @filepath = filepath
10
+ end
11
+
12
+ def mime_type
13
+ MIME::Types.type_for(@filepath).first
14
+ end
15
+
16
+ def content
17
+ Base64.encode64(File.read(@filepath))
18
+ end
19
+
20
+ def title
21
+ File.basename(@filepath, '.*')
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,72 @@
1
+ module HatenablogPublisher
2
+ class Io
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def data_file?
8
+ data_file.present?
9
+ end
10
+
11
+ def data_file
12
+ @options.data_file
13
+ end
14
+
15
+ def write(title:, category:, hatena:, text:)
16
+ if data_file?
17
+ write_data_file(title: title, category: category, hatena: hatena)
18
+ else
19
+ write_file(title: title, category: category, hatena: hatena, text: text)
20
+ end
21
+ end
22
+
23
+ def write_data_file(title:, category:, hatena:)
24
+ data = JSON.parse(File.read(@options.data_file))
25
+ data.each do |l|
26
+ next unless l['filepath'] == @options.filename
27
+
28
+ l.merge!(
29
+ title: title,
30
+ category: category,
31
+ hatena: hatena
32
+ )
33
+ end
34
+ File.write(data_file, JSON.pretty_generate(data, indent: ' ', space_before: ' '))
35
+ end
36
+
37
+ def write_file(title:, category:, hatena:, text:)
38
+ filename = @options.filename
39
+ parsed = FrontMatterParser::Parser.parse_file(filename)
40
+ front_matter = parsed.front_matter.deep_symbolize_keys.merge(
41
+ title: title,
42
+ category: category,
43
+ hatena: hatena
44
+ )
45
+ body = YAML.dump(front_matter.deep_stringify_keys) + "\n---\n\n" + text
46
+ File.write(filename, body)
47
+ end
48
+
49
+ def read
50
+ filename = @options.filename
51
+
52
+ if data_file?
53
+ read_from_datafile(filename)
54
+ else
55
+ read_from_file(filename)
56
+ end
57
+ end
58
+
59
+ def read_from_datafile(filename)
60
+ json = JSON.parse(File.read(data_file))
61
+ target = json.find { |l| l['filepath'] == filename }
62
+
63
+ [target.deep_symbolize_keys, File.read(filename)]
64
+ end
65
+
66
+ def read_from_file(filename)
67
+ parsed = FrontMatterParser::Parser.parse_file(filename)
68
+
69
+ [parsed.front_matter.deep_symbolize_keys, parsed.content]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,41 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+ require 'ostruct'
4
+ module HatenablogPublisher
5
+ class Options < OpenStruct
6
+ attr_reader :args
7
+
8
+ REQUIRE_KEYS = %i[
9
+ consumer_key
10
+ consumer_secret
11
+ access_token
12
+ access_token_secret
13
+ user
14
+ site
15
+ filename
16
+ ].freeze
17
+
18
+ def valid_or_raise
19
+ config_keys = to_h.keys
20
+ key_is_set = ->(key) { config_keys.include?(key) && !to_h[key].nil? }
21
+ unless (lacking_keys = REQUIRE_KEYS.reject { |key| key_is_set.call(key) }).empty?
22
+ raise "Following keys are not setup. #{lacking_keys.map(&:to_s)}"
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ class << self
29
+ def create(args)
30
+ config_file = args[:config] || './hatenablog_publisher_config.yml'
31
+ from_file = if File.exist?(config_file)
32
+ YAML.safe_load(ERB.new(File.read(config_file)).result)
33
+ else
34
+ {}
35
+ end
36
+ config = new(from_file.symbolize_keys.merge(args) { |_k, o, n| n.nil? ? o : n })
37
+ config.valid_or_raise
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ require 'xmlsimple'
2
+
3
+ module HatenablogPublisher
4
+ class Photolife
5
+ include HatenablogPublisher::RequestLogger
6
+
7
+ attr_reader :client
8
+
9
+ ENDPOINT = 'https://f.hatena.ne.jp'.freeze
10
+
11
+ def initialize
12
+ @client = HatenablogPublisher::Api.new(ENDPOINT)
13
+ end
14
+
15
+ def upload(image)
16
+ body = format_body(image)
17
+
18
+ res = with_logging_request(image.title, body) do
19
+ @client.request('/atom/post', body)
20
+ end
21
+
22
+ parse_response(res.body)
23
+ end
24
+
25
+ private
26
+
27
+ def parse_response(response_body)
28
+ XmlSimple.xml_in(response_body)
29
+ end
30
+
31
+ def format_body(image, dirname = 'Hatena Blog')
32
+ body = <<~"XML"
33
+ <entry xmlns="http://purl.org/atom/ns#">
34
+ <title>#{image.title}</title>
35
+ <content mode="base64" type="#{image.mime_type}">
36
+ #{image.content}
37
+ </content>
38
+ <dc:subject>#{dirname}</dc:subject>
39
+ </entry>
40
+ XML
41
+
42
+ body
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ module HatenablogPublisher
2
+ module RequestLogger
3
+ def with_logging_request(identifier, request_body, &block)
4
+ log("#{identifier}-request", request_body)
5
+ res = yield
6
+ log("#{identifier}-response", res.body)
7
+
8
+ res
9
+ end
10
+
11
+ def log(identifier, text)
12
+ Dir.mkdir('tmp') unless Dir.exist?('tmp')
13
+
14
+ classname = self.class.to_s.demodulize.downcase
15
+ filepath = "tmp/hatenablog_publisher-#{classname}-#{identifier}.xml"
16
+ File.write(filepath, text)
17
+ p "[Info] #{classname}: #{identifier}. output log -> #{filepath}."
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module HatenablogPublisher
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'hatenablog_publisher/version'
2
+ require 'hatenablog_publisher/client'
3
+ require 'hatenablog_publisher/entry'
4
+ require 'hatenablog_publisher/photolife'
5
+ require 'hatenablog_publisher/image'
6
+ require 'hatenablog_publisher/context'
7
+ require 'hatenablog_publisher/options'
8
+ require 'hatenablog_publisher/api'
9
+ require 'hatenablog_publisher/generator/body'
10
+ require 'hatenablog_publisher/request_logger'
11
+ require 'hatenablog_publisher/fixed_content/ad'
12
+ require 'hatenablog_publisher/io'
13
+
14
+ module HatenablogPublisher
15
+ class Error < StandardError; end
16
+
17
+ class << self
18
+ def publish(args)
19
+ HatenablogPublisher::Client.new(args).publish
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hatenablog_publisher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - swfz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - sawafuji.09@gmail.com
16
+ executables:
17
+ - hatenablog_publish
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/ci.yml"
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".rubocop.yml"
25
+ - ".travis.yml"
26
+ - CODE_OF_CONDUCT.md
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/console
33
+ - bin/setup
34
+ - exe/hatenablog_publish
35
+ - hatenablog_publisher.gemspec
36
+ - lib/hatenablog_publisher.rb
37
+ - lib/hatenablog_publisher/api.rb
38
+ - lib/hatenablog_publisher/client.rb
39
+ - lib/hatenablog_publisher/context.rb
40
+ - lib/hatenablog_publisher/entry.rb
41
+ - lib/hatenablog_publisher/fixed_content/ad.rb
42
+ - lib/hatenablog_publisher/generator/body.rb
43
+ - lib/hatenablog_publisher/image.rb
44
+ - lib/hatenablog_publisher/io.rb
45
+ - lib/hatenablog_publisher/options.rb
46
+ - lib/hatenablog_publisher/photolife.rb
47
+ - lib/hatenablog_publisher/request_logger.rb
48
+ - lib/hatenablog_publisher/version.rb
49
+ homepage: https://github.com/swfz/hatenablog_publisher
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/swfz/hatenablog_publisher
54
+ source_code_uri: https://github.com/swfz/hatenablog_publisher
55
+ changelog_uri: https://github.com/swfz/hatenablog_publisher
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.3.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.0.1
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Gem that posts to the Hatena Blog API and PhotoLife API
75
+ test_files: []