Class: OReflect::Service

Inherits:
Object
  • Object
show all
Includes:
Accessors
Defined in:
lib/oreflect/service.rb

Constant Summary collapse

DEFAULT_VERSION =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Accessors

included

Constructor Details

#initialize(&block) ⇒ Service

Returns a new instance of Service.



12
13
14
15
16
17
18
# File 'lib/oreflect/service.rb', line 12

def initialize(&block)
  @version = DEFAULT_VERSION
  @endpoints = []
  @errors = {}
  instance_eval(&Error::DEFAULT_ERRORS)
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



8
9
10
# File 'lib/oreflect/service.rb', line 8

def endpoints
  @endpoints
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/oreflect/service.rb', line 8

def errors
  @errors
end

Instance Method Details

#action_by_dashed_id(dashed_action_id) ⇒ Object



28
29
30
# File 'lib/oreflect/service.rb', line 28

def action_by_dashed_id(dashed_action_id)
  action_by_id dashed_action_id.gsub('-', '_')
end

#action_by_id(action_id) ⇒ Object



24
25
26
# File 'lib/oreflect/service.rb', line 24

def action_by_id(action_id)
  actions.detect { |a| a.id.to_s == action_id.to_s }
end

#actionsObject



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

def actions
  endpoints.map { |endpoint| endpoint.actions }.flatten
end

#as_hashObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oreflect/service.rb', line 50

def as_hash
  h = {}
  h[:version] = version
  h[:name] = name
  h[:description] = description
  h[:base_uri] = base_uri
  h[:errors] = errors.values.inject({}) { |hsh, err|
    hsh.merge(err.code => err.as_hash) }
  h[:endpoints] = endpoints.map { |endpoint| endpoint.as_hash }
  h
end

#error(code, &block) ⇒ Object



46
47
48
# File 'lib/oreflect/service.rb', line 46

def error(code, &block)
  @errors[code] = Error.new(code, &block)
end

#resource(template, &block) ⇒ Object



38
39
40
# File 'lib/oreflect/service.rb', line 38

def resource(template, &block)
  @endpoints << Endpoint.new(template, &block)
end

#resources(template, &block) ⇒ Object



42
43
44
# File 'lib/oreflect/service.rb', line 42

def resources(template, &block)
  @endpoints << Endpoint.new(template, true, &block)
end

#version(value = nil) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
# File 'lib/oreflect/service.rb', line 32

def version(value = nil)
  return @version unless value
  raise ArgumentError, 'value must be an integer' unless value.is_a?(Fixnum)
  @version = value
end