dm-ys

a DataMapper extension that uses html table as its schema and data powerfully like YunkerStar

Example

class Maintainer
  include DataMapper::YS
  uri "http://datamapper.org/doku.php?id=gem_maintainers"
end

irb(main):001:0> Maintainer.count
=> 31

irb(main):002:0> Maintainer.first
=> #<Maintainer id=nil _Gem="data_objects" _Maintainer="dbussink" Name="Dirkjan Bussink">

irb(main):003:0> Maintainer.names
=> ["_Gem", "_Maintainer", "Name"]

irb(main):004:0> Maintainer.labels
=> ["Gem", "Maintainer", "Name"]

Anonymous class

is a syntax sugar for generating anonymous class

Wota = DataMapper::YS["http://wota.jp"]

is equivalent to

class Wota
  include DataMapper::YS
  uri "http://wota.jp"
end

Pagination

Special syntax “URI*” means pagination mode that enables recursive retrieving.

class Plugin
  include DataMapper::YS
  uri "http://merbi.st/plugins/index?page=1"
end

irb(main):001:0> Plugin.count
=> 20

This parses only specified uri. Append “*” to uri if you want pagination mode.

class Plugin
  include DataMapper::YS
  uri "http://merbi.st/plugins/index?page=1*"
end

irb(main):001:0> Plugin.count
=> 36

Raw Element

“element_for” method returns raw scraped elements those are currently hpricot elements.

>> Plugin.names               #=> ["Name", "Repos", "Registeredby", "Description", "col_1"]
>> record = Plugin.first      #=> #<Plugin id=1 Name="eventmachine-0.12.5" Repos=...>
>> record.Name                #=> "eventmachine-0.12.5"
>> record.element_for("Name") #=> {elem td {elem a href"/plugins/36" "eventmachine-0.12.5" a} td}
>> record.element_for("Name").class #=> Hpricot::Elem

“link_for” is syntax sugar for extracting href tag from its element.

>> record.link_for("Name")         #=> "http://merbi.st/plugins/36"
>> record.link_for("Registeredby") #=> "http://merbi.st/users/1"

Furthermore, :only_path option can control the fully qualified URL or not.

>> Plugin.ys[:only_path] = true
>> record.link_for("Name")         #=> "/plugins/36"
>> record.link_for("Registeredby") #=> "/users/1"

TODO

  • Feel free to request what you want! :)

Copyright © 2008 [email protected], released under the MIT license