gast 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec83adad16b7da1940d4ad7dcb4de850b804b7b1
4
- data.tar.gz: 854a253b2bdc656cb92d63e8256cdee8246f09fd
3
+ metadata.gz: 2c76e56576ccffbd53ce37ad4435ff10fb77dbfe
4
+ data.tar.gz: 480980571d4a12a68d4039b1572a3297073d627f
5
5
  SHA512:
6
- metadata.gz: 4443e060971ee973f8b4484fc55c91126e7914ee170b78a1aeddd027b91fa8a608bfb8ad8d9f3ad42a80b2927508edec82f1ee357c679cc8c14b3df520757826
7
- data.tar.gz: 26f036921b25862629e282781319a34bf65420868ca66de0c0a30e4ae6cb0f64914a964ee372f4e5df9c0595631ee28a169c2f96bf89a7b403d0de8d5b1129fd
6
+ metadata.gz: e6743a9590249e60d166f427eaeeb0f2b300cb8368ad17630d46a5d5117e1275dcc4cde464f9e98c6a5012f9e1c24c5040bcbbb59dbc70e3e528a60f64a799ea
7
+ data.tar.gz: 492009ee035e492695fb47ba31fa8ad451026029fcafd97898feeab88c38917071dda5a685d5488d46d99a14072272d5f629d5e17197faaa650aea5730f34f07
data/Guardfile CHANGED
@@ -13,9 +13,10 @@ end
13
13
  guard :rspec, cmd: 'bundle ex rspec' do
14
14
  watch(%r{^spec/.+_spec\.rb$})
15
15
  watch('spec/spec_helper.rb') { "spec" }
16
- watch(%r{spec/feature/.+.rb$})
16
+ watch(%r{spec/feature/.+_spec\.rb$})
17
17
 
18
18
  watch(%r{^lib/gast/app.rb$}) { |m| "spec/controller_spec.rb" }
19
+ watch(%r{^lib/gast/helper.rb$}) { "spec" }
19
20
  watch(%r{^lib/gast/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
20
21
  end
21
22
 
data/lib/gast/base.rb CHANGED
@@ -62,8 +62,8 @@ module Gast
62
62
  })
63
63
  end
64
64
 
65
- result = Gast::Memo.save(@content, @language)
66
- @content_id = result[:content_id]
65
+ result = Gast::Memo.create(@content, @language)
66
+ @content_id = result.content_id
67
67
  end
68
68
 
69
69
  before '/list' do
@@ -88,7 +88,9 @@ module Gast
88
88
  end
89
89
 
90
90
  before '/update/:content_id' do
91
- Gast::Memo.update(@content_id, @content, @language)
91
+ if Gast::Memo.changed?(@content_id, @content)
92
+ Gast::Memo.update(@content_id, @content, @language)
93
+ end
92
94
  end
93
95
  end
94
96
  end
@@ -0,0 +1,21 @@
1
+ require 'gast'
2
+
3
+ module Gast
4
+ module Helper
5
+ def escape_html
6
+ CGI.escapeHTML(yield.to_s) if block_given?
7
+ end
8
+
9
+ def unescape_html
10
+ CGI.unescapeHTML(yield.to_s) if block_given?
11
+ end
12
+
13
+ def get_content(id)
14
+ File.read(File.expand_path(Gast::PATH + "/#{id}/content"))
15
+ end
16
+
17
+ def get_language(id)
18
+ File.read(File.expand_path(Gast::PATH + "/#{id}/language"))
19
+ end
20
+ end
21
+ end
data/lib/gast/memo.rb CHANGED
@@ -1,57 +1,90 @@
1
1
  require 'gast'
2
2
 
3
3
  module Gast
