Class: RiderServer::Operation

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/rider_server/operation.rb

Direct Known Subclasses

SessionOperation

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

configure_logger, log, #log

Constructor Details

#initializeOperation

Returns a new instance of Operation.



54
55
56
57
58
# File 'lib/rider_server/operation.rb', line 54

def initialize
  @name = ""
  @arguments = []
  @documentation = ""
end

Class Attribute Details

.operationsObject (readonly)

Returns the value of attribute operations.



19
20
21
# File 'lib/rider_server/operation.rb', line 19

def operations
  @operations
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



42
43
44
# File 'lib/rider_server/operation.rb', line 42

def arguments
  @arguments
end

Class Method Details

.define(&block) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/rider_server/operation.rb', line 32

def self.define(&block)
  instance = new
  instance.instance_eval(&block)
  raise ArgumentError, "Operation #{self} must have a name" if instance.name.empty?
  raise ArgumentError, "Operation #{self} must have a documentation string" if instance.documentation.empty?
  raise ArgumentError, "Operation #{self} must have at least one argument" if instance.arguments.empty?
  @operations[instance.name] = instance
  instance
end

.handle(context, request) ⇒ Object



26
27
28
29
30
# File 'lib/rider_server/operation.rb', line 26

def self.handle(context, request)
  operation = @operations[request.op]
  operation.validate_request!(request)
  operation.handle(context, request)
end

.handles?(request) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rider_server/operation.rb', line 22

def self.handles?(request)
  @operations.key? request.op
end

Instance Method Details

#argument(name, type, description, required: false) ⇒ Object

Raises:

  • (ArgumentError)


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

def argument(name, type, description, required: false)
  @arguments ||= []

  raise ArgumentError, "Invalid argument type #{type}" \
    unless [:string, :integer, :array].include?(type)

  @arguments << {name: name, type: type, required: required, description: description}
end

#documentation(value = nil) ⇒ Object



44
45
46
47
# File 'lib/rider_server/operation.rb', line 44

def documentation(value = nil)
  @documentation = value if value
  @documentation
end

#name(value = nil) ⇒ Object



49
50
51
52
# File 'lib/rider_server/operation.rb', line 49

def name(value = nil)
  @name = value if value
  @name
end

#validate_request!(request) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rider_server/operation.rb', line 69

def validate_request!(request)
  @arguments.each do |arg|
    # Skip optional arguments
    next if !arg[:required] && !request.key?(arg[:name])

    name = arg[:name].to_s

    if arg[:type] == :integer
      request[name] = Integer(request[name])
    elsif arg[:type] == :string
      request[name] = String(request[name])
    elsif arg[:type] == :array && request[name].is_a?(Array)
      # pass, the type is correct
    else
      raise ArgumentError, "Invalid argument type #{arg[:type]}"
    end
  end
end