gisture 0.0.5 → 0.0.6

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: 204135cbcc13305754d2b3a0558222ec220eae6b
4
- data.tar.gz: 068ea799d72c1678747b1cef3a7c93a48a2ebd5d
3
+ metadata.gz: 03e7d1d1abd295d696f903a9017625ef7f4523f0
4
+ data.tar.gz: 4e7d8c8ddb6570ff3b8c1f366f791701e0640bdc
5
5
  SHA512:
6
- metadata.gz: b53b53175b706be880fae8e9e32609edae8909bcc6dc3d3076a698c1f6e05b3463402ce7958fcc578f5bd48e9830a386d666546f60643f8424a52788c83263b0
7
- data.tar.gz: 2e8a7e5565c63d09cb28ec6df0e229c399f0836b33a5a7c14f2286509e57adde000845d35947ed6183fec3e80b6476d07147ccf99f5ea51e5d9609d3b2eed435
6
+ metadata.gz: f1e213b93a08f3c3a552e28e8fb9900a7f504c5bdd1de7265901a8480dd8d869a85f2f6132bcf42cdd1ec88a85013c69d50da5f0fa91bc311a7f30e579990dc5
7
+ data.tar.gz: 8f9244489caba893887c4ecac9352b881e75f1b241c9a7c858cf3d41e8ad0a439dbf926d2dc3eaf7d8352e111b1148c7ae22549e1c2bf443432d796524cba5ea
@@ -0,0 +1,55 @@
1
+ require 'tempfile'
2
+
3
+ module Gisture
4
+ class File
5
+ attr_reader :file, :basename
6
+
7
+ def require!(&block)
8
+ required = require tempfile.path
9
+ unlink_tempfile
10
+ block_given? ? yield : required
11
+ end
12
+
13
+ def load!(&block)
14
+ loaded = load tempfile.path
15
+ unlink_tempfile
16
+ block_given? ? yield : loaded
17
+ end
18
+
19
+ def eval!(&block)
20
+ clean_room = Evaluator.new(content)
21
+ clean_room.instance_eval &block if block_given?
22
+ clean_room
23
+ end
24
+
25
+ def tempfile
26
+ @tempfile ||= begin
27
+ file = Tempfile.new([basename, filename, ::File.extname(filename)].compact, Gisture.configuration.tmpdir)
28
+ file.write(content)
29
+ file.close
30
+ file
31
+ end
32
+ end
33
+
34
+ def unlink_tempfile
35
+ tempfile.unlink
36
+ @tempfile = nil
37
+ end
38
+
39
+ def method_missing(meth, *args, &block)
40
+ return file.send(meth, *args, &block) if file.respond_to?(meth)
41
+ super
42
+ end
43
+
44
+ def respond_to_missing?(meth, include_private=false)
45
+ file.respond_to?(meth, include_private)
46
+ end
47
+
48
+ protected
49
+
50
+ def initialize(file, basename=nil)
51
+ @file = file
52
+ @basename = basename
53
+ end
54
+ end
55
+ end
data/lib/gisture/gist.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'tempfile'
2
-
3
1
  module Gisture
4
2
  class Gist
5
3
  attr_reader :filename, :gist_id, :strategy, :version
@@ -17,21 +15,15 @@ module Gisture
17
15
  end
18
16
 
19
17
  def require!(&block)
20
- required = require tempfile.path
21
- unlink_tempfile
22
- block_given? ? yield : required
18
+ file.require! &block
23
19
  end
24
20
 
25
21
  def load!(&block)
26
- loaded = load tempfile.path
27
- unlink_tempfile
28
- block_given? ? yield : loaded
22
+ file.load! &block
29
23
  end
30
24
 
31
25
  def eval!(&block)
32
- clean_room = Evaluator.new(raw)
33
- clean_room.instance_eval &block if block_given?
34
- clean_room
26
+ file.eval! &block
35
27
  end
36
28
 
37
29
  def github
@@ -51,24 +43,18 @@ module Gisture
51
43
  end
52
44
  end
53
45
 
54
- def gist_file
55
- return @gist_file unless @gist_file.nil?
46
+ def file
47
+ return @file unless @file.nil?
56
48
 
