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_paramsObject

Returns the value of attribute last_params.



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

def last_params
  @last_params
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

#clearObject

Clear all apis in this application.



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

def clear
  @resource_descriptions.clear
  @method_descriptions.clear
end

#clear_lastObject

clear all saved data



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

def clear_last
  @last_api_args = nil
  @last_errors = Array.new
  @last_params = Hash.new
  @last_description = nil
end

#define_method_description(resource_name, method_name) ⇒ Object

create new method api description



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

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

  puts "defining api for #{resource_name}:#{method_name}"

  # create new or get existing api
  key = [resource_name, method_name].join('#')
  @method_descriptions[key] ||= Restapi::MethodDescription.new(method_name, resource_name, self)

  # add mapi key to resource api
  define_resource_description(resource_name).add_method(key)

  @method_descriptions[key]
end

#define_resource_description(resource_name, &block) ⇒ Object

create new resource api description



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

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

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

#get_api_argsObject



112
113
114
115
116
# File 'lib/restapi/application.rb', line 112

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.



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

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

#get_errorsObject



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

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

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

get api for given method



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

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

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

#get_paramsObject



118
119
120
121
122
# File 'lib/restapi/application.rb', line 118

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

#get_resource_description(resource_name) ⇒ Object

get api for given resource



67
68
69
70
71
# File 'lib/restapi/application.rb', line 67

def get_resource_description(resource_name)
  resource_name = get_resource_name(resource_name)

  @resource_descriptions[resource_name]
end

#remove_method_description(resource_name, method_name) ⇒ Object



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

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



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

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)


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

def restapi_provided?
  true unless last_api_args.blank?
end

#to_json(resource_name, method_name) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/restapi/application.rb', line 124

def to_json(resource_name, method_name)
  
  _resources = if resource_name.blank?
    resource_descriptions.collect { |_,v| v.to_json }
  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.configuration.doc_base_url,
      'api_url' => Restapi.configuration.api_base_url,
      'resources' => _resources
    }
  }
end