lumiere 0.0.8 → 0.1.0

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: 181c9f99a468e59da34456795def60868712e56b
4
- data.tar.gz: 47ea9aeab6778b32dea015e865a39730005938cf
3
+ metadata.gz: 62fa0f81be349c04c0c59893fa3933f82d96b802
4
+ data.tar.gz: 6cef7cfa92eb369e2400b529ff041f6fb209642e
5
5
  SHA512:
6
- metadata.gz: b14a9fe81e877dc06954a084e256b6cb67200f555b451ebb0f073d1714dc365d17f899f50cb9f3b9855a966857c08c6bf27cd087310eab73a2f38382a93759d0
7
- data.tar.gz: 5bf3581d0e13ed1a0c456a8a2f1d0d05cd7351a30cd159700dacc19cfb72a8cae792b6f494887e3913691320a2b2002ea708fafe0bbd70a01dacbd6f130e311c
6
+ metadata.gz: 9047d106b199f42e6b98c47801219421d9207df90914807f55d2c1a12a90ba2ea476461ddced51c6af279c001bb373ef42796f5d51e2d989ccc1fd289576491e
7
+ data.tar.gz: 4b8ccafdef4ee51c51374fa0211719b323e9a152ab09726ab9003c199bfa5d6d7b2b2c703bdb1e077a9ad6e8703aad8344d3543fbd42a7d18c33528d3d0dc166
data/lib/fetcher.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'open-uri'
2
+
3
+ module Lumiere
4
+ class Fetcher
5
+
6
+ def initialize(context)
7
+ @api_url = context.api_url
8
+ @unpack_into = context.unpack_into
9
+ end
10
+
11
+ def remote_attributes
12
+ unless request_hash[@api_url]
13
+ request_hash[@api_url] = unpack
14
+ end
15
+ request_hash[@api_url]
16
+ end
17
+
18
+ private
19
+
20
+ def request_hash
21
+ @request_hash ||= {}
22
+ end
23
+
24
+ def unpack
25
+ @unpack_into.from_json(raw_response)
26
+ end
27
+
28
+ def raw_response
29
+ open(@api_url).read
30
+ end
31
+
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Lumiere
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/lumiere.rb CHANGED
@@ -11,6 +11,7 @@ require 'representable/json/collection'
11
11
  require_relative 'extended_uri'
12
12
  require_relative 'provider'
13
13
  require_relative 'playlist'
14
+ require_relative 'fetcher'
14
15
 
15
16
  class Elluminate
16
17
  extend Forwardable
data/lib/playlist.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  module Lumiere
2
2
  module Playlist
3
3
  def self.page_count(total, per_page)
4
- page_count = total / per_page
5
- page_count += 1 unless total % per_page == 0
6
- page_count
4
+ mid_point = total.to_f / per_page.to_f
5
+ mid_point.ceil
7
6
  end
8
7
  end
9
8
  end
