sinatra-disqus 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ bin/
19
+ vendor/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/CHANGES.md ADDED
@@ -0,0 +1,13 @@
1
+ ## CH-CH-CH-CH-CHANGES! ##
2
+
3
+ ### Saturday the 20th of April 2013, v1.0.0 ###
4
+
5
+ * Updated the docs a bit.
6
+ * Bumped to v1.0.0 for semver.
7
+
8
+ ### Thursday the 9th of August 2012, v0.0.1 ###
9
+
10
+ * First release.
11
+ * It has a spec!
12
+
13
+ ----
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra-extension-skeleton.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem "rake"
8
+ gem "wirble"
9
+ gem "yard"
10
+ gem "maruku"
11
+ end
12
+
13
+ group :test do
14
+ gem "rack-test"
15
+ gem "rspec"
16
+ gem "simplecov"
17
+ end
data/LICENCE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Iain Barnett
2
+
3
+ MIT Licence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicence, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ ## Sinatra Disqus ##
2
+
3
+ Drop in disqus to any Sinatra page via a handy helper.
4
+
5
+ ### Installation ###
6
+
7
+ gem install sinatra-disqus
8
+
9
+ or via Bundler:
10
+
11
+ gem "sinatra-disqus"
12
+
13
+
14
+ ### Example ###
15
+
16
+ #### Classic style
17
+
18
+ require 'sinatra'
19
+ require 'haml'
20
+ require 'sinatra/disqus'
21
+
22
+ configure do
23
+ # Disqus settings
24
+ set :disqus_shortname, "example.org"
25
+ # Put disqus into developer mode unless in production.
26
+ set :disqus_developer, !production?
27
+ end
28
+
29
+ get "/" do
30
+ @title = "Home"
31
+ haml :home
32
+ end
33
+
34
+ __END__
35
+
36
+ @@ layout
37
+ !!!
38
+ %body
39
+ = yield
40
+
41
+ @@ home
42
+ #main
43
+ %p
44
+ This is the home page.
45
+ %p
46
+ Now for some comments:
47
+ #comments
48
+ inject_disqus
49
+
50
+ #### Modular style ####
51
+
52
+ require 'sinatra/base' # for modular apps
53
+ require 'haml'
54
+ require 'sinatra/disqus'
55
+
56
+ class Example < Sinatra::Base
57
+ register Sinatra::Disqus # because it's modular
58
+
59
+ configure do
60
+ enable :inline_templates # just for this example
61
+
62
+ # Disqus settings
63
+ set :disqus_shortname, "example.org"
64
+ # Put disqus into developer mode unless in production.
65
+ set :disqus_developer, !production?
66
+ end
67
+
68
+ get "/" do
69
+ @title = "Home"
70
+ haml :home
71
+ end
72
+ end
73
+
74
+ __END__
75
+
76
+ @@ layout
77
+ !!!
78
+ %body
79
+ = yield
80
+
81
+ @@ home
82
+ #main
83
+ %p
84
+ This is the home page.
85
+ %p
86
+ Now for some comments:
87
+ #comments
88
+ inject_disqus # you'd probably do this at the bottom of a view instead.
89
+
90
+ ### Licence ###
91
+
92
+ Look in the LICENCE file.
data/Rakefile ADDED
@@ -0,0 +1,61 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ P = /MODULENAME/
4
+
5
+ def edit_in_place( file_name, module_name )
6
+ File.open(file_name, 'r+') do |f| # open file for update
7
+ lines = f.readlines # read into array of lines
8
+ lines.each do |it| # modify lines
9
+ if it =~ P
10
+ if it =~ /(?:\/|\-)MODULE/
11
+ it.gsub!(P, module_name.downcase)
12
+ else
13
+ it.gsub!(P, module_name)
14
+ end
15
+ it
16
+ end
17
+ end
18
+ f.pos = 0 # back to start
19
+ f.print lines.join # write out modified lines
20
+ f.truncate(f.pos) # truncate to new length
21
+ end
22
+ end
23
+
24
+
25
+
26
+ desc "Rename the files and folders of the project from MODULENAME to something useful."
27
+ task :rename do
28
+ new_name = ENV["n"]
29
+ Dir["./**/*"].reject{|f| f["/vendor"] || f["/bin"] || f["Rakefile"] || f["Gemfile"] || f["LICENCE"] || f["README.md"] }.sort_by{|n| n.length }.reverse.each do |file|
30
+ if File.exists?(file)
31
+ File.file?(file) && edit_in_place(file,new_name)
32
+ if file =~ P
33
+ cmd = "git mv #{file} #{file.sub(P,new_name).downcase}"
34
+ warn cmd
35
+ system(cmd)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ desc "(Re-) generate documentation and place it in the docs/ dir."
43
+ task :docs => :"docs:yard"
44
+ namespace :docs do
45
+ require 'yard'
46
+ YARD::Rake::YardocTask.new do |t|
47
+ t.files = ['lib/**/*.rb']
48
+ t.options = ['-odocs/', '--no-private']
49
+ end
50
+
51
+ desc "Docs including private methods."
52
+ YARD::Rake::YardocTask.new(:all) do |t|
53
+ t.files = ['lib/**/*.rb']
54
+ t.options = ['-odocs/']
55
+ end
56
+
57
+ desc "How to use the docs."
58
+ task :usage do
59
+ puts "Open the index.html file in the docs directory to read them. Does not include methods marked private unless you ran the 'all' version (you'll only need these if you plan to hack on the library itself)."
60
+ end
61
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ module Sinatra
4
+ module Disqus
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,90 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'sinatra/base'
4
+ require 'haml'
5
+
6
+ module Sinatra
7
+ module Disqus
8
+
9
+ Disqus_output_top = <<TOP
10
+ #disqus_thread
11
+ :javascript
12
+ TOP
13
+ Disqus_output_bottom = <<BOTTOM
14
+ (function() {
15
+ var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
16
+ dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
17
+ (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
18
+ })();
19
+
20
+ %noscript
21
+ Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a>
22
+ %p
23
+ <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
24
+ BOTTOM
25
+
26
+ # @private
27
+ module Private
28
+ require 'digest/md5'
29
+
30
+ # Unique (repeatable) identifier for page.
31
+ # @param [String] The page uri.
32
+ def self.disqus_identifier( request_path )
33
+ Digest::MD5.hexdigest(request_path)
34
+ end
35
+
36
+
37
+ # @param [Hash] opts
38
+ # @option opts [String] :title The page title, usually held in @title
39
+ # @option opts [0,1] :disqus_developer The development mode.
40
+ # @option opts [String] :disqus_shortname The short name of the site (e.g. example.org )
41
+ # @option opts [String] :disqus_identifier The unique identifier the page the comments sit on.
42
+ # @option opts [String] :request_url The page's url.
43
+ # @return [String] The portion of the disqus javascript with the vars.
44
+ def self.build_vars( opts )
45
+ <<STR
46
+ var disqus_title = "#{opts[:title].gsub(/'/, "\\\\'")}";
47
+ var disqus_developer = #{opts[:disqus_developer]}; // developer mode
48
+ var disqus_shortname = '#{opts[:disqus_shortname]}';
49
+ var disqus_identifier = '#{opts[:disqus_identifier]}'; // is on if 1
50
+ var disqus_url = '#{opts[:request_url]}';
51
+ STR
52
+ end
53
+ end
54
+
55
+
56
+ module Helpers
57
+
58
+ # @return [String] Returns the bit of HTML/javascript to stick in the page.
59
+ def inject_disqus
60
+ Haml::Engine.new( "#{Disqus_output_top}#{Private.build_vars(request_url: uri(request.path_info), disqus_shortname: settings.disqus_shortname, disqus_developer: settings.disqus_developer, title: @title, disqus_identifier: Private.disqus_identifier(request.path))}#{Disqus_output_bottom}" ).render
61
+ end
62
+
63
+ end
64
+
65
+
66
+ def self.registered(app)
67
+ app.helpers Disqus::Helpers
68
+
69
+
70
+ # @param [String] s The short name of the site (e.g. example.org )
71
+ app.set :disqus_shortname, nil
72
+
73
+
74
+ # @param [true,false,nil] bool Whether developer mode is on (true), off (false) or set to use the default (nil), which is the current environment (if production then off).
75
+ def disqus_developer=(bool)
76
+ @disqus_developer = bool
77
+ end
78
+
79
+ # @return [0,1] 0 is off, 1 is on.
80
+ def disqus_developer
81
+ if @disqus_developer.nil?
82
+ @disqus_developer = ENV['RACK_ENV'] == "production" ? false : true
83
+ end
84
+ @disqus_developer ? 1 : 0
85
+ end
86
+
87
+ end # registered
88
+ end
89
+ register Disqus
90
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sinatra/disqus/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Iain Barnett"]
6
+ gem.email = ["iainspeed@gmail.com"]
7
+ gem.description = %q{Drop in disqus to any Sinatra view via a handy helper.}
8
+ gem.summary = %q{Easy disqus for Sinatra.}
9
+ gem.homepage = "https://bitbucket.org/yb66/sinatra-disqus"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sinatra-disqus"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sinatra::Disqus::VERSION
17
+ gem.add_dependency( "sinatra" )
18
+ gem.add_dependency( "haml" )
19
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+
3
+ require "rspec_helper"
4
+
5
+ require_relative "../lib/sinatra/Disqus.rb"
6
+
7
+ describe "Disqus" do
8
+ before{ get '/' }
9
+ include_context "All pages"
10
+ it_should_behave_like "Any route"
11
+ context "Output" do
12
+ let(:expected) { <<STR
13
+ <div id='disqus_thread'>
14
+ <script type='text/javascript'>
15
+ //<![CDATA[
16
+ var disqus_title = "Home";
17
+ var disqus_developer = 1; // developer mode
18
+ var disqus_shortname = 'example.org';
19
+ var disqus_identifier = '6666cd76f96956469e7be39d750cc7d9'; // is on if 1
20
+ var disqus_url = 'http://example.org/';
21
+ (function() {
22
+ var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
23
+ dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
24
+ (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
25
+ })();
26
+ //]]>
27
+ </script>
28
+ <noscript>
29
+ Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a>
30
+ </noscript>
31
+ <p>
32
+ <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
33
+ </p>
34
+ </div>
35
+ STR
36
+ }
37
+ subject{ last_response.body }
38
+ it { should == expected }
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec'
4
+ require "rack/test"
5
+
6
+ # code coverage
7
+ require 'simplecov'
8
+ SimpleCov.start do
9
+ add_filter "/vendor/"
10
+ add_filter "/bin/"
11
+ end
12
+
13
+ ENV['RACK_ENV'] ||= 'test'
14
+ ENV["EXPECT_WITH"] ||= "racktest"
15
+
16
+
17
+ Spec_dir = File.expand_path( File.dirname __FILE__ )
18
+
19
+
20
+ require "logger"
21
+ logger = Logger.new STDOUT
22
+ logger.level = Logger::DEBUG
23
+ logger.datetime_format = '%a %d-%m-%Y %H%M '
24
+ LOgger = logger
25
+
26
+
27
+
28
+ Dir[ File.join( Spec_dir, "/support/**/*.rb")].each do |f|
29
+ logger.info "requiring #{f}"
30
+ require f
31
+ end
32
+
33
+
34
+ RSpec.configure do |config|
35
+ config.treat_symbols_as_metadata_keys_with_true_values = true
36
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'sinatra/base'
4
+ require_relative "../../../lib/sinatra/Disqus.rb"
5
+
6
+ class Example < Sinatra::Base
7
+ register Sinatra::Disqus
8
+
9
+ configure do
10
+ set :disqus_shortname, "example.org"
11
+ end
12
+
13
+ get "/" do
14
+ @title = "Home"
15
+ inject_disqus
16
+ end
17
+ end
18
+
19
+ shared_context "All pages" do
20
+ include Rack::Test::Methods
21
+ let(:app){ Example }
22
+ end
23
+
24
+ shared_examples_for "Any route" do
25
+ subject { last_response }
26
+ it { should be_ok }
27
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-disqus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Iain Barnett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: haml
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Drop in disqus to any Sinatra view via a handy helper.
47
+ email:
48
+ - iainspeed@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - CHANGES.md
56
+ - Gemfile
57
+ - LICENCE
58
+ - README.md
59
+ - Rakefile
60
+ - lib/sinatra/disqus.rb
61
+ - lib/sinatra/disqus/version.rb
62
+ - sinatra-disqus.gemspec
63
+ - spec/disqus_spec.rb
64
+ - spec/rspec_helper.rb
65
+ - spec/support/shared/all_routes.rb
66
+ homepage: https://bitbucket.org/yb66/sinatra-disqus
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.25
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Easy disqus for Sinatra.
90
+ test_files:
91
+ - spec/disqus_spec.rb
92
+ - spec/rspec_helper.rb
93
+ - spec/support/shared/all_routes.rb