57
49
  if gist.files.count > 1
58
50
  raise ArgumentError, "You must specify a filename if your gist contains more than one file" if filename.nil?
59
- gist.files.each do |file|
60
- @gist_file = file if file[0] == filename
61
- end
62
- raise ArgumentError, "The filename '#{filename}' was not found in the list of files for the gist '#{gist_id}'" if @gist_file.nil?
51
+ @file = Gisture::File.new(gist.files[filename], gist_id)
52
+ raise ArgumentError, "The filename '#{filename}' was not found in the list of files for the gist '#{gist_id}'" if @file.nil?
63
53
  else
64
- @gist_file = gist.files.first
54
+ @file = Gisture::File.new(gist.files.first[1], gist_id)
65
55
  end
66
56
 
67
- @gist_file
68
- end
69
-
70
- def raw
71
- gist_file[1].content
57
+ @file
72
58
  end
73
59
 
74
60
  def strategy=(strat)
@@ -76,15 +62,6 @@ module Gisture
76
62
  @strategy = strat.to_sym
77
63
  end
78
64
 
79
- def tempfile
80
- @tempfile ||= begin
81
- file = Tempfile.new([gist_id, File.extname(gist_file[0])], Gisture.configuration.tmpdir)
82
- file.write(raw)
83
- file.close
84
- file
85
- end
86
- end
87
-
88
65
  def to_h