@@ -0,0 +1,84 @@
1
+ module Lumiere
2
+ class Dailymotion < Provider
3
+ attr_accessor :url
4
+
5
+ USEABLE = ['www.dailymotion.com', 'dailymotion.com']
6
+
7
+ def self.useable?(url)
8
+ uri = URISchemeless.parse(url)
9
+ USEABLE.include?(uri.host)
10
+ end
11
+
12
+ def initialize(url)
13
+ @url = url
14
+ end
15
+
16
+ def provider
17
+ "Dailymotion"
18
+ end
19
+
20
+ def video_id
21
+ @video_id ||= calculate_video_id
22
+ end
23
+
24
+ def api_url
25
+ "https://api.dailymotion.com/video/#{video_id}?fields=id,title,description,duration,created_time,url,thumbnail_720_url,thumbnail_240_url,thumbnail_60_url"
26
+ end
27
+
28
+ def embed_url
29
+ "http://www.dailymotion.com/embed/video/#{video_id}"
30
+ end
31
+
32
+ def embed_code
33
+ "<iframe frameborder=\"0\" src=\"//www.dailymotion.com/embed/video/#{video_id}\" allowfullscreen></iframe>"
34
+ end
35
+
36
+ def title
37
+ fetch.title
38
+ end
39
+
40
+ def description
41
+ fetch.description
42
+ end
43
+
44
+ def duration
45
+ fetch.duration
46
+ end
47
+
48
+ def upload_date
49
+ fetch.upload_date
50
+ end
51
+
52
+ def thumbnail_small
53
+ fetch.thumbnail_small
54
+ end
55
+
56
+ def thumbnail_medium
57
+ fetch.thumbnail_medium
58
+ end
59
+
60
+ def thumbnail_large
61
+ fetch.thumbnail_large
62
+ end
63
+
64
+ def unpack_into
65
+ struct = OpenStruct.new
66
+ struct.extend(DailymotionVideoRepresenter)
67
+ end
68
+
69
+ private
70
+
71
+ def fetch
72
+ @fetch ||= Fetcher.new(self).remote_attributes
73
+ end
74
+
75
+ def calculate_video_id
76
+ uri = URISchemeless.parse(url)
77
+ uri.path.gsub!('/embed/video/', '')
78
+ uri.path.gsub!('/video/', '')
79
+ uri.path.delete!('/')
80
+ uri.path.split('_')[0]
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,16 @@
1
+ module Lumiere
2
+ module DailymotionVideoRepresenter
3
+ include Representable::JSON
4
+ include Representable::Coercion
5
+
6
+ property :video_id, as: :id, type: String
7
+ property :title
8
+ property :description
9
+ property :duration, type: Integer
10
+ property :upload_date, as: :created_time, type: DateTime
11
+ property :thumbnail_small, as: :thumbnail_60_url
12
+ property :thumbnail_medium, as: :thumbnail_240_url
13
+ property :thumbnail_large, as: :thumbnail_720_url
14
+ end
15
+
16
+ end
@@ -3,7 +3,7 @@ module Lumiere
3
3
  include Representable::JSON
4
4
  include Representable::Coercion
5
5
 
6
- property :id
6
+ property :video_id, as: :id, type: String
7
7
  property :title
8
8
  property :description
9
9
  property :duration, type: Integer
@@ -9,12 +9,13 @@ class Vimeo < Provider
9
9
  USEABLE.include?(uri.host)
10
10
  end
11
11
 
12
- def self.new_from_video_id(video_id)
13
- new("http://vimeo.com/#{video_id}")
12
+ def self.new_from_video_id(video_id, fetched=nil)
13
+ new("http://vimeo.com/#{video_id}", fetched)
14
14
  end
15
15
 
16
- def initialize(url)
16
+ def initialize(url, fetched=nil)
17
17
  @url = url
18
+ @fetched = fetched
18
19
  end
19
20
 
20
21
  def provider
@@ -65,10 +66,15 @@ class Vimeo < Provider
65
66
  fetch.thumbnail_large
66
67
  end
67
68
 
69
+ def unpack_into
70
+ struct = []
71
+ struct.extend(VimeoVideosRepresenter)
72
+ end
73
+
68
74
  private
69
75
 
70
76
  def fetch
71
- @fetch ||= [].extend(VimeoVideosRepresenter).from_json(raw_response)[0]
77
+ @fetch ||= Fetcher.new(self).remote_attributes[0]
72
78
  end
73
79
 
74
80
  def calculate_video_id
@@ -3,7 +3,7 @@ class VimeoPlaylist < Provider
3
3
  attr_accessor :url
4
4
 
5
5
  USEABLE = ['vimeo.com', 'player.vimeo.com', 'www.vimeo.com']
6
- MAX_RESULTS = 20
6
+ RESULTS_PER_REQUEST = 20
7
7
 
8
8
  def self.useable?(url)
9
9
  uri = URISchemeless.parse(url)
@@ -37,71 +37,46 @@ class VimeoPlaylist < Provider
37
37
  end
38
38
 
39
39
  def title
40
- fetch! unless @title
41
- @title
40
+ fetch.title
42
41
  end
43
42
 
44
43
  def description
