Class: JoshuaRemote

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, debug: false) ⇒ JoshuaRemote

Returns a new instance of JoshuaRemote.



15
16
17
18
19
# File 'lib/misc/ruby_client.rb', line 15

def initialize root, debug: false
  @debug = debug
  @root  = root
  @path  = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



21
22
23
24
25
26
# File 'lib/misc/ruby_client.rb', line 21

def method_missing name, *args
  @path.push name
  return call if @path[1]
  @path.push args.first if args.first
  self
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



13
14
15
# File 'lib/misc/ruby_client.rb', line 13

def response
  @response
end

Instance Method Details

#call(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/misc/ruby_client.rb', line 28

def call *args
  path =
  if args.first
    args = args.flatten
    args[1] ? args.join('/') : args.first
  else
    '/' + @path.join('/')
  end

  path = [@root, path].join('/')
  puts 'Joshua: %s' % path if @debug

  @path     = []
  @response = JSON.parse HTTP.get(path).to_s
end

#error?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/misc/ruby_client.rb', line 48

def error?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/misc/ruby_client.rb', line 44

def success?
  @response['success'] == true
end