Sha256: 3d86426366b08b210e41c0d4361aaa2d9820070ed99eb15304a906937df5fba9

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require File.dirname(__FILE__) + "/../ext/cparsley"
require "rubygems"
require "json"
require "thread"

class Parsley
  
  def self.user_agent=(agent)
    @user_agent = agent
    CParsley.set_user_agent(agent.to_s)
  end
  
  def self.user_agent
    @user_agent
  end
  
  def initialize(parsley, incl = "")
    if(parsley.is_a?(Hash))
      parsley = parsley.to_json 
    end
    @@mutex ||= Mutex.new
    @@mutex.synchronize do
      @parsley = CParsley.new(parsley, incl)
    end
  end
  
  # Valid options:
  #
  # Requires one of:
  # :file -- the input file path or url
  # :string -- the input string
  #
  # And optionally (default is the first listed value):
  # :input => [:html, :xml]
  # :output => [:ruby, :json, :xml]
  # :prune => [true, false]
  # :collate => [true, false]
  # :base => "http://some/base/href"
  # :allow_net => [true, false]
  # :allow_local => [true, false]
  def parse(options = {})
    options[:file] || options[:string] || (raise ParsleyError.new("must specify what to parse"))
    
    options[:is_file] = !!options[:file]
    options[:has_base] = !!options[:base]
    
    options[:base] = options[:base].to_s
    options[:file] = options[:file].to_s
    options[:string] = options[:string].to_s
    
    options[:input]  ||= :html
    options[:output] ||= :ruby
    
    options[:collate] = true unless options.has_key?(:collate)
    options[:prune] = true unless options.has_key?(:prune)
    options[:allow_net] = true unless options.has_key?(:allow_net)
    options[:allow_local] = true unless options.has_key?(:allow_local)
    
    options[:collate] = !!options[:collate]
    options[:prune] = !!options[:prune]
    options[:allow_net] = !!options[:allow_net]
    options[:allow_local] = !!options[:allow_local]
    
    @parsley.parse(options)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fizx-parsley-ruby-0.3.1 lib/parsley.rb