Class: LibraryThing::Resource
- Inherits:
-
Object
- Object
- LibraryThing::Resource
- Includes:
- HTTParty
- Defined in:
- lib/librarything/resource.rb
Overview
Base class for fetching and parsing data from the LT WebServices APIs
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#item_node ⇒ Object
readonly
Returns the value of attribute item_node.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Class Method Summary collapse
-
.get(query) ⇒ Object
Accepts a hash of query params, and generates a request to the LT API.
-
.get_method(method_name = nil) ⇒ Object
Gets or sets the method name that will be passed to the LT web service.
Instance Method Summary collapse
-
#[](name) ⇒ Object
See: LibraryThing::NodeWrapper#[](name).
-
#initialize(xml) ⇒ Resource
constructor
Accepts xml from an LT API request and creates a wrapper object.
Constructor Details
#initialize(xml) ⇒ Resource
Accepts xml from an LT API request and creates a wrapper object. If there is a problem with the format of the xml, a LibraryThing::Error will be raised.
75 76 77 78 79 |
# File 'lib/librarything/resource.rb', line 75 def initialize(xml) @xml = xml @document = self.parse_response(@xml) @item_node = self.find_item_node(@document) end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
70 71 72 |
# File 'lib/librarything/resource.rb', line 70 def document @document end |
#item_node ⇒ Object (readonly)
Returns the value of attribute item_node.
71 72 73 |
# File 'lib/librarything/resource.rb', line 71 def item_node @item_node end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
69 70 71 |
# File 'lib/librarything/resource.rb', line 69 def xml @xml end |
Class Method Details
.get(query) ⇒ Object
Accepts a hash of query params, and generates a request to the LT API. Returns a wrapped response object. If there is a problem with the request, a LibraryThing::Error will be raised.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/librarything/resource.rb', line 133 def get(query) raise LibraryThing::Error.new("Missing developer key. Please define LibraryThing::DEVELOPER_KEY") unless defined?(LibraryThing::DEVELOPER_KEY) all_params = { :apikey => LibraryThing::DEVELOPER_KEY, :method => self.get_method }.merge(query) response = super('', :query => all_params) self.new(response.body) end |
.get_method(method_name = nil) ⇒ Object
Gets or sets the method name that will be passed to the LT web service. Generally, this will be specified in a sub-class.
121 122 123 124 125 126 127 128 129 |
# File 'lib/librarything/resource.rb', line 121 def get_method(method_name = nil) if method_name @get_method = method_name elsif @get_method @get_method else raise LibraryThing::Error.new('Cannot get a generic resource. Try LibraryThing::Work.get or LibraryThing::Author.get') end end |
Instance Method Details
#[](name) ⇒ Object
See: LibraryThing::NodeWrapper#[](name)
82 83 84 |
# File 'lib/librarything/resource.rb', line 82 def [](name) @item_node[name] end |