Class: CaSmall::WebService

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

Overview

The WebService processes catissuews requests.

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ WebService

Returns a new instance of WebService.

Parameters:

  • mod (Module)

    the application domain module, e.g. CaTissue.



7
8
9
# File 'lib/casmall/web_service.rb', line 7

def initialize(mod)
  @context = mod
end

Instance Method Details

#create(json) ⇒ String

Returns the created object’s identifier as a JSON string.

Parameters:

  • json (String)

    the JSON representation of the object to create

Returns:

  • (String)

    the created object’s identifier as a JSON string



52
53
54
# File 'lib/casmall/web_service.rb', line 52

def create(json)
  JSON[json].create.identifier.to_json
end

#find(name, params) ⇒ String?

Returns the JSON content of the matching object(s), or nil if no match.

Parameters:

  • name (String)

    the search target class name, singular or plural

  • params ({String=>String})

    the attribute => value search arguments

Returns:

  • (String, nil)

    the JSON content of the matching object(s), or nil if no match



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/casmall/web_service.rb', line 14

def find(name, params)
  # the caTissue class to find
  tgt = name.singularize
  logger.info { "caSmall finding #{name.qp} #{params.qp}..." }
  klass = @context.const_get(tgt.camelize)
  # the attribute => value hash
  vh = {}
  # Convert the parameters into attribute => value entries.
  params.each do |k, v|
    pa = k.to_sym
    # the standard property
    prop = klass.property(pa)
    # Convert a numeric attribute string argument to an integer. 
    v = v.to_i if prop.type < Java::JavaLang::Number
    vh[pa] = v
  end
  # the search template
  tmpl = klass.new(vh)
  # the search result
  result = tgt == name ? tmpl.find : tmpl.query      
  # convert the search result, if any, to JSON 
  if result then
    logger.info { "caSmall found #{result.qp}." }
    result.to_json
  end
end

#query(json, path = nil) ⇒ String?

Returns the JSON content of the matching objects, or nil if no match.

Parameters:

  • json (String)

    the JSON representation of the query template

  • path (String, nil) (defaults to: nil)

    the optional query attribute path

Returns:

  • (String, nil)

    the JSON content of the matching objects, or nil if no match



44
45
46
47
48
# File 'lib/casmall/web_service.rb', line 44

def query(json, path=nil)
  path_a = path ? path.split('/').map { |s| s.to_sym } : Array::EMPTY_ARRAY
  # Bump the default nesting, since a query result can be deeply nested. 
  JSON[json].query(*path_a).to_json(:max_nesting => 100)
end

#update(json) ⇒ Object

Parameters:

  • json (String)

    the JSON representation of the object to update



57
58
59
60
61
62
63
64
# File 'lib/casmall/web_service.rb', line 57

def update(json)
  obj = JSON[json]
  if obj.identifier.nil? then
    obj.find or raise NotFoundError.new("Object to update was not found in the database: #{obj}")
  end
  obj.update
  true.to_json
end