Class: LibraryThing::Resource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/librarything/resource.rb

Overview

Base class for fetching and parsing data from the LT WebServices APIs

Direct Known Subclasses

Author, Work

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#documentObject (readonly)

Returns the value of attribute document.



70
71
72
# File 'lib/librarything/resource.rb', line 70

def document
  @document
end

#item_nodeObject (readonly)

Returns the value of attribute item_node.



71
72
73
# File 'lib/librarything/resource.rb', line 71

def item_node
  @item_node
end

#xmlObject (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