gist_updater 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5eebf362cbfac0fde77291183280648a756e2ce0
4
- data.tar.gz: ace6e7aa424f322d0eea4c02d28767ebd9975ebd
3
+ metadata.gz: 859a672a39a6718364468dcfc00bc952ca1b66e8
4
+ data.tar.gz: 964adf30b91cb82e39b2477a4835df45a38a86b4
5
5
  SHA512:
6
- metadata.gz: 12e5950a74565d6c28362b26f602b6466616df71e965f002fc753cd3088ce5f4dd452e7d8374ba26e773437d9595661cbc0ab25df3341bbd81ba2a8baf578093
7
- data.tar.gz: 4eb1d19014098ffe00c1449fd3b78253b255fc69ef735ff57e2f880a051a537cdc54874b08702ae5facb4dc82d9d5cd3986f997c3a6915d5d4b34f29767b2bcf
6
+ metadata.gz: 80a86501c6e476f35082c3e8f287be1769d80875326755c9d28b10e156776ad8be785ef4fdc9815a65c3b0b4f355d4e7d783e74ce0e2109609902dbd17dbe8e4
7
+ data.tar.gz: a117929285e50fddc3ac71bbf6f7b94c160bc39b567746736999e515a12c2ecf4b310f30a3b6668d82e5e0c6f4122c1e75075cf8f3a99c8f8886210e80a8a19f
data/README.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # GistUpdater
2
2
 
