Module: ApiTwister::ClassMethods

Defined in:
lib/api-twister/api_twister.rb

Instance Method Summary collapse

Instance Method Details

#api(name, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/api-twister/api_twister.rb', line 25

def api(name, options={})
  self._api_specifications ||= {}
  _api_specifications[name] ||= []
  
  if options[:only]
    options[:only].each do |only|
      if [:methods, :attributes, :associations].include?(only)
        _api_specifications[name] << _api_definition.all_items(only.to_s.singularize.to_sym)
      else
        _api_specifications[name] << only
      end
    end
  elsif options[:except]
    everything = _api_definition.all_items
    except_list = []
    options[:except].each do |except|
      if [:methods, :attributes, :associations].include?(except)
        except_list << _api_definition.all_items(except.to_s.singularize.to_sym)
      else
        except_list << except
      end
    end
    except_list.flatten!
    everything.flatten!
    _api_specifications[name]  = everything - except_list
  else
    self._api_specifications[name] = _api_definition.all_items
  end
  _api_specifications[name].flatten!
end

#api_hash(name, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/api-twister/api_twister.rb', line 56

def api_hash(name, options = {})
  spec = self._api_specifications[name]
  hash = { :only => [] }
  hash[:skip_types] = options[:skip_types] if options[:skip_types]
  hash[:root] = options[:root] if options[:root]
  user = options[:user]

  spec.each do |item|
    object = self._api_definition.api_items[item]
    if object.is_a?(ApiMethod)
      hash[:methods] ||= []
      hash[:methods] << item if user_has_permission?(user, item)
    elsif object.is_a?(ApiAttribute)
      hash[:only] << item if user_has_permission?(user, item)
    else
      raise "Nil object. Spec: #{spec}, Item: #{item}" if object.nil?
      hash[:include] ||= {}

      hash[:include][item] = object.api_hash(name, options) if user_has_permission?(user, object.association)
    end
  end if spec
  hash
end

#api_models(user) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/api-twister/api_twister.rb', line 80

def api_models(user)
  #TODO: Test
  models = [self] if user_has_permission?(user, self.name)
  self._api_definition.all_objects(:association).each do |assoc|
    models += assoc.model.api_models if user_has_permission?(user, assoc.association)
  end
  models
end

#define_api(options = {}) {|self._api_definition| ... } ⇒ Object

Yields:

  • (self._api_definition)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/api-twister/api_twister.rb', line 12

def define_api(options={})
  self._api_definition ||= ApiDefinition.new(self, options)

  yield self._api_definition if block_given?

  # if no attributes were added in block, add all the attributes here
  unless self._api_definition.has_attributes?
    self.new.attributes.symbolize_keys.keys.each { |k| _api_definition.attribute k }
  end

  self._api_definition
end

#exposes_as(item) ⇒ Object



89
90
91
92
# File 'lib/api-twister/api_twister.rb', line 89

def exposes_as(item)
  value = _api_definition.api_items[item.to_sym]
  value.is_a?(ApiAssociation) ? :association : value
end