lib/nicoquery/object_mapper/getthumbinfo.rb in nicoquery-0.1.8 vs lib/nicoquery/object_mapper/getthumbinfo.rb in nicoquery-0.1.8.2

- old
+ new

@@ -7,11 +7,15 @@ class GetThumbInfo def initialize(xml) @xml = xml @parser = Nori.new @parsed_xml = @parser.parse xml + @hash = @parsed_xml['nicovideo_thumb_response']['thumb'] + if !exist? + @hash = @parser.parse null_xml + end end def deleted? if @parsed_xml['nicovideo_thumb_response']["error"].presence @parsed_xml['nicovideo_thumb_response']["error"].presence["code"].presence == "DELETED" @@ -30,11 +34,11 @@ def exist? if @parsed_xml['nicovideo_thumb_response']["error"].presence @parsed_xml['nicovideo_thumb_response']["error"].presence["code"].presence != "NOT_FOUND" else - false + true end end def video_id return nil if @hash == nil @@ -84,11 +88,11 @@ return nil if @hash == nil @hash['size_low'].to_i end def view_counter - return nil if @hash == nil + return nil if @hash.nil? || @hash['view_counter'].nil? @hash['view_counter'].to_i end def comment_num return nil if @hash == nil @@ -129,11 +133,11 @@ return nil if @hash == nil @hash['no_live_play'] == 1 end def tags - return nil if @hash == nil + return nil if @hash.nil? || @hash['tags'].nil? xml = @xml.scan(/\<tags domain=\"jp\">\n.+\n\<\/tags\>/m)[0] parsed = Nokogiri::XML xml parsed.xpath("//tag").map do |tag_object| generate_tag_hash_by tag_object end @@ -153,9 +157,44 @@ else false end { text: text, lock: lock } + end + + def null_xml + <<-EOS + <video_id></video_id> + <title></title> + <description></description> + <thumbnail_url></thumbnail_url> + <first_retrieve></first_retrieve> + <length></length> + <movie_type></movie_type> + <size_high></size_high> + <size_low></size_low> + <view_counter></view_counter> + <comment_num></comment_num> + <mylist_counter></mylist_counter> + <last_res_body></last_res_body> + <watch_url></watch_url> + <thumb_type></thumb_type> + <embeddable></embeddable> + <no_live_play></no_live_play> + <tags domain="jp"> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + <tag></tag> + </tags> + <user_id></user_id> + EOS end end end end \ No newline at end of file