4
- class Memo
5
- def self.save(content, language = 'no-highlight')
6
- @repo = Gast::Repository.new
7
- @repo.create
8
- @repo.content = CGI.unescapeHTML(content.to_s)
9
- @repo.language = CGI.unescapeHTML(language.to_s)
10
- @repo.publish
11
- @repo.commit!
12
- {
13
- content_id: @repo.dir_name,
14
- language: language
15
- }
16
- end
17
-
18
- def self.update(content_id, content, language = 'no-highlight')
19
- return content_id if content.to_s.chomp == item(content_id).chomp
20
- @repo = Gast::Repository.new
21
- @repo.dir_name = content_id
22
- @repo.create
23
- @repo.content = CGI.unescapeHTML(content.to_s)
24
- @repo.language = CGI.unescapeHTML(language.to_s)
25
- @repo.publish
26
- @repo.commit!
27
- {
28
- content_id: @repo.dir_name,
29
- language: language
30
- }
31
- end
32
-
33
- def self.number
4
+ module Memo
5
+ extend Helper
6
+
7
+ class Response
8
+ attr_reader :content_id, :language, :content
9
+
10
+ def initialize(content_id, language, content)
11
+ @content_id = content_id
12
+ @language = language
13
+ @content = content
14
+ end
15
+ end
16
+
17
+ class Request
18
+ attr_reader :content_id, :language, :content
19
+
20
+ def initialize(content_id, language, content)
21
+ @content_id = content_id
22
+ @language = language
23
+ @content = content
24
+ end
25
+ end
26
+
27
+ def response(content_id, language, content)
28
+ Response.new(content_id, language, content)
29
+ end
30
+ module_function :response
31
+
32
+ def request(content_id, language, content)
33
+ Request.new(content_id, language, content)
34
+ end
35
+ module_function :request
36
+
37
+ def repository(content, language, content_id = nil)
38
+ repo = Repository.new
39
+ repo.dir_name = content_id
40
+ repo.create
41
+ repo.content = unescape_html { content }
42
+ repo.language = unescape_html { language }
43
+ repo.write
44
+ repo.commit!
45
+
46
+ repo.dir_name
47
+ end
48
+
49
+ def create(content, language = DEFAULT_HIGHLIGHT)
50
+ content_id = repository(content, language)
51
+ response(content_id, language, content)
52
+ end
53
+ module_function :create
54
+
55
+ def update(content_id, content, language = DEFAULT_HIGHLIGHT)
56
+ content_id = repository(content, language, content_id)
57
+ response(content_id, language, content)
58
+ end
59
+ module_function :update
60
+
61
+ def changed?(content_id, content)
62
+ content.to_s.chomp != item(content_id).chomp
63
+ end
64
+ module_function :changed?
65
+
66
+ def number
34
67
  lists.length
35
68
  end
69
+ module_function :number
36
70
 
37
- def self.lists
71
+ def lists
38
72
  Dir.glob(File.expand_path(Gast::PATH + '/**')).map do |dir|
39
73
  dir.split('/').last
40
74
  end
41
75
  end
76
+ module_function :lists
42
77
 
43
- def self.item(id)
44
- CGI.escapeHTML(
45
- File.read(File.expand_path(Gast::PATH + "/#{id}/content"))
46
- )
78
+ def item(id)
79
+ escape_html { get_content(id) }
47
80
  end
81
+ module_function :item
48
82
 
49
- def self.language(id)
50
- CGI.escapeHTML(
51
- File.read(File.expand_path(Gast::PATH + "/#{id}/language"))
52
- )
83
+ def language(id)
84
+ escape_html { get_language(id) }
53
85
  end
86
+ module_function :language
54
87
 
55
- def initialize; end
88
+ extend Memo
56
89
  end
57
90
  end
@@ -9,15 +9,15 @@ module Gast
9
9
  def initialize; end
10
10
 
11
11
  def create
12
- @dir_name = new_name_of_repository if @dir_name.nil?
13
- @path = path_of_repository
12
+ new_name_of_repository
13
+ path_of_repository
14
14
  create_dir
15
- @git = Git.init(@path)
15
+ setup_repository
16
16
  end
17
17
 
18
- def publish
19
- save_content
20
- save_language
18
+ def write
19
+ write_content
20
+ write_language
21
21
  end
22
22
 
23
23
  def remove!
@@ -25,13 +25,11 @@ module Gast
25
25
  end
26
26
 
27
27
  def save!
28
- @git.add(all: true)
29
- @git.commit_all("commit: #{DateTime.now}")
28
+ commit_all
30
29
  end
31
30
 
32
31
  def commit!