89
66
  { gist_id: gist_id,
90
67
  version: version,
@@ -95,7 +72,7 @@ module Gisture
95
72
  protected
96
73
 
97
74
  def initialize(gist, strategy: nil, filename: nil, version: nil)
98
- self.strategy = strategy || :eval
75
+ self.strategy = strategy || Gisture.configuration.strategy
99
76
  @filename = filename
100
77
 
101
78
  if gist.match(/[^a-f0-9]+/i) # non-hex chars, probably a URL
@@ -108,11 +85,6 @@ module Gisture
108
85
 
109
86
  end
110
87
 
111
- def unlink_tempfile
112
- tempfile.unlink
113
- @tempfile = nil
114
- end
115
-
116
88
  def parse_gist_url(gist_url)
117
89
  case gist_url.to_s
118
90
  when GIST_URL_WITH_VERSION_REGEX
@@ -1,7 +1,7 @@
1
1
  module Gisture
2
2
  class Railtie < Rails::Railtie
3
3
  rake_tasks do
4
- Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
4
+ Dir[::File.join(::File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module Gisture
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
data/lib/gisture.rb CHANGED
@@ -3,6 +3,7 @@ require 'github_api'
3
3
  require 'gisture/github_api/client/gists'
4
4
  require 'gisture/version'
5
5
  require 'gisture/evaluator'
6
+ require 'gisture/file'
6
7
  require 'gisture/gist'
7
8
  require 'gisture/railtie' if defined?(Rails)
8
9
 
@@ -20,6 +21,7 @@ module Gisture
20
21
  config.user = nil # global user used in requets if none provided
21
22
  config.org = nil # global organization used in request if none provided
22
23
 
24
+ config.strategy = :eval # default execution strategy
23
25
  config.tmpdir = Dir.tmpdir # location to store gist tempfiles
24
26
  end
25
27
 
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Gisture::Gist do
4
+ subject { Gisture::File.new(Gisture::Gist.new(TEST_GIST_ID).gist.files.first[1], TEST_GIST_ID) }
5
+
6
+ it "delegates missing methods to the file hash" do
7
+ expect(subject.respond_to?(:content)).to be_true
8
+ expect(subject.respond_to?(:filename)).to be_true
9
+ end
10
+
11
+ describe "#tempfile" do
12
+ it "creates and returns a tempfile" do
13
+ expect(subject.tempfile).to be_a(Tempfile)
14
+ end
15
+
16
+ it "uses the gist_id as the base of the filename" do
17
+ matched = ::File.basename(subject.tempfile.path).match(/#{TEST_GIST_ID}/)
18
+ expect(matched).to_not be_nil
19
+ end
20
+
21
+ it "uses the same extension as the gist's filename" do
22
+ expect(::File.extname(subject.tempfile.path)).to eql(::File.extname(subject.file.filename))
23
+ end
24
+
25
+ it "creates the file in the configured tempdir" do
26
+ tmp_path = ::File.join(::File.dirname(__FILE__), '../', 'tmp')
27
+ begin
28
+ FileUtils.mkdir_p tmp_path
29
+ Gisture.configure do |config|
30
+ config.tmpdir = tmp_path
31
+ end
32
+
33
+ expect(::File.dirname(subject.tempfile.path)).to eql(tmp_path)
34
+ rescue => e
35
+ raise e
36
+ ensure
37
+ Gisture.configure do |config|
38
+ config.tmpdir = Dir.tmpdir
39
+ end
40
+ FileUtils.rm_rf tmp_path
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,13 +1,5 @@
1
1
  require "spec_helper"
2
2
 
3
- TEST_GIST_ID = "520b474ea0248d1a0a74"
4
- TEST_GIST_URL = "https://gist.github.com/markrebec/520b474ea0248d1a0a74"
5
- TEST_GIST_VERSION = "49a9d887eeb8c723ab23deddfbbb75d4b70e8014"
6
- TEST_GIST_VERSION_URL = "https://gist.github.com/markrebec/520b474ea0248d1a0a74/49a9d887eeb8c723ab23deddfbbb75d4b70e8014"
7
- TEST_GIST_FILENAME = "test.rb"
8
- MULTI_FILE_TEST_GIST_ID = "0417bf78a7c2b825b4ef"
9
- MULTI_FILE_TEST_GIST_FILENAMES = ['file_one.rb', 'file_two.rb']
10
-
11
3
  RSpec.describe Gisture::Gist do
12
4
  context "when passing a gist ID" do
13
5
  it "sets the gist ID as the gist_id" do
@@ -87,20 +79,14 @@ RSpec.describe Gisture::Gist do
87
79
  end
88
80
  end
89
81
 
90
- describe "#gist_file" do
91
- it "returns an array" do
92
- expect(Gisture::Gist.new(TEST_GIST_ID).gist_file).to be_a(Array)
93
- end
94
-
95
- it "contains the filename and file object" do
96
- gist = Gisture::Gist.new(TEST_GIST_ID)
97
- expect(gist.gist_file[0]).to eql(TEST_GIST_FILENAME)
98
- expect(gist.gist_file[1]).to be_a(Hashie::Mash)
82
+ describe "#file" do
83
+ it "returns a Gisture::File" do
84
+ expect(Gisture::Gist.new(TEST_GIST_ID).file).to be_a(Gisture::File)
99
85
  end
100
86
 
101
87
  context "when a gist contains a single file" do
102
88
  it "returns the file" do
103
- expect(Gisture::Gist.new(TEST_GIST_ID).gist_file[0]).to eql(TEST_GIST_FILENAME)
89
+ expect(Gisture::Gist.new(TEST_GIST_ID).file.filename).to eql(TEST_GIST_FILENAME)
104
90
  end
105
91
  end
106
92
 
@@ -109,7 +95,7 @@ RSpec.describe Gisture::Gist do
109
95
  subject { Gisture::Gist.new(MULTI_FILE_TEST_GIST_ID, filename: MULTI_FILE_TEST_GIST_FILENAMES.sample) }
110
96
 
111
97
  it "returns the specified file" do
112
- expect(subject.gist_file[0]).to eql(subject.filename)
98
+ expect(subject.file.filename).to eql(subject.filename)
113
99
  end
114
100
  end
115
101
 
@@ -117,20 +103,12 @@ RSpec.describe Gisture::Gist do
117
103
  subject { Gisture::Gist.new(MULTI_FILE_TEST_GIST_ID) }
118
104
 
119
105
  it "raises an ArgumentError" do
120
- expect { subject.gist_file }.to raise_exception(ArgumentError)
106
+ expect { subject.file }.to raise_exception(ArgumentError)
121
107
  end
122
108
  end
123
109
  end
124
110
  end
125
111
 
126
- describe "#raw" do
127
- subject { Gisture::Gist.new(TEST_GIST_ID) }
128
-
129
- it "returns the raw gist content" do
130
- expect(subject.raw).to eql(subject.gist_file[1].content)
131
- end
132
- end
133
-
134
112
  describe "#strategy=" do
135
113
  subject { Gisture::Gist.new(TEST_GIST_ID) }
136
114
 
@@ -148,42 +126,6 @@ RSpec.describe Gisture::Gist do
148
126
  end
149
127
  end
150
128
 
151
- describe "#tempfile" do
152
- subject { Gisture::Gist.new(TEST_GIST_ID) }
153
-
154
- it "creates and returns a tempfile" do
155
- expect(subject.tempfile).to be_a(Tempfile)
156
- end
157
-
158
- it "uses the gist_id as the base of the filename" do
159
- matched = File.basename(subject.tempfile.path).match(/#{TEST_GIST_ID}/)
160
- expect(matched).to_not be_nil
161
- end
162
-
163
- it "uses the same extension as the gist's filename" do
164
- expect(File.extname(subject.tempfile.path)).to eql(File.extname(subject.gist_file[0]))
165
- end
166
-
167
- it "creates the file in the configured tempdir" do
168
- tmp_path = File.join(File.dirname(__FILE__), '../', 'tmp')
169
- begin
170
- FileUtils.mkdir_p tmp_path
171
- Gisture.configure do |config|
172
- config.tmpdir = tmp_path
173
- end
174
-
175
- expect(File.dirname(subject.tempfile.path)).to eql(tmp_path)
176
- rescue => e
177
- raise e
178
- ensure
179
- Gisture.configure do |config|
180
- config.tmpdir = Dir.tmpdir
181
- end
182
- FileUtils.rm_rf tmp_path
183
- end
184
- end
185
- end
186
-
187
129
  describe "#to_h" do
188
130
  subject { Gisture::Gist.new(TEST_GIST_ID, filename: "test.rb") }
189
131
 
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,14 @@ Gisture.configure do |config|
9
9
  config.oauth_token = ENV['GITHUB_OAUTH_TOKEN']
10
10
  end
11
11
 
12
+ TEST_GIST_ID = "520b474ea0248d1a0a74"
13
+ TEST_GIST_URL = "https://gist.github.com/markrebec/520b474ea0248d1a0a74"
14
+ TEST_GIST_VERSION = "49a9d887eeb8c723ab23deddfbbb75d4b70e8014"
15
+ TEST_GIST_VERSION_URL = "https://gist.github.com/markrebec/520b474ea0248d1a0a74/49a9d887eeb8c723ab23deddfbbb75d4b70e8014"
16
+ TEST_GIST_FILENAME = "test.rb"
17
+ MULTI_FILE_TEST_GIST_ID = "0417bf78a7c2b825b4ef"
18
+ MULTI_FILE_TEST_GIST_FILENAMES = ['file_one.rb', 'file_two.rb']
19
+
12
20
  RSpec.configure do |config|
13
21
  #config.before(:suite) do
14
22
  # ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gisture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - lib/gisture.rb
77
77
  - lib/gisture/evaluator.rb
78
+ - lib/gisture/file.rb
78
79
  - lib/gisture/gist.rb
79
80
  - lib/gisture/github_api/client/gists.rb
80
81
  - lib/gisture/railtie.rb
@@ -86,6 +87,7 @@ files:
86
87
  - spec/gists/method.rb
87
88
  - spec/gists/simple.rb
88
89
  - spec/gisture/evaluator_spec.rb
90
+ - spec/gisture/file_spec.rb
89
91
  - spec/gisture/gist_spec.rb
90
92
  - spec/gisture_spec.rb
91
93
  - spec/spec_helper.rb
@@ -121,6 +123,7 @@ test_files:
121
123
  - spec/gists/method.rb
122
124
  - spec/gists/simple.rb
123
125
  - spec/gisture/evaluator_spec.rb
126
+ - spec/gisture/file_spec.rb
124
127
  - spec/gisture/gist_spec.rb
125
128
  - spec/gisture_spec.rb
126
129
  - spec/spec_helper.rb