lib/nicoquery/object/movie.rb in nicoquery-0.1.6.1 vs lib/nicoquery/object/movie.rb in nicoquery-0.1.7
- old
+ new
@@ -6,12 +6,10 @@
module Object
class Movie
attr_reader :video_id
[
- 'deleted?',
-
'title',
'url',
'thread_id',
'view_counter',
'comment_num',
@@ -19,19 +17,15 @@
'publish_date',
'description',
].each do |field_name|
define_method(field_name) do
source =
- @source['mylist_rss'].presence ||
- @source['tag_search_rss'].presence ||
- @source['video_array'].presence ||
- @source['gethumbinfo'].presence ||
- Proc.new do
- source = (NicoQuery::Api::GetThumbInfo.new(@video_id || @thread_id)).get
- set_getthumbinfo_source(NicoQuery::ObjectMapper::GetThumbInfo.new source)
- end.call
-
+ @source[:mylist_rss].presence ||
+ @source[:tag_search_rss].presence ||
+ @source[:video_array].presence ||
+ @source[:getthumbinfo].presence ||
+ Proc.new { get_and_set_getthumbinfo_source; @source[:getthumbinfo] }.call
source.send field_name
end
end
[
@@ -45,43 +39,78 @@
# 'user_id',
'tags',
].each do |field_name|
define_method(field_name) do
source =
- @source['gethumbinfo'].presence ||
- @source['video_array'].presence ||
- Proc.new do
- source = (NicoQuery::Api::GetThumbInfo.new(@video_id || @thread_id)).get
- set_getthumbinfo_source(NicoQuery::ObjectMapper::GetThumbInfo.new source)
- end.call
+ @source[:getthumbinfo].presence ||
+ @source[:video_array].presence ||
+ Proc.new { get_and_set_getthumbinfo_source; @source[:getthumbinfo] }.call
source.send field_name
end
end
- def initialize(video_id_of_thread_id)
+ def initialize(video_id_or_thread_id)
@source = {}
- if video_id_of_thread_id.to_s.match(/sm|nm/)
- @video_id = video_id_of_thread_id
+ @response = {}
+
+ if video_id_or_thread_id.to_s.match(/sm|nm/)
+ @video_id = video_id_or_thread_id
else
- @thread_id = video_id_of_thread_id
+ @thread_id = video_id_or_thread_id
end
end
+ def available?
+ [exist?, !deleted?].all?
+ end
+
+ def community?
+ unless @source[:video_array].present?
+ get_and_set_video_array_source
+ end
+ @source[:video_array].community?
+ end
+
+ def deleted?
+ unless @source[:getthumbinfo].present?
+ get_and_set_getthumbinfo_source
+ end
+ @source[:getthumbinfo].deleted?
+ end
+
+ def exist?
+ unless @source[:getthumbinfo].present?
+ get_and_set_getthumbinfo_source
+ end
+ @source[:getthumbinfo].exist?
+ end
+
def set_getthumbinfo_source(source_object)
- @source['getthumbinfo'] ||= source_object
+ @source[:getthumbinfo] ||= source_object
end
def set_mylist_rss_source(source_object)
- @source['mylist_rss'] ||= source_object
+ @source[:mylist_rss] ||= source_object
end
def set_tag_search_rss_source(source_object)
- @source['tag_search_rss'] ||= source_object
+ @source[:tag_search_rss] ||= source_object
end
def set_video_array_source(source_object)
- @source['video_array'] ||= source_object
+ @source[:video_array] ||= source_object
+ end
+
+ def get_and_set_getthumbinfo_source
+ @response[:getthumbinfo] = (NicoQuery::Api::GetThumbInfo.new(@video_id || @thread_id)).get
+ set_getthumbinfo_source(NicoQuery::ObjectMapper::GetThumbInfo.new @response[:getthumbinfo][:body])
+ end
+
+ def get_and_set_video_array_source
+ @response[:video_array] = (NicoQuery::Api::VideoArray.new([@video_id || @thread_id])).get
+ parsed = (NicoQuery::ObjectMapper::VideoArray.new @response[:video_array][:body])
+ set_video_array_source parsed.movies[0]
end
end
end
end