33
- return save! if @git.ls_files.length == 0
34
- return save! if @git.status.changed.length != 0
32
+ return save! unless changed_of_contents?
35
33
  end
36
34
 
37
35
  private
@@ -42,7 +40,7 @@ module Gast
42
40
  FileUtils.chmod(0755, @path)
43
41
  end
44
42
 
45
- def save_content
43
+ def write_content
46
44
  path = File.expand_path(@path + '/content')
47
45
  open(path, 'w', 0644) do |io|
48
46
  io.flock(File::LOCK_EX)
@@ -51,7 +49,7 @@ module Gast
51
49
  end
52
50
  end
53
51
 
54
- def save_language
52
+ def write_language
55
53
  path = File.expand_path(@path + '/language')
56
54
  open(path, 'w', 0644) do |io|
57
55
  io.flock(File::LOCK_EX)
@@ -61,11 +59,28 @@ module Gast
61
59
  end
62
60
 
63
61
  def new_name_of_repository
64
- Digest::SHA512.new.update(rand.to_s).to_s[0..30]
62
+ if @dir_name.nil?
63
+ @dir_name = Digest::SHA512.new.update(rand.to_s).to_s[0..30]
64
+ end
65
65
  end
66
66
 
67
67
  def path_of_repository
68
- File.expand_path(File.join(Gast::PATH, @dir_name))
68
+ @path = File.expand_path(File.join(Gast::PATH, @dir_name))
69
+ end
70
+
71
+ def setup_repository
72
+ @git = Git.init(@path)
73
+ end
74
+
75
+ def changed_of_contents?
76
+ return false if @git.ls_files.length == 0
77
+ return false if @git.status.changed.length != 0
78
+ return true
79
+ end
80
+
81
+ def commit_all
82
+ @git.add(all: true)
83
+ @git.commit_all("commit: #{DateTime.now}")
69
84
  end
70
85
  end
71
86
  end
@@ -1,5 +1,5 @@
1
1
  %h1
2
2
  = @content_id
3
- %form{ role: "form", action: "/posts/update/#{params[:id].to_s}", method: "POST" }
3
+ %form{ role: "form", action: "/posts/update/#{@content_id}", method: "POST" }
4
4
  %input{ type: "hidden", name: "_method", value: "put" }
5
5
  = haml :form_area
@@ -1,6 +1,6 @@
1
1
  %h1
2
2
  = @content_id
3
- %form{ role: "form", action: "/posts/edit/#{params[:id].to_s}", method: "GET" }
3
+ %form{ role: "form", action: "/posts/edit/#{@content_id}", method: "GET" }
4
4
  %button{ type: "submit", class: "btn btn-primary" }
5
5
  Update
6
6
 
data/lib/gast/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gast
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/gast.rb CHANGED
@@ -5,6 +5,7 @@ require 'sinatra/contrib'
5
5
  require 'sinatra/namespace'
6
6
  require 'sprockets'
7
7
  require 'sprockets-helpers'
8
+ require 'gast/helper'
8
9
  require 'gast/memo'
9
10
  require 'gast/app'
10
11
  require 'gast/repository'
@@ -25,4 +26,6 @@ module Gast
25
26
  LANGUAGES = YAML.load_file(
26
27
  File.expand_path('../config/languages.yml', __dir__)
27
28
  )
29
+
30
+ DEFAULT_HIGHLIGHT = 'no-highlight'
28
31
  end
@@ -1,22 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
- def app
4
- Gast::App
5
- end
6
-
7
3
  describe Gast::App do
8
4
 
9
- after(:each) do
10
- FileUtils.rm_r(Dir.glob(Gast::PATH + '/**'), secure: true)
5
+ def app
6
+ Gast::App
11
7
  end
12
8
 
13
9
  let(:hello_world) { 'Hello World' }
14
10
  let(:welcome_to_underground) { 'Welcome to underground' }
15
11
  let(:inline_html) { "<script>alert('Hello world!');</script>" }
16
12
  let(:sample_of_code_ruby) { get_fixture('sample_of_code.rb') }
17
-
18
13
  let(:view_path) { %r{.+/posts/view/[a-z0-9]+} }
19
14
 
