Class: Restapi::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/restapi/application.rb

Defined Under Namespace

Classes: Engine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



18
19
20
21
22
23
# File 'lib/restapi/application.rb', line 18

def initialize
  super
  @method_descriptions = Hash.new
  @resource_descriptions = Hash.new
  clear_last
end

Instance Attribute Details

#last_api_argsObject

Returns the value of attribute last_api_args.



15
16
17
# File 'lib/restapi/application.rb', line 15

def last_api_args
  @last_api_args
end

#last_descriptionObject

Returns the value of attribute last_description.



15
16
17
# File 'lib/restapi/application.rb', line 15

def last_description
  @last_description
end

#last_errorsObject

Returns the value of attribute last_errors.



15
16
17
# File 'lib/restapi/application.rb', line 15

def last_errors
  @last_errors
end

#last_examplesObject

Returns the value of attribute last_examples.



15
16
17
# File 'lib/restapi/application.rb', line 15

def last_examples
  @last_examples
end

#last_paramsObject

Returns the value of attribute last_params.



15
16
17
# File 'lib/restapi/application.rb', line 15

def last_params
  @last_params
end

#last_seeObject

Returns the value of attribute last_see.



15
16
17
# File 'lib/restapi/application.rb', line 15

def last_see
  @last_see
end

#method_descriptionsObject (readonly)

Returns the value of attribute method_descriptions.



16
17
18
# File 'lib/restapi/application.rb', line 16

def method_descriptions
  @method_descriptions
end

#resource_descriptionsObject (readonly)

Returns the value of attribute resource_descriptions.



16
17
18
# File 'lib/restapi/application.rb', line 16

def resource_descriptions
  @resource_descriptions
end

Instance Method Details

#active_dsl?Boolean

Is there a reason to interpret the DSL for this run? with specific setting for some environment there is no reason the dsl should be interpreted (e.g. no validations and doc from cache)

Returns:

  • (Boolean)


205
206
207
# File 'lib/restapi/application.rb', line 205

def active_dsl?
  Restapi.configuration.validate? || ! Restapi.configuration.use_cache? || Restapi.configuration.force_dsl?
end

#add_example(example) ⇒ Object



54
55
56
# File 'lib/restapi/application.rb', line 54

def add_example(example)
  @last_examples << example.strip_heredoc
end

#add_method_description_args(method, path, desc) ⇒ Object



50
51
52
# File 'lib/restapi/application.rb', line 50

def add_method_description_args(method, path, desc)
  @last_api_args << MethodDescription::Api.new(method, path, desc)
end

#api_controllers_pathsObject



191
192
193
# File 'lib/restapi/application.rb', line 191

def api_controllers_paths
  Dir[Restapi.configuration.api_controllers_matcher]
end

#clearObject

Clear all apis in this application.



99
100
101
102
# File 'lib/restapi/application.rb', line 99

def clear
  @resource_descriptions.clear
  @method_descriptions.clear
end

#clear_lastObject

clear all saved data



105
106
107
108
109
110
111
112
# File 'lib/restapi/application.rb', line 105

def clear_last
  @last_api_args = []
  @last_errors = []
  @last_params = []
  @last_description = nil
  @last_examples = []
  @last_see = nil
end

#define_method_description(controller, method_name) ⇒ Object

create new method api description



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restapi/application.rb', line 26

def define_method_description(controller, method_name)
  # create new or get existing api
  resource_name = get_resource_name(controller)
  key = [resource_name, method_name].join('#')
  # add method description key to resource description
  resource = define_resource_description(controller)

  method_description = Restapi::MethodDescription.new(method_name, resource, self)

  @method_descriptions[key] ||= method_description

  @method_descriptions[key]
end

#define_resource_description(controller, &block) ⇒ Object

create new resource api description



41
42
43
44
45
46
47
48
# File 'lib/restapi/application.rb', line 41

def define_resource_description(controller, &block)
  resource_name = get_resource_name(controller)

  # puts "defining api for #{resource_name}"

  @resource_descriptions[resource_name] ||=
    Restapi::ResourceDescription.new(controller, resource_name, &block)
end

#get_api_argsObject



127
128
129
130
131
# File 'lib/restapi/application.rb', line 127

