aweplug 1.0.0.a15 → 1.0.0.a16

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.
@@ -14,24 +14,47 @@ module Aweplug
14
14
  # Returns the html snippet
15
15
  #
16
16
  def vimeo(url)
17
- id = video_id(url)
18
- title = video_title(id)
17
+ video = Video.new(url, access_token, site)
19
18
  out = %Q[<div class="embedded-media">] +
20
- %Q[<h4>#{title}</h4>] +
21
- %Q[<iframe src="//player.vimeo.com/video/#{id}\?title=0&byline=0&portrait=0&badge=0&color=2664A2" width="500" height="313" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen}></iframe>]
22
- cast = video_cast(id)
23
- cast.each do |c|
19
+ %Q[<h4>#{video.title}</h4>] +
20
+ %Q[<iframe src="//player.vimeo.com/video/#{video.id}\?title=0&byline=0&portrait=0&badge=0&color=2664A2" width="500" height="313" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen}></iframe>]
21
+ video.cast.each do |c|
24
22
  out += %Q[<div class="follow-links">] +
25
- %Q[<span class="title">Follow #{first_name(c.realname)}</span>] +
26
- %Q[<a><i class="icon-rss"></i></a>] +
27
- %Q[<a><i class="icon-facebook"></i></a>] +
28
- %Q[<a><i class="icon-twitter"></i></a>] +
29
- %Q[<a><i class="icon-linkedin"></i></a>] +
30
- %Q[</div>]
23
+ %Q[<span class="title">Follow #{first_name(c.realname)}</span>] +
24
+ # TODO add in follow links
25
+ %Q[<a><i class="icon-rss"></i></a>] +
26
+ %Q[<a><i class="icon-facebook"></i></a>] +
27
+ %Q[<a><i class="icon-twitter"></i></a>] +
28
+ %Q[<a><i class="icon-linkedin"></i></a>] +
29
+ %Q[</div>]
31
30
  end
32
31
  out + %Q[</div>]
33
32
  end
34
33
 
34
+ # Public: Embeds a vimeo video thumbnail into a web page. Retrieves the title
35
+ # and video cast from vimeo using the authenticated API.
36
+ #
37
+ # url - the URL of the vimeo page for the video to display
38
+ #
39
+ # Returns the html snippet.
40
+ def vimeo_thumb(url)
41
+ video = Video.new(url, access_token, site)
42
+ out = %Q{<a href="#{site.base_url}/video/vimeo/#{video.id}">} +
43
+ %Q{<img src="#{video.thumb_url}" />} +
44
+ %Q{</a>} +
45
+ %Q{<span class="label material-duration">#{video.duration}</span>} +
46
+ # TODO Add this in once the DCP supports manually adding tags
47
+ # %Q{<span class="label material-level-beginner">Beginner<span>} +
48
+ %Q{<h4><a href="#{video.thumb_url}">#{video.title}</a></h4>} +
49
+ # TODO Wire in link to profile URL
50
+ %Q{<p class="author">Author: <a href="#">#{video.author.realname}</a></p>} +
51
+ %Q{<p class="material-datestamp">Added #{video.upload_date}</p>} +
52
+ # TODO wire in ratings
53
+ #%Q{<p class="rating">Video<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-empty"></i><i class="fa fa-star-empty"></i></p>} +
54
+ %Q{<div class="body"><p>#{video.description}</p></div>}
55
+ out
56
+ end
57
+
35
58
  # Internal: Extracts a firstname from a full name
36
59
  #
37
60
  # full_name - the full name, e.g. Pete Muir
@@ -39,87 +62,151 @@ module Aweplug
39
62
  full_name.split[0]
40
63
  end
41
64
 
42
- # Internal: Extracts a Vimeo video id from a Vimeo video URL
43
- #
44
- # url - the url of the video
45
- def video_id(url)
46
- url.match(/^.*\/(\d*)$/)[1]
47
- end
65
+ # Internal: Data object to hold and parse values from the Vimeo API.
66
+ class Video
67
+ def initialize(url, access_token, site)
68
+ @id = url.match(/^.*\/(\d*)$/)[1]
69
+ @site = site
70
+ @access_token = access_token
71
+ fetch_info
72
+ fetch_cast
73
+ fetch_thumb_url
74
+ end
48
75
 
49
- # Internal: Retrieves a video title using the Vimeo API
50
- #
51
- # video_id - the id of the video to fetch the title for
52
- def video_title(video_id)
53
- body = exec_method "vimeo.videos.getInfo", video_id
54
- if body
55
- JSON.parse(body)["video"][0]["title"]
56
- else
57
- "Unable to fetch video info from vimeo"
76
+ def id
77
+ @id
58
78
  end
59
- end
60
79
 
61
- # Internal: Retrieves the cast of a video using the Vimeo API
62
- #
63
- # video_id - the id of the video to fetch the title for
64
- def video_cast(video_id)
65
- body = exec_method "vimeo.videos.getCast", video_id
66
- cast = []
67
- if body
68
- JSON.parse(body)["cast"]["member"].each do |c|
69
- cast << OpenStruct.new(c)
80
+ def title
81
+ @video["title"]
82
+ end
83
+
84
+ def duration
85
+ t = @video["duration"].to_i
86
+ Time.at(t).utc.strftime("%T")
87
+ end
88
+
89
+ def upload_date
90
+ d = @video["upload_date"]
91
+ DateTime.parse(d).strftime("%F %T")
92
+ end
93
+
94
+ def description
95
+ d = @video["description"]
96
+ out = ""
97
+ if d
98
+ i = 0
99
+ max_length = 150
100
+ d.scan(/[^\.!?]+[\.!?]/).map(&:strip).each do |s|
101
+ i += s.length
102
+ if i > max_length
103
+ break
104
+ else
105
+ out += s
106
+ end
107
+ end
70
108
  end
109
+ out
71
110
  end
72
- cast
73
- end
74
111
 
75
- # Internal: Execute a method against the Vimeo API
76
- #
77
- # method - the API method to execute
78
- # video_id - the id of the video to execute the method for
79
- #
80
- # Returns JSON retreived from the Vimeo API
81
- def exec_method(method, video_id)
82
- if access_token
83
- query = "http://vimeo.com/api/rest/v2?method=#{method}&video_id=#{video_id}&format=json"
84
- access_token.get(query).body
112
+ def author
113
+ if @cast[0]
114
+ @cast[0]
115
+ else
116
+ @cast = Openstruct.new({"realname" => "Unknown"})
117
+ end
85
118
  end
86
- end
87
119
 
88
- # Internal: Obtains an OAuth::AcccessToken for the Vimeo API, using the
89
- # vimeo_client_id and vimeo_access_token defined in site/config.yml and
90
- # vimeo_client_secret and vimeo_access_token_secret defined in environment
91
- # variables
92
- #
93
- # Returns an OAuth::AccessToken for the Vimeo API
94
- def access_token
95
- if @access_token
96
- @access_token
97
- else
98
- if not ENV['vimeo_client_secret']
99
- puts 'Cannot fetch video info from vimeo, vimeo_client_secret is missing from environment variables'
100
- return
120
+ def cast
121
+ @cast
122
+ end
123
+
124
+ def thumb_url
125
+ @thumb["_content"]
126
+ end
127
+
128
+ def fetch_info
129
+ body = exec_method "vimeo.videos.getInfo", @id
130
+ if body
131
+ @video = JSON.parse(body)["video"][0]
132
+ else
133
+ @video = {"title" => "Unable to fetch video info from vimeo"}
101
134
  end
102
- if not site.vimeo_client_id
103
- puts 'Cannot fetch video info vimeo, vimeo_client_id is missing from _config/site.yml'
104
- return
135
+ end
136
+
137
+ def fetch_thumb_url
138
+ body = exec_method "vimeo.videos.getThumbnailUrls", @id
139
+ if body
140
+ @thumb = JSON.parse(body)["thumbnails"]["thumbnail"][1]
141
+ else
142
+ @thumb = {"_content" => ""}
105
143
  end
106
- if not ENV['vimeo_access_token_secret']
107
- puts 'Cannot fetch video info from vimeo, vimeo_access_token_secret is missing from environment variables'
108
- return
144
+ end
145
+
146
+ def fetch_cast
147
+ body = exec_method "vimeo.videos.getCast", @id
148
+ @cast = []
149
+ if body
150
+ JSON.parse(body)["cast"]["member"].each do |c|
151
+ o = OpenStruct.new(c)
152
+ if o.username != "jbossdeveloper"
153
+ @cast << o
154
+ end
155
+ end
109
156
  end
110
- if not site.vimeo_access_token
111
- puts 'Cannot fetch video info from vimeo, vimeo_access_token is missing from _config/site.yml'
112
- return
157
+ end
158
+
159
+ # Internal: Execute a method against the Vimeo API
160
+ #
161
+ # method - the API method to execute
162
+ # video_id - the id of the video to execute the method for
163
+ #
164
+ # Returns JSON retreived from the Vimeo API
165
+ def exec_method(method, video_id)
166
+ if access_token
167
+ query = "http://vimeo.com/api/rest/v2?method=#{method}&video_id=#{video_id}&format=json"
168
+ access_token.get(query).body
113
169
  end
114
- consumer = OAuth::Consumer.new(site.vimeo_client_id, ENV['vimeo_client_secret'],
115
- { :site => "https://vimeo.com",
116
- :scheme => :header
170
+ end
171
+
172
+ # Internal: Obtains an OAuth::AcccessToken for the Vimeo API, using the
173
+ # vimeo_client_id and vimeo_access_token defined in site/config.yml and
174
+ # vimeo_client_secret and vimeo_access_token_secret defined in environment
175
+ # variables
176
+ #
177
+ # site - Awestruct Site instance
178
+ #
179
+ # Returns an OAuth::AccessToken for the Vimeo API
180
+ def access_token
181
+ if @access_token
182
+ @access_token
183
+ else
184
+ if not ENV['vimeo_client_secret']
185
+ puts 'Cannot fetch video info from vimeo, vimeo_client_secret is missing from environment variables'
186
+ return
187
+ end
188
+ if not @site.vimeo_client_id
189
+ puts 'Cannot fetch video info vimeo, vimeo_client_id is missing from _config/site.yml'
190
+ return
191
+ end
192
+ if not ENV['vimeo_access_token_secret']
193
+ puts 'Cannot fetch video info from vimeo, vimeo_access_token_secret is missing from environment variables'
194
+ return
195
+ end
196
+ if not @site.vimeo_access_token
197
+ puts 'Cannot fetch video info from vimeo, vimeo_access_token is missing from _config/site.yml'
198
+ return
199
+ end
200
+ consumer = OAuth::Consumer.new(@site.vimeo_client_id, ENV['vimeo_client_secret'],
201
+ { :site => "https://vimeo.com",
202
+ :scheme => :header
117
203
  })
118
- # now create the access token object from passed values
119
- token_hash = { :oauth_token => site.vimeo_access_token,
120
- :oauth_token_secret => ENV['vimeo_access_token_secret']
121
- }
122
- OAuth::AccessToken.from_hash(consumer, token_hash )
204
+ # now create the access token object from passed values
205
+ token_hash = { :oauth_token => @site.vimeo_access_token,
206
+ :oauth_token_secret => ENV['vimeo_access_token_secret']
207
+ }
208
+ OAuth::AccessToken.from_hash(consumer, token_hash )
209
+ end
123
210
  end
124
211
  end
125
212
  end
@@ -1,4 +1,4 @@
1
1
  module Aweplug
2
- VERSION='1.0.0.a15'
2
+ VERSION='1.0.0.a16'
3
3
  end
4
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aweplug
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.a15
4
+ version: 1.0.0.a16
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-11 00:00:00.000000000 Z
12
+ date: 2014-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: octokit
@@ -190,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
190
  version: '0'
191
191
  segments:
192
192
  - 0
193
- hash: 1205855873976585474
193
+ hash: -3016901967987087946
194
194
  required_rubygems_version: !ruby/object:Gem::Requirement
195
195
  none: false
196
196
  requirements: