Class: DeziResponse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_resp) ⇒ DeziResponse

Returns a new instance of DeziResponse.



35
36
37
38
39
40
41
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dezi/response.rb', line 35

def initialize(http_resp)
    @http_resp = http_resp
    
    #warn http_resp.headers.inspect
    #warn "code=" + http_resp.status.to_s
    
    @is_ok = false
    if (http_resp.status.to_s =~ /^2\d\d/)
        @is_ok = true
    end
    
    if (!@is_ok)
        return
    end
    
    #warn "is_ok=#{@is_ok}"
    
    body = http_resp.body
    
    #warn body.inspect
    
    # set body keys as attributes in the object
    body.each {|k,v|
    
        # results are special
        if k == 'results'
            next
        end
    
        # create the attribute
        self.instance_eval { class << self; self end }.send(:attr_accessor, k)
        
        # assign the value
        send("#{k}=",v)
    }
    
    if body['results'].nil?
        @results = []
        return
    end

    #warn 'body[results] is not nil'
    
    # make each result Hash into a DeziDoc object
    @results = body['results'].map {|r|
        rhash = r.to_hash
        res_fields = rhash.keys
        result = { 'fields' => {} }
        res_fields.each {|f|
            result['fields'][f] = rhash.delete(f)
            if (DeziDoc.method_defined? f)
                result[f] = result['fields'][f]
            end
        }
        DeziDoc.new(result)
    }

end

Instance Attribute Details

#resultsObject

most attributes are assigned dynamically in initialize(). Try:

puts response.inspect

to see them.



33
34
35
# File 'lib/dezi/response.rb', line 33

def results
  @results
end

Instance Method Details

#is_successObject



98
99
100
# File 'lib/dezi/response.rb', line 98

def is_success()
    return @is_ok
end

#statusObject



94
95
96
# File 'lib/dezi/response.rb', line 94

def status()
    return @http_resp.status
end