15
+ after(:each) { cleanup_in_the_dir }
16
+
20
17
  it 'should be get index' do
21
18
  get '/'
22
19
  expect(last_response).to be_ok
@@ -121,5 +118,4 @@ describe Gast::App do
121
118
  expect(latest_content).to eq sample_of_code_ruby
122
119
  expect(latest_language_of_content).to eq 'python'
123
120
  end
124
-
125
121
  end
@@ -1,10 +1,27 @@
1
- describe "the index page", type: :feature do
2
- it "get index in" do
1
+ require 'spec_helper'
2
+
3
+ describe 'the index page', type: :feature do
4
+ it 'get index in' do
3
5
  visit '/'
4
6
 
5
- get_languages.each_with_index do |language, idx|
6
- find(:xpath, "//select/option[#{idx+1}]").text eq language
7
+ Gast::LANGUAGES.each_with_index do |language, idx|
8
+ find("//select/option[#{idx+1}]").text eq language
7
9
  end
8
10
  end
9
- :qa
11
+
12
+ it 'should be able to selected of multi language' do
13
+ visit '/'
14
+
15
+ find('//select').select('python')
16
+ find('//select').text eq 'python'
17
+ find('//select').select('ruby')
18
+ find('//select').text eq 'ruby'
19
+ end
20
+
21
+ it 'should be able to write text on the textarea' do
22
+ visit '/'
23
+
24
+ find('//textarea').set 'Hello world'
25
+ find('//textarea').text eq 'Hello world'
26
+ end
10
27
  end
data/spec/memo_spec.rb CHANGED
@@ -3,15 +3,12 @@ require 'spec_helper'
3
3
  describe Gast::Memo do
4
4
  let(:welcome_to_underground) { 'Welcome to underground' }
5
5
 
6
- after(:each) do
7
- FileUtils.rm_r(Dir.glob(Gast::PATH + '/**'), secure: true)
8
- end
6
+ after(:each) { cleanup_in_the_dir }
9
7
 
10
8
  it 'should be get list of directory' do
11
9
  10.times do |i|
12
- Gast::Memo.save(welcome_to_underground)
10
+ Gast::Memo.create(welcome_to_underground)
13
11
  end
14
12
  expect(Gast::Memo.number).to eq 10
15
13
  end
16
-
17
14
  end
@@ -1,11 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gast::Repository do
4
-
5
4
  let(:repo) { Gast::Repository.new }
6
-
7
5
  let(:git) { Git.init(repo.path) }
8
-
9
6
  let(:user_content) { 'Hello World' }
10
7
  let(:selected_language) { 'ruby' }
11
8
 
@@ -13,13 +10,11 @@ describe Gast::Repository do
13
10
  repo.content = user_content
14
11
  repo.language = selected_language
15
12
  repo.create
16
- repo.publish
13
+ repo.write
17
14
  repo.commit!
18
15
  end
19
16
 
20
- after(:each) do
21
- FileUtils.rm_r(Dir.glob(Gast::PATH + '/**'), secure: true)
22
- end
17
+ after(:each) { cleanup_in_the_dir }
23
18
 
24
19
  it 'should be create of repository' do
25
20
  expect(FileTest.exists?(repo.path)).to be_true
@@ -38,5 +33,4 @@ describe Gast::Repository do
38
33
  expect(git.log.first.message).to eq "commit: #{DateTime.now}"
39
34
  Timecop.return
40
35
  end
41
-
42
36
  end
data/spec/spec_helper.rb CHANGED
@@ -65,3 +65,7 @@ end
65
65
  def latest_language_of_content
66
66
  File.read(latest_repository + '/language')
67
67
  end
68
+
69
+ def cleanup_in_the_dir
70
+ FileUtils.rm_r(Dir.glob(Gast::PATH + '/**'), secure: true)
71
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - futoase
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-27 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -244,6 +244,7 @@ files:
244
244
  - lib/gast.rb
245
245
  - lib/gast/app.rb
246
246
  - lib/gast/base.rb
247
+ - lib/gast/helper.rb
247
248
  - lib/gast/memo.rb
248
249
  - lib/gast/repository.rb
249
250
  - lib/gast/templates/edit.haml