3
+ [![Build Status](https://img.shields.io/circleci/project/masutaka/gist_updater/master.svg?maxAge=2592000?style=flat-square)][circleci]
4
+ [![License](https://img.shields.io/github/license/masutaka/gist_updater.svg?maxAge=2592000?style=flat-square)][license]
5
+ [![Gem](https://img.shields.io/gem/v/gist_updater.svg?maxAge=2592000?style=flat-square)][gem-link]
6
+
7
+ [circleci]: https://circleci.com/gh/masutaka/gist_updater
8
+ [license]: https://github.com/masutaka/gist_updater/blob/master/LICENSE.txt
9
+ [gem-link]: http://badge.fury.io/rb/gist_updater
10
+
11
+ ## Description
12
+
3
13
  Updates your Gist files which are defined in YAML
4
14
 
5
15
  ## Installation
@@ -20,6 +30,8 @@ Or install it yourself as:
20
30
 
21
31
  ## Usage
22
32
 
33
+ ### CLI
34
+
23
35
  ```
24
36
  gist_updater commands:
25
37
  gist_updater help [COMMAND] # Describe available commands or one specific command
@@ -34,7 +46,21 @@ Options:
34
46
  d, [--debug], [--no-debug] # Debug mode
35
47
  ```
36
48
 
37
- ### YAML format
49
+ ### Script
50
+
51
+ ```ruby
52
+ require 'gist_updater'
53
+
54
+ GistUpdater::Updater.new(
55
+ yaml: <User definition YAML file>,
56
+ user: <GitHub usename>, # or $GISTUPDATER_USER
57
+ token: <GitHub personal access token>, # or $GISTUPDATER_ACCESS_TOKEN
58
+ debug: <true or false>
59
+ ).update
60
+ #=> update count
61
+ ```
62
+
63
+ ## YAML format
38
64
 
39
65
  ```yaml
40
66
  -
@@ -66,7 +92,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
66
92
 
67
93
  ## Contributing
68
94
 
69
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gist_updater. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/masutaka/gist_updater. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
70
96
 
71
97
 
72
98
  ## License
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'thor'
4
- require 'yaml'
5
4
 
6
5
  module GistUpdater
7
6
  class Commands < Thor
@@ -19,45 +18,12 @@ module GistUpdater
19
18
 
20
19
  desc 'update', 'Update your Gist files (default)'
21
20
  def update
22
- configs.each do |config|
23
- content = ContentFactory.build(user, access_token, config)
24
-
25
- if content.gist == content.local
26
- puts <<~EOS if options[:debug]
27
- There was no need to update `#{content.name}`.
28
- EOS
29
- else
30
- content.update
31
- end
32
- end
21
+ Updater.new(options).update
33
22
  end
34
23
 
35
24
  desc 'version', 'Display version'
36
25
  def version
37
26
  puts VERSION
38
27
  end
39
-
40
- private
41
-
42
- def configs
43
- @configs ||= YAML.load(IO.read(options[:yaml]))
44
- end
45
-
46
- def user
47
- @user ||= options[:user] ||
48
- ENV['GISTUPDATER_USER'] ||
49
- help_and_exit
50
- end
51
-
52
- def access_token
53
- @access_token ||= options[:token] ||
54
- ENV['GISTUPDATER_ACCESS_TOKEN'] ||
55
- help_and_exit
56
- end
57
-
58
- def help_and_exit
59
- help
60
- exit(1)
61
- end
62
28
  end
63
29
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module GistUpdater
6
+ class Config
7
+ def initialize(file)
8
+ @file = file
9
+ end
10
+
11
+ def each
12
+ return enum_for(:each) unless block_given?
13
+
14
+ config.each do |gist_id, file_path|
15
+ yield(gist_id, file_path)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :file
22
+
23
+ def config
24
+ @config ||= YAML.load(IO.read(file))
25
+ end
26
+ end
27
+ end
@@ -4,37 +4,53 @@ require 'octokit'
4
4
 
5
5
  module GistUpdater
6
6
  class Content
7
- attr_reader :name
7
+ def initialize(user:, access_token:, gist_id:, file_path:)
8
+ @user = user
9
+ @access_token = access_token
10
+ @gist_id = gist_id
11
+ @file_path = file_path
12
+ end
8
13
 
9
- def initialize(user:, access_token:, gist_id:, file_name:)
10
- @client = Octokit::Client.new(
11
- login: user,
12
- access_token: access_token
13
- )
14
- @gist_id = gist_id
15
- @name = file_name
16
- @basename = File.basename(file_name)
14
+ def update_if_need
15
+ if need_to_update?
16
+ result = update
17
+ puts "Updated `#{file_path}` to #{result.html_url}"
18
+ elsif GistUpdater.debug
19
+ puts "There was no need to update `#{file_path}`."
20
+ end
21
+
22
+ result
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :user, :access_token, :gist_id, :file_path
28
+
29
+ def client
30
+ @client ||= Octokit::Client.new(login: user, access_token: access_token)
31
+ end
32
+
33
+ def need_to_update?
34
+ @need_to_update ||= gist != local
17
35
  end
18
36
 
19
37
  def gist
20
- @gist ||= client.gist(gist_id).files[basename].content
38
+ @gist ||= client.gist(gist_id).files[file_name].content
21
39
  end
22
40
 
23
41
  def local
24
- @local ||= File.read(name)
42
+ @local ||= File.read(file_path)
25
43
  end
26
44
 
27
45
  def update
28
- result = client.edit_gist(
46
+ client.edit_gist(
29
47
  gist_id,
30
- files: { basename => { 'content' => local } }
48
+ files: { file_name => { 'content' => local } }
31
49
  )
32
- puts "Updated `#{name}` to #{result.html_url}"
33
- result
34
50
  end
35
51
 
36
- private
37
-
38
- attr_reader :client, :gist_id, :basename
52
+ def file_name
53
+ @file_name ||= File.basename(file_path)
54
+ end
39
55
  end
40
56
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GistUpdater
4
+ class << self
5
+ attr_accessor :debug
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GistUpdater
4
+ class Updater
5
+ def initialize(options, config_class = Config)
6
+ @user = options[:user] || ENV['GISTUPDATER_USER']
7
+ @access_token = options[:token] || ENV['GISTUPDATER_ACCESS_TOKEN']
8
+ @config = config_class.new(options[:yaml])
9
+ GistUpdater.debug = options[:debug]
10
+ end
11
+
12
+ def update
13
+ count = 0
14
+
15
+ config.each do |gist_id, file_path|
16
+ count += 1 if update_1(gist_id, file_path)
17
+ end
18
+
19
+ count
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :user, :access_token, :config
25
+
26
+ def update_1(gist_id, file_path)
27
+ Content.new(
28
+ user: user,
29
+ access_token: access_token,
30
+ gist_id: gist_id,
31
+ file_path: file_path
32
+ ).update_if_need
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GistUpdater
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/gist_updater.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'gist_updater/commands'
4
+ require 'gist_updater/config'
4
5
  require 'gist_updater/content'
5
- require 'gist_updater/content_factory'
6
+ require 'gist_updater/debug'
7
+ require 'gist_updater/updater'
6
8
  require 'gist_updater/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gist_updater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Masuda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-24 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit
@@ -121,8 +121,10 @@ files:
121
121
  - gist_updater.yml.example
122
122
  - lib/gist_updater.rb
123
123
  - lib/gist_updater/commands.rb
124
+ - lib/gist_updater/config.rb
124
125
  - lib/gist_updater/content.rb
125
- - lib/gist_updater/content_factory.rb
126
+ - lib/gist_updater/debug.rb
127
+ - lib/gist_updater/updater.rb
126
128
  - lib/gist_updater/version.rb
127
129
  homepage: https://github.com/masutaka/gist_updater
128
130
  licenses:
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module GistUpdater
4
- class ContentFactory
5
- def self.build(user, access_token, config, content_class = Content)
6
- content_class.new(
7
- user: user,
8
- access_token: access_token,
9
- gist_id: config[0],
10
- file_name: config[1]
11
- )
12
- end
13
- end
14
- end