45
- fetch! unless @description
46
- @description
44
+ fetch.description
47
45
  end
48
46
 
49
47
  def upload_date
50
- fetch! unless @upload_date
51
- @upload_date
48
+ fetch.upload_date
52
49
  end
53
50
 
54
51
  def thumbnail_small
55
- fetch! unless @thumbnail_small
56
- @thumbnail_small
52
+ fetch.thumbnail_small
57
53
  end
58
54
 
59
55
  def thumbnail_medium
60
- fetch! unless @thumbnail_medium
61
- @thumbnail_medium
56
+ fetch.thumbnail_medium
62
57
  end
63
58
 
64
59
  def thumbnail_large
65
- fetch! unless @thumbnail_large
66
- @thumbnail_large
60
+ fetch.thumbnail_large
67
61
  end
68
62
 
69
63
  def total_videos
70
- fetch! unless @total_videos
71
- @total_videos
64
+ fetch.total_videos
72
65
  end
73
66
 
74
- def videos
75
- return @videos if @videos
76
-
77
- @videos ||= []
78
- page_count = Playlist.page_count(total_videos, MAX_RESULTS)
79
- page_count = 3 if page_count > 3 #VIMEO CANT DEAL WITH MORE THAN 60 RESULTS ON SIMPLE API...
80
-
81
- page_count.times.with_index(1) do |times, index|
82
- @videos += fetch_videos(index)
83
- end
84
-
85
- @videos = @videos.map do |video|
86
- Vimeo.new_from_video_id(video.id)
87
- end
67
+ def unpack_into
68
+ struct = OpenStruct.new
69
+ struct.extend(VimeoPlaylistRepresenter)
88
70
  end
89
71
 
90
- private
91
-
92
- attr_writer :title, :description, :upload_date, :thumbnail_small, :thumbnail_medium, :thumbnail_large, :total_videos
93
-
94
- def fetch!
95
- self.extend(VimeoPlaylistRepresenter).from_json(raw_response)
72
+ def videos
73
+ @videos ||= VideoFetcher.new(playlist_id, total_videos).videos
96
74
  end
97
75
 
98
- def fetch_videos(page=1)
99
- [].extend(VimeoVideosRepresenter).from_json(raw_response_videos(page))
100
- end
76
+ private
101
77
 
102
- def raw_response_videos(page=1)
103
- url = "http://vimeo.com/api/v2/album/#{playlist_id}/videos.json?page=#{page}"
104
- open(url).read
78
+ def fetch
79
+ @fetch ||= Fetcher.new(self).remote_attributes
105
80
  end
106
81
 
107
82
  def calculate_playlist_id
@@ -112,5 +87,53 @@ class VimeoPlaylist < Provider
112
87
  uri.path
113
88
  end
114
89
 
90
+ class VideoFetcher
91
+ RESULTS_PER_REQUEST = 20
92
+
93
+ def initialize(playlist_id, total_videos)
94
+ @playlist_id = playlist_id
95
+ @total_videos = total_videos
96
+ @page = 1
97
+ end
98
+
99
+ def videos
100
+ videos = []
101
+ page_count.times do
102
+ videos += fetched_videos
103
+ @page += 1
104
+ end
105
+
106
+ videos.map do |video|
107
+ Vimeo.new_from_video_id(video.video_id, video)
108
+ end
109
+ end
110
+
111
+ def api_url
112
+ "http://vimeo.com/api/v2/album/#{@playlist_id}/videos.json?page=#{@page}"
113
+ end
114
+
115
+ def unpack_into
116
+ struct = []
117
+ struct.extend(VimeoVideosRepresenter)
118
+ end
119
+
120
+ private
121
+
122
+ def fetched_videos
123
+ fetch
124
+ end
125
+
126
+ def fetch
127
+ Fetcher.new(self).remote_attributes
128
+ end
129
+
130
+ def page_count
131
+ page_count = Playlist.page_count(@total_videos, RESULTS_PER_REQUEST)
132
+ #VIMEO CANT DEAL WITH MORE THAN 60 RESULTS ON SIMPLE API...
133
+ page_count = 3 if page_count > 3
134
+ page_count
135
+ end
136
+ end
137
+
115
138
  end