def get_api_args
  api_args = @last_api_args.clone
  @last_api_args.clear
  api_args
end

#get_descriptionObject

Return the current description, clearing it in the process.



115
116
117
118
119
# File 'lib/restapi/application.rb', line 115

def get_description
  desc = @last_description
  @last_description = nil
  desc
end

#get_errorsObject



121
122
123
124
125
# File 'lib/restapi/application.rb', line 121

def get_errors
  errors = @last_errors.clone
  @last_errors.clear
  errors
end

#get_examplesObject



145
146
147
148
149
# File 'lib/restapi/application.rb', line 145

def get_examples
  examples = @last_examples.clone
  @last_examples.clear
  examples
end

#get_method_description(resource_name, method_name = nil) ⇒ Object Also known as: []

get api for given method

There are two ways how this method can be used: 1) Specify both parameters

resource_name: controller class or string with resource name (plural)
method_name: name of the method (string or symbol)

2) Specify only first parameter:

resource_name: string containing both resource and method name joined
with # (eg. "users#create")


72
73
74
75
76
# File 'lib/restapi/application.rb', line 72

def get_method_description(resource_name, method_name = nil)
  resource_name = get_resource_name(resource_name)
  key = method_name.blank? ? resource_name : [resource_name, method_name].join('#')
  @method_descriptions[key]
end

#get_paramsObject



139
140
141
142
143
# File 'lib/restapi/application.rb', line 139

def get_params
  params = @last_params.clone
  @last_params.clear
  params
end

#get_resource_description(resource_name) ⇒ Object

get api for given resource



80
81
82
83
84
# File 'lib/restapi/application.rb', line 80

def get_resource_description(resource_name)
  resource_name = get_resource_name(resource_name)

  @resource_descriptions[resource_name]
end

#get_seeObject



133
134
135
136
137
# File 'lib/restapi/application.rb', line 133

def get_see
  see = @last_see
  @last_see = nil
  see
end

#recorded_examplesObject



151
152
153
154
155
156
157
158
159
160
# File 'lib/restapi/application.rb', line 151

def recorded_examples
  return @recorded_examples if @recorded_examples
  tape_file = File.join(Rails.root,"doc","restapi_examples.yml")
  if File.exists?(tape_file)
    @recorded_examples = YAML.load_file(tape_file)
  else
    @recorded_examples = {}
  end
  @recorded_examples
end

#reload_documentationObject



195
196
197
198
199
200
# File 'lib/restapi/application.rb', line 195

def reload_documentation
  reload_examples
  api_controllers_paths.each do |f|
    load_controller_from_file f
  end
end

#reload_examplesObject



162
163
164
# File 'lib/restapi/application.rb', line 162

def reload_examples
  @recorded_examples = nil
end

#remove_method_description(resource_name, method_name) ⇒ Object



86
87
88
89
90
# File 'lib/restapi/application.rb', line 86

def remove_method_description(resource_name, method_name)
  resource_name = get_resource_name(resource_name)

  @method_descriptions.delete [resource_name, method_name].join('#')
end

#remove_resource_description(resource_name) ⇒ Object



92
93
94
95
96
# File 'lib/restapi/application.rb', line 92

def remove_resource_description(resource_name)
  resource_name = get_resource_name(resource_name)

  @resource_descriptions.delete resource_name
end

#restapi_provided?Boolean

check if there is some saved description

Returns:

  • (Boolean)


59
60
61
# File 'lib/restapi/application.rb', line 59

def restapi_provided?
  true unless last_api_args.blank?
end

#to_json(resource_name, method_name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/restapi/application.rb', line 166

def to_json(resource_name, method_name)

  _resources = if resource_name.blank?
    # take just resources which have some methods because
    # we dont want to show eg ApplicationController as resource
    resource_descriptions.inject({}) do |result, (k,v)|
      result[k] = v.to_json unless v._methods.blank?
      result
    end
  else
    [@resource_descriptions[resource_name].to_json(method_name)]
  end

  {
    :docs => {
      :name => Restapi.configuration.app_name,
      :info => Restapi.configuration.app_info,
      :copyright => Restapi.configuration.copyright,
      :doc_url => Restapi.full_url(""),
      :api_url => Restapi.configuration.api_base_url,
      :resources => _resources
    }
  }
end