Module: ActionWebService::SignatureTypes

Included in:
API::Base, BaseType, Casting::BaseCaster
Defined in:
lib/action_web_service/support/signature_types.rb

Overview

Action Web Service supports the following base types in a signature:

:int

Represents an integer value, will be cast to an integer using Integer(value)

:string

Represents a string value, will be cast to an string using the to_s method on an object

:base64

Represents a Base 64 value, will contain the binary bytes of a Base 64 value sent by the caller

:bool

Represents a boolean value, whatever is passed will be cast to boolean (true, ‘1’, ‘true’, ‘y’, ‘yes’ are taken to represent true; false, ‘0’, ‘false’, ‘n’, ‘no’ and nil represent false)

:float

Represents a floating point value, will be cast to a float using Float(value)

:time

Represents a timestamp, will be cast to a Time object

:datetime

Represents a timestamp, will be cast to a DateTime object

:date

Represents a date, will be cast to a Date object

For structured types, you’ll need to pass in the Class objects of ActionWebService::Struct and ActiveRecord::Base derivatives.

Class Method Summary collapse

Class Method Details

.canonical_signature(signature) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
23
# File 'lib/action_web_service/support/signature_types.rb', line 16

def canonical_signature(signature) # :nodoc:
  return nil if signature.nil?
  unless signature.is_a?(Array)
    raise(ActionWebServiceError, "Expected signature to be an Array")
  end
  i = -1
  signature.map{ |spec| canonical_signature_entry(spec, i += 1) }
end

.canonical_signature_entry(spec, i) ⇒ Object

:nodoc:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/action_web_service/support/signature_types.rb', line 25

def canonical_signature_entry(spec, i) # :nodoc:
  orig_spec = spec
  name = "param#{i}"
  if spec.is_a?(Hash)
    name, spec = spec.keys.first, spec.values.first
  end
  type = spec
  if spec.is_a?(Array)
    ArrayType.new(orig_spec, canonical_signature_entry(spec[0], 0), name)
  else
    type = canonical_type(type)
    if type.is_a?(Symbol)
      BaseType.new(orig_spec, type, name)
    else
      StructuredType.new(orig_spec, type, name)
    end
  end
end

.canonical_type(type) ⇒ Object

:nodoc:



44
45
46
47
48
49
# File 'lib/action_web_service/support/signature_types.rb', line 44

def canonical_type(type) # :nodoc:
  type_name = symbol_name(type) || class_to_type_name(type)
  type = type_name || type
  return canonical_type_name(type) if type.is_a?(Symbol)
  type
end

.canonical_type_class(type) ⇒ Object

:nodoc:



79
80
81
82
# File 'lib/action_web_service/support/signature_types.rb', line 79

def canonical_type_class(type) # :nodoc:
  type = canonical_type(type)
  type.is_a?(Symbol) ? type_name_to_class(type) : type
end

.canonical_type_name(name) ⇒ Object

:nodoc:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/action_web_service/support/signature_types.rb', line 51

def canonical_type_name(name) # :nodoc:
  name = name.to_sym
  case name
    when :int, :integer, :fixnum
      :int
    when :bignum, :long
      :long
    when :string, :text
      :string
    when :base64, :binary
      :base64
    when :bool, :boolean
      :bool
    when :float, :double
      :float
    when :decimal
      :decimal
    when :time, :timestamp
      :time
    when :datetime
      :datetime
    when :date
      :date
    else
      raise(TypeError, "#{name} is not a valid base type")
  end
end

.class_to_type_name(klass) ⇒ Object

:nodoc:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/action_web_service/support/signature_types.rb', line 89

def class_to_type_name(klass) # :nodoc:
  klass = klass.class unless klass.is_a?(Class)
  if derived_from?(Integer, klass) || derived_from?(Fixnum, klass)
    :int
  elsif derived_from?(Bignum, klass)
    :long
  elsif klass == String
    :string
  elsif klass == Base64
    :base64
  elsif klass == TrueClass || klass == FalseClass
    :bool
  elsif derived_from?(Float, klass) || derived_from?(Precision, klass) || derived_from?(Numeric, klass)
    :float
  elsif klass == Time
    :time
  elsif klass == DateTime
    :datetime
  elsif klass == Date
    :date
  else
    nil
  end
end

.derived_from?(ancestor, child) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


141
142
143
# File 'lib/action_web_service/support/signature_types.rb', line 141

def derived_from?(ancestor, child) # :nodoc:
  child.ancestors.include?(ancestor)
end

.symbol_name(name) ⇒ Object

:nodoc:



84
85
86
87
# File 'lib/action_web_service/support/signature_types.rb', line 84

def symbol_name(name) # :nodoc:
  return name.to_sym if name.is_a?(Symbol) || name.is_a?(String)
  nil
end

.type_name_to_class(name) ⇒ Object

:nodoc:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/action_web_service/support/signature_types.rb', line 114

def type_name_to_class(name) # :nodoc:
  case canonical_type_name(name)
  when :int
    Integer
  when :long
    Bignum
  when :string
    String
  when :base64
    Base64
  when :bool
    TrueClass
  when :float
    Float
  when :decimal
    BigDecimal
  when :time
    Time
  when :date
    Date
  when :datetime
    DateTime
  else
    nil
  end
end