Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
One project,three gems    Mike Williams  Cogent Consulting   in association with
Architecture“Atlas”            <stuff/>
arborealGrowing Trees    (restaurant)                                                    (hotel)           Melbourne      ...
arborealhow do I select this?                       Melbourne                                               Sydney        ...
arborealPath Enumeration@victoria = Place.find_by_name(“Victoria”)@victoria.ancestry_string#=> “-4-14-15-”                 ...
arboreal@victoria.ancestorsSELECT * FROM places WHERE (id in (4,14,15))                          Melbourne                ...
arboreal@australia = Place.find_by_name(“Australia”)@australia.childrenSELECT * FROM places WHERE (places.parent_id = 15)  ...
arboreal@australia.descendantsSELECT * FROM places WHERE (places.ancestry_string like ‘-4-14-15-%’)     “-4-14-15-22-”    ...
arboreal@australia.subtreeSELECT * FROM places WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’)     “-4-...
arboreal@australia.subtree.scope(:find, :conditions)#=> [“places.id = ? OR places.ancestry_string like ?”, 15, “-4-14-15-%”...
arboreal@australia.contained_poisSELECT ... FROM pois LEFT OUTER JOIN places ON places.id = pois.place_id WHERE (places.id...
GET http://stuff.local/places/15/pois “Atlas”                      <pois/>
<pois type="array">  <poi>    <created-at type="datetime">2010-08-25T23:35:59Z</created-at>    <email nil="true"></email> ...
<pois type="array">  <poi href="http://stuff.local/pois/6">    <name>Footscray Market</name>    <type>See</type>    <revie...
# BEFORE ...xml.pois :type => “array” do @pois.each do |poi|  xml.poi :href => poi_url(poi) do   xml.name(poi.name)   xml....
# BEFORE ...                                                             representativexml.pois :type => “array” do @pois....
# XML ...                                                                 representativeRepresentative::Xml.new(xml) do |r...
<pois type="array">  <poi href="http://stuff.local/pois/6">    <name>Footscray Market</name>    <type>See</type>    <revie...
representative[    {        "name": "Footscray Market",        "type": "See",        "review": "A frenetic covered food ma...
“Atlas”          <pois/>
<pois/>   something          that wants           POI data
<pois/>   somethingfake                  that wantsAtlas                   POI data
sham                                                                                    rackrequire ‘fakeweb’FakeWeb.regis...
sham                             rackIt’s just Rack!client   Net::HTTP         Sham        Rack         Rack        app
sham                                            rackIt’s just Rack!ShamRack.at("www.example.com”) do |env|  [     "200 OK"...
sham                                             rackIt’s just Rack!ShamRack.at("www.example.com").sinatra do  get "/hello...
http://github.com/mdub/arborealhttp://github.com/mdub/representativehttp://github.com/mdub/sham_rack
Upcoming SlideShare
Loading in …5
×

One project, 3 gems

1,271 views

Published on

Three handy Ruby gems inspired by the same project at Lonely Planet.

Published in: Technology, Travel, Business
  • Be the first to comment

  • Be the first to like this

One project, 3 gems

  1. 1. One project,three gems Mike Williams Cogent Consulting in association with
  2. 2. Architecture“Atlas” <stuff/>
  3. 3. arborealGrowing Trees (restaurant) (hotel) Melbourne Sydney Geelong Victoria NSW (sight) WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  4. 4. arborealhow do I select this? Melbourne Sydney Geelong Victoria NSW WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  5. 5. arborealPath Enumeration@victoria = Place.find_by_name(“Victoria”)@victoria.ancestry_string#=> “-4-14-15-” Melbourne Sydney Geelong Victoria NSW Australia 15 WA New Zealand Pacific 14 Europe WORLD 4 Asia Americas Africa
  6. 6. arboreal@victoria.ancestorsSELECT * FROM places WHERE (id in (4,14,15)) Melbourne Sydney Geelong Victoria NSW Australia 15 WA New Zealand Pacific 14 Europe WORLD 4 Asia Americas Africa
  7. 7. arboreal@australia = Place.find_by_name(“Australia”)@australia.childrenSELECT * FROM places WHERE (places.parent_id = 15) Melbourne Sydney Geelong Victoria NSW Australia 15 WA New Zealand Pacific Europe Asia WORLD Americas Africa
  8. 8. arboreal@australia.descendantsSELECT * FROM places WHERE (places.ancestry_string like ‘-4-14-15-%’) “-4-14-15-22-” Melbourne Sydney “-4-14-15-16-” Geelong Victoria NSW “-4-14-15-” WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  9. 9. arboreal@australia.subtreeSELECT * FROM places WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’) “-4-14-15-22-” Melbourne Sydney “-4-14-15-16-” Geelong Victoria NSW “-4-14-15-” Australia 15 WA New Zealand Pacific Europe Asia WORLD Americas Africa
  10. 10. arboreal@australia.subtree.scope(:find, :conditions)#=> [“places.id = ? OR places.ancestry_string like ?”, 15, “-4-14-15-%”]class Place def contained_pois Poi.scoped(:include => :place, :conditions => subtree.scope(:find, :conditions)) endend@australia.contained_poisSELECT ... FROM pois LEFT OUTER JOIN places ON places.id = pois.place_id WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’)
  11. 11. arboreal@australia.contained_poisSELECT ... FROM pois LEFT OUTER JOIN places ON places.id = pois.place_id WHERE (places.id = 15 OR places.ancestry_string like ‘-4-14-15-%’) Melbourne Sydney Geelong Victoria NSW WA Australia New Zealand Pacific Europe Asia WORLD Americas Africa
  12. 12. GET http://stuff.local/places/15/pois “Atlas” <pois/>
  13. 13. <pois type="array"> <poi> <created-at type="datetime">2010-08-25T23:35:59Z</created-at> <email nil="true"></email> <id type="integer">6</id> <latitude type="decimal">-37.8002389992226</latitude> <longitude type="decimal">144.901535511017</longitude> <name>Footscray Market</name> <place-id type="integer">26</place-id> <review>A frenetic covered food market.</review> <type>See</type> <updated-at type="datetime">2010-08-25T23:35:59Z</updated-at> <url nil="true"></url> </poi> <poi> <created-at type="datetime">2010-08-25T23:35:59Z</created-at> <email nil="true"></email> <id type="integer">7</id> <latitude type="decimal">-37.8020224141169</latitude> <longitude type="decimal">144.983611106873</longitude> <name>Panama Dining Room</name> <place-id type="integer">26</place-id> <review>Franco-Fitzroy pub grub and ersatz Manhattan views.</review> <type>Eat</type> <updated-at type="datetime">2010-08-25T23:35:59Z</updated-at> <url nil="true"></url> </poi> ...
  14. 14. <pois type="array"> <poi href="http://stuff.local/pois/6"> <name>Footscray Market</name> <type>See</type> <review>A frenetic covered food market.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/7"> <name>Panama Dining Room</name> <type>Eat</type> <review>Franco-Fitzroy pub grub and ersatz Manhattan views.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/8"> <name>Sydney Opera House</name> <type>See</type> <review/> <place href="http://stuff.local/places/17"> <name>Sydney</name> </place> </poi></pois>
  15. 15. # BEFORE ...xml.pois :type => “array” do @pois.each do |poi| xml.poi :href => poi_url(poi) do xml.name(poi.name) xml.type(poi.type) xml.review(poi.review) xml.place :href => place_url(poi.place) do xml.name poi.place.name end end endend
  16. 16. # BEFORE ... representativexml.pois :type => “array” do @pois.each do |poi| xml.poi :href => poi_url(poi) do xml.name(poi.name) xml.type(poi.type) xml.review(poi.review) xml.place :href => place_url(poi.place) do xml.name poi.place.name end end endend# AFTER ...Representative::Xml.new(xml) do |r| r.list_of :pois, @pois, :item_attributes => {:href => method(:poi_url)} do r.element :name r.element :type r.element :review r.element :place, :href => method(:place_url) do r.element :name end endend
  17. 17. # XML ... representativeRepresentative::Xml.new(xml) do |r| r.list_of :pois, @pois, :item_attributes => {:href => method(:poi_url)} do r.element :name r.element :type r.element :review r.element :place, :href => method(:place_url) do r.element :name end endend# JSON ...Representative::Json.new do |r| r.list_of :pois, @pois, :item_attributes => {:href => method(:poi_url)} do r.element :name r.element :type r.element :review r.element :place, :href => method(:place_url) do r.element :name end endend.to_s
  18. 18. <pois type="array"> <poi href="http://stuff.local/pois/6"> <name>Footscray Market</name> <type>See</type> <review>A frenetic covered food market.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/7"> <name>Panama Dining Room</name> <type>Eat</type> <review>Franco-Fitzroy pub grub and ersatz Manhattan views.</review> <place href="http://stuff.local/places/26"> <name>Melbourne</name> </place> </poi> <poi href="http://stuff.local/pois/8"> <name>Sydney Opera House</name> <type>See</type> <review/> <place href="http://stuff.local/places/17"> <name>Sydney</name> </place> </poi></pois>
  19. 19. representative[ { "name": "Footscray Market", "type": "See", "review": "A frenetic covered food market.", "place": { "name": "Melbourne" } }, { "name": "Panama Dining Room", "type": "Eat", "review": "Franco-Fitzroy pub grub and ersatz Manhattan views.", "place": { "name": "Melbourne" } }, { "name": "Sydney Opera House", "type": "See", "review": null, "place": { "name": "Sydney" } }]
  20. 20. “Atlas” <pois/>
  21. 21. <pois/> something that wants POI data
  22. 22. <pois/> somethingfake that wantsAtlas POI data
  23. 23. sham rackrequire ‘fakeweb’FakeWeb.register_uri(:get, “http://www.example.com/hello”, :body => “Hello World!”)require ‘webmock’stub_request(:get, “www.example.com/hello”).to_return(:body => “Hello, World!”)require ‘sham_rack’ShamRack.at(“www.example.com”).stub.register_resource(“/hello”, “Hello, World!”)
  24. 24. sham rackIt’s just Rack!client Net::HTTP Sham Rack Rack app
  25. 25. sham rackIt’s just Rack!ShamRack.at("www.example.com”) do |env| [ "200 OK", { "Content-type" => "text/plain" }, "Hello, world!" ]endShamRack.at("www.example.com").rackup do use Some::Middleware use Some::Other::Middleware run MyApp.newend
  26. 26. sham rackIt’s just Rack!ShamRack.at("www.example.com").sinatra do get "/hello/:subject" do "Hello, #{params[:subject]}" endend
  27. 27. http://github.com/mdub/arborealhttp://github.com/mdub/representativehttp://github.com/mdub/sham_rack

×