Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
add "nibbler/json" as experimental JSON nibbler
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 15, 2010
1 parent ae8abd5 commit 9dfdebc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
38 changes: 6 additions & 32 deletions examples/twitter.rb
Expand Up @@ -5,43 +5,17 @@
# #
# Requirements: a JSON library (tested with "json" gem) # Requirements: a JSON library (tested with "json" gem)


require 'scraper' require 'nibbler/json'
require 'json' require 'json'
require 'time' require 'time'


# a wrapper for JSON data that provides `at` and `search`
class JsonDocument
def initialize(obj)
@data = String === obj ? JSON.parse(obj) : obj
end

def self.[](obj)
self.class === obj ? obj : new(obj)
end

def search(selector)
@data.to_a
end

def at(selector)
@data[selector]
end
end

# a scraper that works with JsonDocument
class JsonScraper < Scraper
def self.convert_document(doc)
JsonDocument[doc]
end
end

# now here's the real deal # now here's the real deal
class Twitter < JsonScraper class Twitter < NibblerJSON
elements :tweets, :with => JsonScraper do elements :tweets, :with => NibblerJSON do
element :created_at element :created_at, :with => lambda { |time| Time.parse(time) }
element :text element :text
element :id element :id
element 'user' => :author, :with => JsonScraper do element 'user' => :author, :with => NibblerJSON do
element 'name' => :full_name element 'name' => :full_name
element 'screen_name' => :username element 'screen_name' => :username
end end
Expand All @@ -51,7 +25,7 @@ class Twitter < JsonScraper
twitter = Twitter.parse(DATA.read) twitter = Twitter.parse(DATA.read)


twitter.tweets.each do |tweet| twitter.tweets.each do |tweet|
puts "@%s: %s" % [tweet.author.username, tweet.text] puts "@%s: %s" % [tweet.author.username, tweet.created_at.inspect]
puts puts
end end


Expand Down
27 changes: 27 additions & 0 deletions lib/nibbler/json.rb
@@ -0,0 +1,27 @@
require 'nibbler'

# a wrapper for JSON data that provides `at` and `search`
class Nibbler::JsonDocument
def initialize(obj)
@data = String === obj ? JSON.parse(obj) : obj
end

def self.[](obj)
self.class === obj ? obj : new(obj)
end

def search(selector)
@data.to_a
end

def at(selector)
@data[selector]
end
end

# a scraper that works with JsonDocument
class NibblerJSON < Nibbler
def self.convert_document(doc)
Nibbler::JsonDocument[doc]
end
end

0 comments on commit 9dfdebc

Please sign in to comment.