Class: Parsley

Inherits:
Object
  • Object
show all
Defined in:
lib/parsley.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsley, incl = "") ⇒ Parsley

Returns a new instance of Parsley.



17
18
19
20
21
22
23
24
25
# File 'lib/parsley.rb', line 17

def initialize(parsley, incl = "")
  if(parsley.is_a?(Hash))
    parsley = recursive_stringify(parsley).to_json 
  end
  @@mutex ||= Mutex.new
  @@mutex.synchronize do
    @parsley = CParsley.new(parsley, incl)
  end
end

Class Method Details

.user_agentObject



13
14
15
# File 'lib/parsley.rb', line 13

def self.user_agent
  @user_agent
end

.user_agent=(agent) ⇒ Object



8
9
10
11
# File 'lib/parsley.rb', line 8

def self.user_agent=(agent)
  @user_agent = agent
  CParsley.set_user_agent(agent.to_s)
end

Instance Method Details

#parse(options = {}) ⇒ Object

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] :sgwrap => [false, true] :collate => [true, false] :base => “some/base/href” :allow_net => [true, false] :allow_local => [true, false]



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/parsley.rb', line 42

def parse(options = {})
  options[:file] || options[:string] || (raise ParsleyError.new("must specify what to parse"))
  
  options[:sgwrap] = !!options[:sgwrap]
  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