116
139
  end
@@ -9,12 +9,13 @@ class YouTube < Provider
9
9
  USEABLE.include?(uri.host)
10
10
  end
11
11
 
12
- def self.new_from_video_id(video_id)
12
+ def self.new_from_video_id(video_id, fetched=nil)
13
13
  new("http://www.youtube.com/watch?v=#{video_id}")
14
14
  end
15
15
 
16
- def initialize(url=nil)
16
+ def initialize(url, fetched=nil)
17
17
  @url = url
18
+ @fetched = fetched
18
19
  end
19
20
 
20
21
  def provider
@@ -38,46 +39,42 @@ class YouTube < Provider
38
39
  end
39
40
 
40
41
  def thumbnail_small
41
- fetch! unless defined?(@thumbnails)
42
- @thumbnails[0].url
42
+ fetch.thumbnails[0].url
43
43
  end
44
44
 
45
45
  def thumbnail_medium
46
- fetch! unless defined?(@thumbnails)
47
- @thumbnails[1].url
46
+ fetch.thumbnails[1].url
48
47
  end
49
48
 
50
49
  def thumbnail_large
51
- fetch! unless defined?(@thumbnails)
52
- @thumbnails[2].url
50
+ fetch.thumbnails[2].url
53
51
  end
54
52
 
55
53
  def title
56
- fetch! unless defined?(@title)
57
- @title
54
+ fetch.title
58
55
  end
59
56
 
60
57
  def description
61
- fetch! unless defined?(@description)
62
- @description
58
+ fetch.description
63
59
  end
64
60
 
65
61
  def duration
66
- fetch! unless defined?(@duration)
67
- @duration
62
+ fetch.duration
68
63
  end
69
64
 
70
65
  def upload_date
71
- fetch! unless defined?(@upload_date)
72
- @upload_date
66
+ fetch.upload_date
73
67
  end
74
68
 
75
- private
69
+ def unpack_into
70
+ struct = OpenStruct.new
71
+ struct.extend(YouTubeVideoEntryRepresenter)
72
+ end
76
73
 
77
- attr_writer :thumbnails, :video_id, :title, :description, :duration, :upload_date
74
+ private
78
75
 
79
- def fetch!
80
- self.extend(YouTubeVideoEntryRepresenter).from_json(raw_response)
76
+ def fetch
77
+ @fetch ||= Fetcher.new(self).remote_attributes
81
78
  end
82
79
 
83
80
  def calculate_video_id
@@ -93,6 +90,5 @@ class YouTube < Provider
93
90
  uri.path
94
91
  end
95
92
  end
96
-
97
93
  end
98
94
  end
@@ -26,7 +26,7 @@ module Lumiere
26
26
  end
27
27
  end
28
28
 
29
- collection :videos, as: :entry, extend: YouTubeVideoRepresenter, class: YouTube
29
+ collection :videos, as: :entry, extend: YouTubeVideoRepresenter, class: OpenStruct
30
30
  end
31
31
 
32
32
  end
@@ -3,7 +3,7 @@ class YouTubePlaylist < Provider
3
3
  attr_accessor :url
4
4
 
5
5
  USEABLE = ['www.youtube.com', 'youtube.com', 'youtu.be']
6
- MAX_RESULTS = 25
6
+ RESULTS_PER_REQUEST = 25
7
7
 
8
8
  def self.useable?(url)
9
9
  uri = URISchemeless.parse(url)
@@ -25,7 +25,7 @@ class YouTubePlaylist < Provider
25
25
 
26
26
  def api_url
27
27
  url = "http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}"
28
- url << "?max-results=#{MAX_RESULTS}"
28
+ url << "?max-results=#{RESULTS_PER_REQUEST}"
29
29
  url << "&start-index=#{@start_index}"
30
30
  url << "&v=2&alt=json"
31
31
  url
