Class: Shatter::Service::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/shatter/service/function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function_params) ⇒ Function

Returns a new instance of Function.



20
21
22
# File 'lib/shatter/service/function.rb', line 20

def initialize(function_params)
  @params = function_params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



18
19
20
# File 'lib/shatter/service/function.rb', line 18

def params
  @params
end

Class Method Details

.define_param(name, type:, nullable: true) ⇒ Object



8
9
10
11
# File 'lib/shatter/service/function.rb', line 8

def self.define_param(name, type:, nullable: true)
  @param_meta ||= {}
  @param_meta[name] = { name:, type:, nullable: }
end

.invokeObject



58
59
60
# File 'lib/shatter/service/function.rb', line 58

def self.invoke
  raise "cant invoke for base function"
end

.metaObject



13
14
15
# File 'lib/shatter/service/function.rb', line 13

def self.meta
  @param_meta || {}
end

.to_typescriptObject



62
63
64
65
66
67
# File 'lib/shatter/service/function.rb', line 62

def self.to_typescript
  function_nm = Shatter::Service::Base.service_definition.function_collection.to_a.detect do |fn_def|
                  fn_def[1] == self
                end[0]
  ERB.new(File.read("#{__dir__}/../../../templates/function_definition.ts.erb")).result(binding)
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
# File 'lib/shatter/service/function.rb', line 24

def call
  { result: nil, error: "Invalid Parameters" } unless valid_params?
  { result: nil, error: nil }.merge(invoke.merge(uuid: params[:uuid]))
rescue StandardError => e
  Shatter.logger.error e.message
  Shatter.logger.error e.backtrace
  { result: nil, error: "Something Went Wrong", uuid: params[:uuid] }
end

#valid_param?(arg) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/shatter/service/function.rb', line 40

def valid_param?(arg)
  meta = self.class.meta[arg]
  return false if meta.nil?

  meta => type:, nullable:
  val = value_for_arg(arg)
  return nullable if val.nil?

  return false if type == "string" && !val.is_a?(String)
  return false if type == "integer" && !val.is_a?(Integer)

  true
end

#valid_params?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/shatter/service/function.rb', line 33

def valid_params?
  self.class.meta.each_key do |arg|
    return false unless valid_param?(arg)
  end
  true
end

#value_for_arg(arg) ⇒ Object



54
55
56
# File 'lib/shatter/service/function.rb', line 54

def value_for_arg(arg)
  @params[arg.to_s] || @params[arg.to_sym]
end