conred 0.2.6 → 0.2.7

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.
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in conred.gemspec
4
- gemspec
5
- gem 'rake'
6
- gem 'haml'
4
+ gemspec
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
+ [![Code Climate](https://codeclimate.com/github/janjiss/conred.png)](https://codeclimate.com/github/janjiss/conred)
2
+ [![Build Status](https://travis-ci.org/janjiss/conred.png?branch=master)](https://travis-ci.org/janjiss/conred)
3
+
1
4
  # Conred
2
5
 
3
6
  In every project we have common things like video
4
- embeding from url, body formating (Of course we have Textile, but it is not good for all cases),
5
- external url protocol adding. These are the cases where Conred saves the day.
7
+ embeding from url, user input displaying, formating, trimming stripping,
8
+ external url protocol adding and all that nasty stuff that we write in our apps.
9
+ These are the cases where Conred saves the day.
6
10
 
7
11
  ## Installation
8
12
 
@@ -20,20 +24,31 @@ Or install it yourself as:
20
24
 
21
25
  ## Usage
22
26
 
23
- Right now (version 0.0.8) has several helpers and video class.
24
- For video you can use:
27
+ ### Iframe generetor for Youtube and Vimeo videos:
25
28
 
26
- c = Conred::Video.new("http://www.youtube.com/watch?v=Lrj5Kxdzouc")
29
+ c = Conred::Video.new(
30
+ video_url: "http://www.youtube.com/watch?v=tNtW9pGFPTA&feature=plcp",
31
+ width: 285,
32
+ height: 185,
33
+ error_message: "Video url is invalid"
34
+ )
27
35
 
28
- Then for embed code:
36
+ __NOTE:__ This is new constructor type as of version 0.3.0.
37
+
38
+ Then you can get your ready embed code like this (Conred will recognize video provider by itself):
29
39
 
30
40
  c.code
31
41
 
32
- You can also check if it is youtube video or vimeo video like this:
42
+ You can also check if it is youtube or vimeo video like this:
33
43
 
34
44
  c.youtube_video? ==> true
35
45
  c.vimeo_video? ==> false
36
- Currently video feature works with Youtube and Vimeo URL's
46
+
47
+ Or if it exists:
48
+
49
+ c.exists? ==> true
50
+
51
+ ### General helpers for rails app
37
52
 
38
53
  If you wish to use text helpers then in your application_helper add this include line:
39
54
 
@@ -51,8 +66,7 @@ Sanitizes body, allowed tags are(p a strong ul ol li blockquote strike u em):
51
66
 
52
67
  External link formating
53
68
 
54
- external_url("www.google.lv") => "http://www.google.lv"
55
-
69
+ external_url("www.google.com") => "http://www.google.com"
56
70
 
57
71
  ## Contributing
58
72
 
@@ -61,3 +75,9 @@ External link formating
61
75
  3. Create tests for it (Important!)
62
76
  4. Create new Pull Request
63
77
  5. Be happy
78
+
79
+ ## Thank you's
80
+
81
+ I would like to thank these guys for contributing:
82
+ @alexcp
83
+ @barisbalic
data/conred.gemspec CHANGED
@@ -8,8 +8,10 @@ Gem::Specification.new do |gem|
8
8
  gem.summary = %q{ConcepticHQ reusable code}
9
9
  gem.homepage = "http://github.com/janjiss/conred"
10
10
 
11
+ gem.add_dependency "haml"
11
12
  gem.add_development_dependency "rspec"
12
- gem.add_dependency "actionpack"
13
+ gem.add_development_dependency "rake"
14
+ gem.add_development_dependency "actionpack"
13
15
 
14
16
  gem.files = `git ls-files`.split($\)
15
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
data/lib/conred.rb CHANGED
@@ -5,5 +5,13 @@ require "conred/video"
5
5
  require "conred/links"
6
6
  require "haml"
7
7
  module Conred
8
-
9
- end
8
+ def Video.new arguments
9
+ if Video::Youtube.url_format_is_valid? arguments[:video_url]
10
+ Video::Youtube.new arguments
11
+ elsif Video::Vimeo.url_format_is_valid? arguments[:video_url]
12
+ Video::Vimeo.new arguments
13
+ else
14
+ Video::Other.new arguments
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Conred
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
data/lib/conred/video.rb CHANGED
@@ -2,80 +2,104 @@ require "conred/version"
2
2
  require "action_view"
3
3
  require "net/http"
4
4
  module Conred
5
- class Video
6
- def initialize(video_url, width = 670, height = 450, error_message = "Video url you have provided is invalid")
7
- @width = width
8
- @height = height
9
- @video_url = video_url
10
- @error_message = error_message
11
- end
12
-
13
- def code
14
- if youtube_video?
15
- video_from_youtube_url
16
- elsif vimeo_video?
17
- video_from_vimeo_url
18
- else
19
- @error_message
20
- end
5
+ module Video
6
+ def initialize(arguments = {:width => 670, :height => 450, :error_message => "Video url you have provided is invalid"})
7
+ @width = arguments[:width]
8
+ @height = arguments[:height]
9
+ @error_message = arguments[:error_message]
10
+ @video_id = get_video_id_from arguments[:video_url]
21
11
  end
22
12
 
23
13
  def youtube_video?
24
- /^(http:\/\/)*(www\.)*(youtube.com|youtu.be)/ =~ @video_url ? true : false
14
+ is_a?(Video::Youtube)
25
15
  end
26
16
 
27
17
  def vimeo_video?
28
- /^(http:\/\/)*(www\.)*(vimeo.com)/ =~ @video_url ? true : false
18
+ is_a?(Video::Vimeo)
29
19
  end
30
20
 
31
-
32
- def video_from_vimeo_url
33
- vimeo_partial = "../views/video/vimeo_iframe"
21
+ def code
34
22
  render(
35
- vimeo_partial,
36
- :vimeo_id => video_id,
37
- :height => @height,
23
+ :video_link => video_link,
24
+ :height => @height,
38
25
  :width => @width
39
26
  ).html_safe
40
27
  end
41
28
 
42
- def video_from_youtube_url
43
- youtube_partial = "../views/video/youtube_iframe"
44
- render(
45
- youtube_partial,
46
- :youtube_id => video_id,
47
- :height => @height,
48
- :width => @width
49
- ).html_safe
29
+ def exist?
30
+ response = Net::HTTP.get_response(URI(api_uri))
31
+ response.is_a?(Net::HTTPSuccess)
50
32
  end
51
33
 
52
- def render(path_to_partial, locals = {})
34
+ private
35
+
36
+ def render(locals = {})
53
37
  path = File.join(
54
38
  File.dirname(__FILE__),
55
- path_to_partial.split("/")
39
+ "../views/video/video_iframe".split("/")
56
40
  )
57
41
  Haml::Engine.new(File.read("#{path}.html.haml")).render(Object.new, locals)
58
42
  end
59
43
 
60
- def video_id
61
- if @video_url[/vimeo\.com\/([0-9]*)/]
62
- video_id = $1
63
- elsif @video_url[/youtu\.be\/([^\?]*)/]
64
- video_id = $1
65
- else
66
- @video_url[/(v=([A-Za-z0-9_-]*))/]
67
- video_id = $2
44
+ class Youtube
45
+ include Video
46
+
47
+ def self.url_format_is_valid? url
48
+ /^(http:\/\/)*(www\.)*(youtube.com|youtu.be)/ =~ url
49
+ end
50
+
51
+ def api_uri
52
+ "http://gdata.youtube.com/feeds/api/videos/#{@video_id}"
53
+ end
54
+
55
+ def video_link
56
+ "http://www.youtube.com/embed/#{@video_id}?wmode=transparent"
57
+ end
58
+
59
+ private
60
+
61
+ def get_video_id_from url
62
+ if url[/youtu\.be\/([^\?]*)/]
63
+ video_id = $1
64
+ else
65
+ url[/(v=([A-Za-z0-9_-]*))/]
66
+ video_id = $2
67
+ end
68
68
  end
69
69
  end
70
70
 
71
- def exist?
72
- if youtube_video?
73
- response = Net::HTTP.get_response(URI("http://gdata.youtube.com/feeds/api/videos/#{video_id}"))
74
- else
75
- response = Net::HTTP.get_response(URI("http://vimeo.com/api/v2/video/#{video_id}.json"))
71
+ class Vimeo
72
+ include Video
73
+
74
+ def self.url_format_is_valid? url
75
+ /^(http:\/\/)*(www\.)*(vimeo.com)/ =~ url
76
+ end
77
+
78
+ def api_uri
79
+ "http://vimeo.com/api/v2/video/#{@video_id}.json"
80
+ end
81
+
82
+ def video_link
83
+ "http://player.vimeo.com/video/#{@video_id}"
84
+ end
85
+
86
+ private
87
+
88
+ def get_video_id_from url
89
+ url[/vimeo\.com\/([0-9]*)/]
90
+ video_id = $1
76
91
  end
77
- response.is_a?(Net::HTTPSuccess)
78
92
  end
79
93
 
94
+ class Other
95
+ include Video
96
+ def initialize(arguments = {:error_message => "Video url you have provided is invalid"})
97
+ @error_message = arguments[:error_message]
98
+ end
99
+
100
+ def code
101
+ @error_message
102
+ end
103
+ end
80
104
  end
81
105
  end
@@ -0,0 +1 @@
1
+ %iframe{:allowFullScreen => "", :frameborder => "0", :height => height, :mozallowfullscreen => "", :src => "#{video_link}", :webkitAllowFullScreen => "", :width => width}
@@ -3,14 +3,14 @@ require "conred"
3
3
  describe Conred do
4
4
  describe Conred::Video do
5
5
 
6
- let(:short_youtube_url) {Conred::Video.new("http://youtu.be/SZt5RFzqEfY")}
7
- let(:short_youtube_url_with_www) {Conred::Video.new("www.youtu.be/SZt5RFzqEfY")}
8
- let(:long_youtube_url_with_features) {Conred::Video.new("http://www.youtube.com/watch?NR=1&feature=endscreen&v=Lrj5Kxdzouc")}
9
- let(:short_youtube_url_without_http_and_www) {Conred::Video.new("youtu.be/SZt5RFzqEfY")}
10
-
11
- let(:vimeo_url) {Conred::Video.new("http://vimeo.com/12311233")}
12
- let(:evil_vimeo) {Conred::Video.new("eeevil vimeo www.vimeo.com/12311233")}
13
- let(:vimeo_without_http) {Conred::Video.new("vimeo.com/12311233")}
6
+ let(:short_youtube_url) {Conred::Video.new(:video_url=>"http://youtu.be/SZt5RFzqEfY")}
7
+ let(:short_youtube_url_with_www) {Conred::Video.new(:video_url=>"www.youtu.be/SZt5RFzqEfY")}
8
+ let(:long_youtube_url_with_features) {Conred::Video.new(:video_url=>"http://www.youtube.com/watch?NR=1&feature=endscreen&v=Lrj5Kxdzouc")}
9
+ let(:short_youtube_url_without_http_and_www) {Conred::Video.new(:video_url=>"youtu.be/SZt5RFzqEfY")}
10
+
11
+ let(:vimeo_url) {Conred::Video.new(:video_url=>"http://vimeo.com/12311233")}
12
+ let(:evil_vimeo) {Conred::Video.new(:video_url=>"eeevil vimeo www.vimeo.com/12311233")}
13
+ let(:vimeo_without_http) {Conred::Video.new(:video_url=>"vimeo.com/12311233")}
14
14
 
15
15
  it "should match youtube video" do
16
16
  short_youtube_url.should be_youtube_video
@@ -30,26 +30,42 @@ describe Conred do
30
30
  vimeo_url.should be_vimeo_video
31
31
  end
32
32
 
33
- it "should return correct embed code" do
34
- Conred::Video.new("http://www.youtube.com/watch?NR=1&feature=endscreen&v=Lrj5Kxdzouc", 450, 300).code.should match(/Lrj5Kxdzouc/)
35
- Conred::Video.new("http://www.youtube.com/watch?v=Lrj5Kxdzouc", 450, 300).code.should match(/Lrj5Kxdzouc/)
36
- Conred::Video.new("http://www.youtube.com/watch?v=Lrj5Kxdzouc", 450, 300).code.should match(/width='450'/)
37
- Conred::Video.new("http://www.youtube.com/watch?v=Lrj5Kxdzouc", 450, 300).code.should match(/height='300'/)
38
- Conred::Video.new("http://vimeo.com/49556689", 450, 300).code.should match(/49556689/)
39
- Conred::Video.new("http://vimeo.com/49556689", 450, 300).code.should match(/width='450'/)
40
- Conred::Video.new("http://vimeo.com/49556689", 450, 300).code.should match(/height='300'/)
41
- Conred::Video.new("http://google.com/12311233", 450, 300, "Some mistake in url").code.should == "Some mistake in url"
33
+ describe "youtube embed code" do
34
+ subject {Conred::Video.new(:video_url=>"http://www.youtube.com/watch?v=Lrj5Kxdzouc", :width=>450,:height=> 300).code }
35
+ it { should match(/Lrj5Kxdzouc/)}
36
+ it { should match(/width='450'/)}
37
+ it {should match(/height='300'/)}
38
+ end
39
+
40
+ describe "vimeo embed code" do
41
+ subject { Conred::Video.new(:video_url=>"http://vimeo.com/49556689", :width=>450, :height=>300).code }
42
+ it {should match(/49556689/)}
43
+ it {should match(/width='450'/)}
44
+ it {should match(/height='300'/)}
45
+ end
46
+
47
+ it "should render error message when url is invalid" do
48
+ Conred::Video.new(:video_url=>"http://google.com/12311233", :width=>450, :height=>300, :error_message=>"Some mistake in url").code.should == "Some mistake in url"
49
+ end
50
+
51
+ it "should return correct embed code when passing arguments in url" do
52
+ Conred::Video.new(:video_url=>"http://www.youtube.com/watch?NR=1&feature=endscreen&v=Lrj5Kxdzouc",:width=> 450,:height=> 300).code.should match(/Lrj5Kxdzouc/)
42
53
  end
43
54
 
44
55
  describe "check if a video exist" do
45
56
  it "should return false if request 404" do
46
- non_existing_video = Conred::Video.new("http://www.youtube.com/watch?v=Lrj5Kxdzoux")
57
+ non_existing_video = Conred::Video.new(:video_url=>"http://www.youtube.com/watch?v=Lrj5Kxdzoux")
47
58
  Net::HTTP.stub(:get_response=>Net::HTTPNotFound.new(true, 404, "Not Found"))
48
59
  non_existing_video.exist?.should be_false
49
60
  end
50
61
 
62
+ it "should make a request to the proper uri" do
63
+ non_existing_video = Conred::Video.new(:video_url=>"http://www.youtube.com/watch?v=Lrj5Kxdzoux")
64
+ non_existing_video.api_uri.should eq("http://gdata.youtube.com/feeds/api/videos/Lrj5Kxdzoux")
65
+ end
66
+
51
67
  it "should be true if response is 200" do
52
- existing_video = Conred::Video.new("http://www.youtube.com/watch?v=Lrj5Kxdzouc")
68
+ existing_video = Conred::Video.new(:video_url=>"http://www.youtube.com/watch?v=Lrj5Kxdzouc")
53
69
  Net::HTTP.stub(:get_response=>Net::HTTPOK.new(true,200,"OK"))
54
70
  existing_video.exist?.should be_true
55
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conred
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-11 00:00:00.000000000 Z
12
+ date: 2013-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: haml
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'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: rspec
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -27,6 +43,22 @@ dependencies:
27
43
  - - ! '>='
28
44
  - !ruby/object:Gem::Version
29
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: actionpack
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -35,7 +67,7 @@ dependencies:
35
67
  - - ! '>='
36
68
  - !ruby/object:Gem::Version
37
69
  version: '0'
38
- type: :runtime
70
+ type: :development
39
71
  prerelease: false
40
72
  version_requirements: !ruby/object:Gem::Requirement
41
73
  none: false
@@ -61,8 +93,7 @@ files:
61
93
  - lib/conred/links.rb
62
94
  - lib/conred/version.rb
63
95
  - lib/conred/video.rb
64
- - lib/views/video/vimeo_iframe.html.haml
65
- - lib/views/video/youtube_iframe.html.haml
96
+ - lib/views/video/video_iframe.html.haml
66
97
  - spec/conred_spec.rb
67
98
  - spec/conred_spec/helpers_spec.rb
68
99
  - spec/conred_spec/video_spec.rb
@@ -1 +0,0 @@
1
- %iframe{:allowFullScreen => "", :frameborder => "0", :height => height, :mozallowfullscreen => "", :src => "http://player.vimeo.com/video/#{vimeo_id}", :webkitAllowFullScreen => "", :width => width}
@@ -1 +0,0 @@
1
- %iframe{:allowfullscreen => "", :frameborder => "0", :height => height, :src => "http://www.youtube.com/embed/#{youtube_id}?wmode=transparent", :title => "YouTube video player", :width => width}