@@ -44,63 +44,61 @@ class YouTubePlaylist < Provider
44
44
  return @videos
45
45
  end
46
46
 
47
+ @videos = fetch.videos
47
48
  #take into account the first request that calling total_results will trigger through fetch!
48
49
  #todo refactor this
49
50
  remaining_pages = page_count - 1
50
-
51
51
  remaining_pages.times do
52
52
  @start_index =+ @videos.size + 1
53
- fetch!
53
+ @videos += fetch!.videos
54
+ end
55
+
56
+ @videos.map do |video|
57
+ YouTube.new_from_video_id(video.video_id, video)
54
58
  end
59
+ end
55
60
 
56
- @videos
61
+ def page_count
62
+ Playlist.page_count(total_results, RESULTS_PER_REQUEST)
57
63
  end
58
64
 
59
65
  def thumbnail_small
60
- fetch! unless @thumbnails
61
- @thumbnails[0].url
66
+ fetch.thumbnails[0].url
62
67
  end
63
68
 
64
69
  def thumbnail_medium
65
- fetch! unless @thumbnails
66
- @thumbnails[1].url
70
+ fetch.thumbnails[1].url
67
71
  end
68
72
 
69
73
  def thumbnail_large
70
- fetch! unless @thumbnails
71
- @thumbnails[2].url
74
+ fetch.thumbnails[2].url
72
75
  end
73
76
 
74
77
  def title
75
- fetch! unless @title
76
- @title
78
+ fetch.title
77
79
  end
78
80
 
79
81
  def description
80
- fetch! unless @description
81
- @description
82
+ fetch.description
82
83
  end
83
84
 
84
85
  def total_results
85
- fetch! unless @total_results
86
- @total_results
86
+ fetch.total_results
87
87
  end
88
88
 
89
- private
90
-
91
- attr_writer :thumbnails, :title, :description, :total_results
92
-
93
- def page_count
94
- Playlist.page_count(total_results, MAX_RESULTS)
89
+ def unpack_into
90
+ struct = OpenStruct.new
91
+ struct.extend(YouTubePlaylistRepresenter)
95
92
  end
96
93
 
97
- def videos=(videos)
98
- @videos ||= []
99
- @videos += videos
94
+ private
95
+
96
+ def fetch
97
+ @fetch ||= fetch!
100
98
  end
101
99
 
102
100
  def fetch!
103
- self.extend(YouTubePlaylistRepresenter).from_json(raw_response)
101
+ Fetcher.new(self).remote_attributes
104
102
  end
105
103
 
106
104
  def calculate_playlist_id
data/lib/provider.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Lumiere
2
2
  class Provider
3
3
 
4
- PROVIDERS = %w(YouTubePlaylist VimeoPlaylist YouTube Vimeo)
4
+ PROVIDERS = %w(YouTubePlaylist VimeoPlaylist YouTube Vimeo Dailymotion)
5
5
  PROVIDERS.each do |provider|
6
6
  require_relative "provider/#{provider.downcase}/#{provider.downcase}"
7
7
  require_relative "provider/#{provider.downcase}/representer"
@@ -84,11 +84,5 @@ module Lumiere
84
84
  video_id == other.video_id
85
85
  end
86
86
  end
87
-
88
- private
89
-
90
- def raw_response
91
- open(api_url).read
92
- end
93
87
  end
94
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lumiere
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Entwistle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2014-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: representable
@@ -130,10 +130,13 @@ extra_rdoc_files: []
130
130
  files:
131
131
  - LICENSE.txt
132
132
  - lib/extended_uri.rb
133
+ - lib/fetcher.rb
133
134
  - lib/lumiere.rb
134
135
  - lib/lumiere/version.rb
135
136
  - lib/playlist.rb
136
137
  - lib/provider.rb
138
+ - lib/provider/dailymotion/dailymotion.rb
139
+ - lib/provider/dailymotion/representer.rb
137
140
  - lib/provider/vimeo/representer.rb
138
141
  - lib/provider/vimeo/vimeo.rb
139
142
  - lib/provider/vimeoplaylist/representer.rb