WpUser::Existable specs

This commit is contained in:
erwanlr
2013-03-27 16:52:32 +01:00
parent 52c1bd3558
commit ec9eadda8e
6 changed files with 225 additions and 150 deletions

View File

@@ -3,13 +3,22 @@
class WpUser < WpItem
module Existable
# @param [ Typhoeus::Response ] response
# @param [ Hash ] options
#
# @return [ Boolean ]
def exists_from_response?(response, options = {})
load_login_from_response(response)
load_from_response(response)
@login ? true : false
end
def load_login_from_response(response)
# Load the login and display_name from the response
#
# @param [ Typhoeus::Response ] response
#
# @return [ void ]
def load_from_response(response)
if response.code == 301 # login in location?
location = response.headers_hash['Location']
@@ -22,11 +31,18 @@ class WpUser < WpItem
@display_name = WpUser::Existable.display_name_from_body(response.body)
end
end
private :load_from_response
# @param [ String ] text
#
# @return [ String ] The login
def self.login_from_author_pattern(text)
text[%r{/author/([^/\b]+)/?}i, 1]
end
# @param [ String ] body
#
# @return [ String ] The login
def self.login_from_body(body)
# Feed URL with Permalinks
login = WpUser::Existable.login_from_author_pattern(body)
@@ -39,11 +55,16 @@ class WpUser < WpItem
login
end
# @param [ String ] body
#
# @return [ String ] The display_name
def self.display_name_from_body(body)
if title_tag = body[%r{<title>([^<]+)</title>}i, 1]
title_tag.sub!('&#124;', '|')
title_tag = Nokogiri::HTML::DocumentFragment.parse(title_tag).to_s
# &amp; are not decoded with Nokogiri
title_tag.sub!('&amp;', '&')
return title_tag[%r{([^|]+) }, 1]
return title_tag[%r{([^|«]+) }, 1]
end
end