Class: ActiveJabber::Base::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, part, parts) ⇒ Request

Returns a new instance of Request.



61
62
63
64
65
66
67
# File 'lib/activejabber.rb', line 61

def initialize(base, part, parts)
  @base = base
  @parts = parts
  unless part.nil?
    @parts << ('/' + part.to_s)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

The chainable magic happens here.



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
104
105
106
107
# File 'lib/activejabber.rb', line 77

def method_missing(method, *args)
  if args.length > 0 or self.parse_format(method) or method.to_s.ends_with? '!'
    format = self.parse_format(method)
    opts = {
      :format => (format or :text),
      :args => '',
      :timeout => 5.0
    }
    if args.length >= 1
      # TODO: Logic to handle other ways of transforming data into a sendable format.
      if args.first.respond_to? :to_s
        opts[:args] = args.first.to_s
      end
      if args.second.is_a? Hash
        opts.merge! args.second
      end
    end
    if args.first.is_a? Hash
      opts.merge! args.first
    end
    # TODO: Support more formats.
    if format == :json
      @parts << '.json'
    else
      @parts << ('/' + method.to_s.gsub(/[?!]$/, ''))
    end
    @base.request(@parts.join(''), opts)
  else
    Request.new(@base, method, @parts)
  end
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



59
60
61
# File 'lib/activejabber.rb', line 59

def parts
  @parts
end

Instance Method Details

#parse_format(method) ⇒ Object

Determines if this is a known format.



69
70
71
72
73
74
75
# File 'lib/activejabber.rb', line 69

def parse_format(method)
  if method.to_s == 'json' or method.to_s == 'json?'
    :json
  elsif method.to_s == 'text' or method.to_s == 'text?'
    :text
  end
end