Class: I3Ipc::Reply
- Inherits:
-
Object
- Object
- I3Ipc::Reply
- Defined in:
- lib/i3ipc/reply.rb
Overview
Wrapper for reply from i3-ipc.
Able to parse Numeric, String, TrueClass, FalseClass, Array, Hash from passed string in json format.
Class Method Summary collapse
-
.parse(response) ⇒ Reply
Parses response from I3-ipc protocol.
Instance Method Summary collapse
-
#initialize(data) ⇒ Reply
constructor
A new instance of Reply.
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to?(method_sym, include_private = false) ⇒ Boolean
-
#success? ⇒ Boolean
Indicates whether this reply is successful or not.
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data) ⇒ Reply
Returns a new instance of Reply.
35 36 37 |
# File 'lib/i3ipc/reply.rb', line 35 def initialize(data) @data = data.dup end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/i3ipc/reply.rb', line 57 def method_missing(name, *args, &block) if @data.include?(name) raise ArgumentError.new('wrong number of arguments (%d for 0)' % args.length) if args.length > 0 return @data[name] else super end end |
Class Method Details
.parse(response) ⇒ Reply
Parses response from I3-ipc protocol.
44 45 46 |
# File 'lib/i3ipc/reply.rb', line 44 def self.parse(response) parse_data JSON.parse(response) end |
Instance Method Details
#respond_to?(method_sym, include_private = false) ⇒ Boolean
66 67 68 69 70 71 72 |
# File 'lib/i3ipc/reply.rb', line 66 def respond_to?(method_sym, include_private = false) if @data.include?(method_sym) true else super end end |
#success? ⇒ Boolean
Indicates whether this reply is successful or not.
53 54 55 |
# File 'lib/i3ipc/reply.rb', line 53 def success? not self.respond_to? :error end |
#to_h ⇒ Object
78 79 80 81 82 83 |
# File 'lib/i3ipc/reply.rb', line 78 def to_h data = @data.dup data.each do |k, v| data[k] = Reply.unparse_data v end end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/i3ipc/reply.rb', line 74 def to_s JSON.pretty_generate(to_h) end |