undees / rforce (http://rforce.rubyforge.org)

A simple, usable binding to the SalesForce API.

Clone this repository (size: 109.4 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/undees/rforce/

Changed (Δ276 bytes):

raw changeset »

lib/rforce/method_keys.rb (1 lines added, 0 lines removed)

lib/rforce/soap_pullable.rb (1 lines added, 1 lines removed)

lib/rforce/soap_response.rb (7 lines added, 8 lines removed)

lib/rforce/soap_response_expat.rb (7 lines added, 7 lines removed)

lib/rforce/soap_response_hpricot.rb (13 lines added, 17 lines removed)

spec/rforce_spec.rb (1 lines added, 1 lines removed)

tasks/timing.rake (1 lines added, 1 lines removed)

Up to file-list lib/rforce/method_keys.rb:

@@ -3,6 +3,7 @@ module RForce
3
3
  # to supplement the traditional way of indexing: hash[key]
4
4
  module MethodKeys
5
5
    def method_missing(method, *args)
6
      raise NoMethodError unless respond_to?('[]')
6
7
      self[method]
7
8
    end
8
9
  end

Up to file-list lib/rforce/soap_pullable.rb:

@@ -19,7 +19,7 @@ module RForce
19
19
        return
20
20
      end
21
21
            
22
      @stack.push({})
22
      @stack.push(MethodHash.new)
23
23
    end
24
24
    
25
25
    def text(data)

Up to file-list lib/rforce/soap_response.rb:

@@ -15,21 +15,20 @@ module RForce
15
15
      define_method(unused) {}
16
16
    end
17
17
18
    def initialize(content)
19
      @content = content
20
    end
21
    
18
22
    # Parses an XML string into structured data.
19
    def initialize(content)
23
    def parse
20
24
      @current_value = nil
21
25
      @stack = []
22
26
      @parsed = {}
23
27
      @done = false
24
28
      @namespaces = []
25
29
26
      REXML::Document.parse_stream content, self
27
    end
28
29
    # Allows this object to act like a hash (and therefore
30
    # via MethodKeys from the include above).
31
    def [](symbol)
32
      @parsed[symbol]
30
      REXML::Document.parse_stream @content, self
31
      @parsed
33
32
    end
34
33
  end
35
34
end

Up to file-list lib/rforce/soap_response_expat.rb:

@@ -7,13 +7,17 @@ module RForce
7
7
    include SoapPullable
8
8
9
9
    def initialize(content)
10
      @content = content
11
    end
12
    
13
    def parse   
10
14
      @current_value = nil
11
15
      @stack = []
12
16
      @parsed = {}
13
17
      @done = false
14
18
      @namespaces = []
15
      
16
      XML::Parser.new.parse(content) do |type, name, data|
19
20
      XML::Parser.new.parse(@content) do |type, name, data|
17
21
        case type
18
22
        when XML::Parser::START_ELEM
19
23
          tag_start name, data
@@ -26,11 +30,7 @@ module RForce
26
30
        break if @done
27
31
      end
28
32
29
      self
30
    end
31
32
    def [](key)
33
      @parsed[key.to_sym]
33
      @parsed
34
34
    end
35
35
  end
36
36
end

Up to file-list lib/rforce/soap_response_hpricot.rb:

@@ -3,24 +3,21 @@ require 'hpricot'
3
3
4
4
module RForce
5
5
  class SoapResponseHpricot
6
    include MethodKeys
7
8
6
    # Parses an XML string into structured data.
9
7
    def initialize(content)
10
      document = Hpricot.XML(content)
11
      node = document%'soapenv:Body'
12
      
13
      @parsed = SoapResponseHpricot.parse node
14
    end
15
16
    # Allows this object to act like a hash (and therefore
17
    # via MethodKeys via the include above).
18
    def [](symbol)
19
      @parsed[symbol]
8
      @content = content
20
9
    end
21
10
22
11
    # Digests an XML DOM node into nested Ruby types.
23
    def SoapResponseHpricot.parse(node)
12
    def parse
13
      document = Hpricot.XML(@content)
14
      node = document % 'soapenv:Body'
15
      self.class.node_to_ruby node
16
    end
17
    
18
    private
19
    
20
    def self.node_to_ruby(node)
24
21
      # Convert text nodes into simple strings.
25
22
      children = node.children.reject do |c|
26
23
        c.is_a?(Hpricot::Text) && c.to_s.strip.empty?
@@ -50,20 +47,20 @@ module RForce
50
47
51
48
        case elements[name]
52
49
          # The most common case: unique child element tags.
53
        when NilClass: elements[name] = parse(e)
50
        when NilClass: elements[name] = node_to_ruby(e)
54
51
55
52
          # Non-unique child elements become arrays:
56
53
57
54
          # We've already created the array: just
58
55
          # add the element.
59
        when Array: elements[name] << parse(e)
56
        when Array: elements[name] << node_to_ruby(e)
60
57
61
58
          # We haven't created the array yet: do so,
62
59
          # then put the existing element in, followed
63
60
          # by the new one.
64
61
        else
65
62
          elements[name] = [elements[name]]
66
          elements[name] << parse(e)
63
          elements[name] << node_to_ruby(e)
67
64
        end
68
65
      end
69
66
@@ -71,4 +68,3 @@ module RForce
71
68
    end
72
69
  end
73
70
end
74

Up to file-list spec/rforce_spec.rb:

@@ -52,7 +52,7 @@ describe SoapResponse do
52
52
53
53
    @rexml_recs, @expat_recs, @hpricot_recs =
54
54
        [SoapResponse, SoapResponseExpat, SoapResponseHpricot].map do |klass|
55
      results = klass.new(@contents)
55
      results = klass.new(@contents).parse
56
56
      results[:queryResponse][:result][:records]
57
57
    end
58
58
  end

Up to file-list tasks/timing.rake:

@@ -9,7 +9,7 @@ task :timing do
9
9
   RForce::SoapResponseExpat,
10
10
   RForce::SoapResponseHpricot].each do |klass|
11
11
    started_at = Time.now
12
    results = klass.new(contents)
12
    klass.new(contents).parse
13
13
    elapsed = Time.now - started_at
14
14
    puts "#{klass}: #{elapsed}"
15
15
  end