gist_directory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1422f15dc239a5d7c48b8e3bdd209d0795c0295
4
+ data.tar.gz: 2e5680ae30af48b17e1a21df8af29954f4e13c51
5
+ SHA512:
6
+ metadata.gz: 56ed10a967e084f1e7de289608b48cc529816e9093a6586c7d36cb88845d7f6233b3de11bd67d1891119bfd51aeca45a05ad72073275b6108b5827244eed6f00
7
+ data.tar.gz: 187ecee0af3c0319f5f27cf81d45fd094a34c7fc990558f379d526444faa5b6f774c93ad097bd4be330c0b4853cc583c94508fcc0bda24d1b6d7026fa2b4de13
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Joe Yates
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # gist_directory
2
+
3
+ Create collections of gists in a local directory
4
+
5
+ # Usage
6
+
7
+ Create a gist called `food.md` in a directory called `food`
8
+ under your home directory:
9
+
10
+ ```shell
11
+ gist_directory ~/food
12
+ ```
13
+
14
+ Create a gist called `bacon.txt`:
15
+
16
+ ```shell
17
+ gist_directory ~/food bacon.txt
18
+ ```
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
4
+
5
+ require "gist_directory"
6
+
7
+ def usage
8
+ puts <<-EOT
9
+ #{$0} DIRECTORY [FILE]"
10
+
11
+ PATH:
12
+ Creates a Gist with a single file.
13
+
14
+ FILE:
15
+
16
+ By default, a Markdown file called basename([DIRECTORY]) + ".md" is created.
17
+ Supply an optional filename to use instead of this default.
18
+ EOT
19
+ end
20
+
21
+ if ARGV.size == 0
22
+ usage
23
+ exit 0
24
+ end
25
+
26
+ path = ARGV[0]
27
+ directory = File.basename(path)
28
+ file = ARGV[1] || directory + ".md"
29
+
30
+ GistDirectory.new(path: path).create(filename: file)
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift(File.expand_path("lib", __dir__))
3
+ require "gist_directory"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "gist_directory"
7
+ gem.summary =
8
+ gem.description = "Create collections of Gists"
9
+ gem.authors = ["Joe Yates"]
10
+ gem.email = ["joe.g.yates@gmail.com"]
11
+ gem.homepage = "https://github.com/joeyates/gist_directory"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^spec/})
16
+ gem.require_paths = ["lib"]
17
+ gem.version = GistDirectory::VERSION
18
+
19
+ gem.required_ruby_version = ">= 2.0.0"
20
+
21
+ gem.add_runtime_dependency "git"
22
+ gem.add_runtime_dependency "gist"
23
+
24
+ gem.add_development_dependency "rake"
25
+ gem.add_development_dependency "rspec", ">= 3.0.0"
26
+ gem.add_development_dependency "pry-byebug"
27
+ end
@@ -0,0 +1,86 @@
1
+ require "gist"
2
+ require "git"
3
+
4
+ class GistDirectory
5
+ MAJOR = 0
6
+ MINOR = 0
7
+ REVISION = 1
8
+ VERSION = [MAJOR, MINOR, REVISION].map(&:to_s).join('.')
9
+
10
+ attr_reader :path
11
+ attr_reader :git
12
+
13
+ def initialize(path: nil)
14
+ @path = File.expand_path(path)
15
+ end
16
+
17
+ def create(filename: nil)
18
+ raise "No filename supplied" if filename.nil?
19
+
20
+ if path_exists?
21
+ fail_unless_gist_directory
22
+ fail_if_filename_exists filename
23
+ add_file_to_directory filename: filename
24
+ else
25
+ do_gist_create filename: filename
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def add_file_to_directory(filename: nil)
32
+ do_file_add filename: filename
33
+ end
34
+
35
+ def do_gist_create(filename: nil)
36
+ result = Gist.gist(content_for_empty_file, filename: filename, public: true)
37
+ gist_hash = result["id"]
38
+
39
+ Git.clone("git@gist.github.com:#{gist_hash}", path)
40
+ end
41
+
42
+ def do_file_add(filename: nil)
43
+ absolute = filepath(filename: filename)
44
+ File.open(absolute, "w") { |f| f.puts content_for_empty_file }
45
+ git.add(absolute)
46
+ git.commit("Added '#{filename}'")
47
+ git.push
48
+ end
49
+
50
+ def fail_unless_gist_directory
51
+ if ! is_gist_directory?
52
+ raise "#{path} exists and does not contain a Gist repository"
53
+ end
54
+ end
55
+
56
+ def fail_if_filename_exists(filename)
57
+ absolute = filepath(filename: filename)
58
+ if File.exist?(absolute)
59
+ raise "A file called #{filename} already exists in #{path}"
60
+ end
61
+ end
62
+
63
+ def git
64
+ @git ||= Git.open(path)
65
+ end
66
+
67
+ def filepath(filename: nil)
68
+ File.join(path, filename)
69
+ end
70
+
71
+ def path_exists?
72
+ File.directory?(path)
73
+ end
74
+
75
+ def is_gist_directory?
76
+ return false unless File.directory?(File.join(path, ".git"))
77
+ origin = git.remotes.select { |r| r.name == "origin" }[0]
78
+ return false if origin.nil?
79
+ m = %r(^git@gist\.github\.com:\w+$).match(origin.url)
80
+ ! m.nil?
81
+ end
82
+
83
+ def content_for_empty_file
84
+ "Empty Gist"
85
+ end
86
+ end
data/rakefile.rb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ task :default => :spec
7
+
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = "spec/**/*_spec.rb"
10
+ end
@@ -0,0 +1,106 @@
1
+ require "gist_directory"
2
+
3
+ RSpec.describe GistDirectory do
4
+ let(:path) { "/foo/bar" }
5
+ let(:path_exists) { false }
6
+ let(:filename) { "baz.txt" }
7
+ let(:gist_result) { {"id" => gist_hash} }
8
+ let(:gist_hash) { "gist_sha1_hash" }
9
+ let(:git) { double(Git) }
10
+ subject { described_class.new(path: path) }
11
+
12
+ before do
13
+ allow(File).to receive(:directory?).and_call_original
14
+ allow(File).to receive(:directory?).with(path) { path_exists }
15
+ allow(Gist).to receive(:gist) { gist_result }
16
+ allow(Git).to receive(:clone)
17
+ allow(Git).to receive(:open) { git }
18
+ end
19
+
20
+ describe "#create" do
21
+ context "when the path doesn't exist" do
22
+ it "creates a Gist, adding the file" do
23
+ subject.create(filename: filename)
24
+ expect(Gist).to have_received(:gist).with(
25
+ "Empty Gist", {filename: filename, public: true}
26
+ )
27
+ end
28
+ end
29
+
30
+ context "when the path exists" do
31
+ let(:path_exists) { true }
32
+ let(:dot_git_path) { File.join(path, ".git") }
33
+
34
+ before do
35
+ allow(File).to receive(:directory?).with(dot_git_path) { dot_git_exists }
36
+ end
37
+
38
+ context "and contains a Git repo" do
39
+ let(:dot_git_exists) { true }
40
+ let(:remotes) { [double(Git::Remote, name: "origin", url: repo_url)] }
41
+ let(:filepath) { File.join(path, filename) }
42
+ let(:file) { double(File, puts: nil) }
43
+
44
+ before do
45
+ allow(File).to receive(:open).and_call_original
46
+ allow(File).to receive(:open).with(filepath, "w").and_yield(file)
47
+ allow(File).to receive(:exist?).and_call_original
48
+ allow(File).to receive(:exist?).with(filepath) { file_exists }
49
+ allow(git).to receive(:remotes) { remotes }
50
+ allow(git).to receive(:add)
51
+ allow(git).to receive(:commit)
52
+ allow(git).to receive(:push)
53
+ end
54
+
55
+ context "which is a Gist repo" do
56
+ let(:repo_url) { "git@gist.github.com:1234" }
57
+
58
+ context "when the file doesn't exist" do
59
+ let(:file_exists) { false }
60
+
61
+ it "creates the file" do
62
+ subject.create(filename: filename)
63
+ expect(file).to have_received(:puts)
64
+ end
65
+
66
+ it "adds the file" do
67
+ subject.create(filename: filename)
68
+ expect(git).to have_received(:add).with(filepath)
69
+ expect(git).to have_received(:commit)
70
+ end
71
+ end
72
+
73
+ context "when the file exists" do
74
+ let(:file_exists) { true }
75
+
76
+ it "fails" do
77
+ expect do
78
+ subject.create(filename: filename)
79
+ end.to raise_error(RuntimeError, /#{filename} already exists/)
80
+ end
81
+ end
82
+ end
83
+
84
+ context "which is not a Gist repo" do
85
+ let(:repo_url) { "git@example.com:1234" }
86
+
87
+ it "fails" do
88
+ expect do
89
+ subject.create(filename: filename)
90
+ end.to raise_error(RuntimeError, /not contain a Gist repository/)
91
+ end
92
+ end
93
+ end
94
+
95
+ context "but doesn't contain a Git repo" do
96
+ let(:dot_git_exists) { false }
97
+
98
+ it "fails" do
99
+ expect do
100
+ subject.create(filename: filename)
101
+ end.to raise_error(RuntimeError, /not contain a Gist repository/)
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,26 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+
10
+ # config.filter_run :focus
11
+ config.run_all_when_everything_filtered = true
12
+
13
+ config.disable_monkey_patching!
14
+
15
+ config.warnings = true
16
+
17
+ if config.files_to_run.one?
18
+ config.default_formatter = "doc"
19
+ end
20
+
21
+ config.profile_examples = 10
22
+
23
+ config.order = :random
24
+
25
+ Kernel.srand config.seed
26
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gist_directory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joe Yates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Create collections of Gists
84
+ email:
85
+ - joe.g.yates@gmail.com
86
+ executables:
87
+ - gist_directory
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - bin/gist_directory
97
+ - gist-directory.gemspec
98
+ - lib/gist_directory.rb
99
+ - rakefile.rb
100
+ - spec/gist_directory_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/joeyates/gist_directory
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.0.0
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.4.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Create collections of Gists
125
+ test_files:
126
+ - spec/gist_directory_spec.rb
127
+ - spec/spec_helper.rb