Class: Linkscape::Response::ResponseData

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/linkscape/response.rb

Defined Under Namespace

Classes: Flags

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, type = nil) ⇒ ResponseData

Returns a new instance of ResponseData.



26
27
28
29
30
31
32
33
34
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
# File 'lib/linkscape/response.rb', line 26

def initialize(data, type=nil)
  @data = data
  @type = if type
    type.to_sym
  elsif Hash === @data
    :hash
  elsif Array === @data
    :array
  end
  
  @data.symbolize_keys! if Hash === @data
  @data = @data.collect{|d|ResponseData.new(d)} if Array === @data
  
  if Hash === @data && !type.nil?
    Linkscape::Constants::CalculationKeyMap.each do |key, keys|
      unless @data[keys[0]].nil? or @data[keys[1]].nil?
        @data[key] = @data[keys[0]] - @data[keys[1]]
      end
    end
  end
  
  if @type == :hash
    subdatas = {}
    @data.each do |k,v|
      if field = Linkscape::Constants::ResponseFields[k]
        if subject = field[:subject]
          subdatas[subject] ||= {}
          v = ResponseData::Flags.new(v, field[:bitfield]) if field[:bitfield]
          subdatas[subject][field[:key]] = v
        end
      end
    end
    @subjects = []
    subdatas.each do |k,v|
      @data[k] = ResponseData.new(v, k)
      @subjects.push k
    end
  end
  
end

Instance Attribute Details

#subjectsObject (readonly)

Returns the value of attribute subjects.



10
11
12
# File 'lib/linkscape/response.rb', line 10

def subjects
  @subjects
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/linkscape/response.rb', line 10

def type
  @type
end

Instance Method Details

#[](*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/linkscape/response.rb', line 68

def [](*args)
  if Array === @data
    @data[*args]
  else
    k = args.first.to_sym
    if @subjects && @subjects.length == 1 && @data[@subjects.first][k]
      @data[@subjects.first][k]
    else
      @data[args.first.to_sym]
    end
  end
rescue
  nil
end

#inspectObject



110
111
112
113
# File 'lib/linkscape/response.rb', line 110

def inspect
  #<Linkscape::Response:0x10161d8a0 @response=#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>>
  %Q[#<#{self.class} @type=#{@type.inspect}] + (@subjects ? %Q[ @subjects=#{@subjects.inspect}] : "") + %Q[>]
end

#to_s(indent = "") ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/linkscape/response.rb', line 83

def to_s(indent="")
  printer = Proc.new do |h,prefix|
    o = ""
    h.sort{|l,r|l[0].to_s<=>r[0].to_s}.each do |k,v|
      v = v.to_s
      o += %Q[%s%-#{Linkscape::Constants::LongestKeyLength+5}.#{Linkscape::Constants::LongestKeyLength+5}s - %s\n] % [prefix, k, v]
    end
    o
  end
  if type == :array
    o = ""
    @data.each_with_index do |d,idx|
      o += %Q[#{indent}[#{idx}]\n] + d.to_s("#{indent}  ") + "\n"
    end
    o
  elsif @subjects
    o = ""
    @subjects.each do |s|
      o += %Q[#{indent}#{s}\n]
      o += printer.call(@data[s], "#{indent}  #{s}.")
    end
    o
  else
    printer.call(@data, indent)
  end
end