Class: Linkscape::Response::ResponseData
- Inherits:
-
Object
- Object
- Linkscape::Response::ResponseData
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/linkscape/response.rb
Defined Under Namespace
Classes: Flags
Instance Attribute Summary collapse
-
#subjects ⇒ Object
readonly
Returns the value of attribute subjects.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#initialize(data, type = nil) ⇒ ResponseData
constructor
A new instance of ResponseData.
- #inspect ⇒ Object
- #to_s(indent = "") ⇒ Object
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 |
# 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 @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
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
10 11 12 |
# File 'lib/linkscape/response.rb', line 10 def subjects @subjects end |
#type ⇒ Object (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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/linkscape/response.rb', line 59 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 |
#inspect ⇒ Object
105 106 107 108 |
# File 'lib/linkscape/response.rb', line 105 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/linkscape/response.rb', line 74 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| field = Linkscape::Constants::ResponseFields[k] v = v.to_s # v = ((field && field[:bitfield]) ? v.to_a.inspect : v).to_s #desc = field ? field[:name] : '*'+k.inspect #o += %Q[%s%-#{Linkscape::Constants::LongestNameLength+15}.#{Linkscape::Constants::LongestNameLength+15}s - %s\n] % [prefix, desc, v] 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 |