Class: OCLC::Classify::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/oclc_classify.rb

Constant Summary collapse

XPATH =
{
	:code            => '//response/@code',
	:author          => '//work/@author',
	:title           => '//work/@title',
	:recommendations => '//recommendations',
}
KEYS =
{
	:popular => 'mostpopular',
	:recent  => 'mostrecent',
	:latest  => 'latestedition',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Response

Returns a new instance of Response.



95
96
97
98
99
100
# File 'lib/oclc_classify.rb', line 95

def initialize(url)
	@response = Nokogiri::HTML(open(url))
	@code     = @response.xpath(XPATH[:code]).to_s
	@author   = @response.xpath(XPATH[:author]).to_s
	@title    = @response.xpath(XPATH[:title]).to_s
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



80
81
82
# File 'lib/oclc_classify.rb', line 80

def author
  @author
end

#codeObject (readonly)

Returns the value of attribute code.



80
81
82
# File 'lib/oclc_classify.rb', line 80

def code
  @code
end

#responseObject (readonly)

Returns the value of attribute response.



80
81
82
# File 'lib/oclc_classify.rb', line 80

def response
  @response
end

#titleObject (readonly)

Returns the value of attribute title.



80
81
82
# File 'lib/oclc_classify.rb', line 80

def title
  @title
end

Instance Method Details

#recommendations(scheme, key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/oclc_classify.rb', line 102

def recommendations(scheme, key)
	raise "Invalid scheme: must be 'ddc' or 'lcc'" unless scheme =~ /(dd|lc)c/
	raise "Invalid key: " + key.to_s unless KEYS.keys.include? key 
	path = '/' + scheme + '/' + KEYS[key]
	if @code == '0' or @code == '2'
		results = OCLC::Classify::Recommendations.new(@response.xpath(XPATH[:recommendations] + path))
		results.process
		return results.recommendations
	else
		return []
	end
end