Class: SwaggerYard::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_yard/operation.rb

Constant Summary collapse

PARAMETER_LIST_REGEX =
/\A\[(\w*)\]\s*(\w*)(\(required\))?\s*(.*)\n([.\s\S]*)\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Operation

Returns a new instance of Operation.



36
37
38
39
40
41
# File 'lib/swagger_yard/operation.rb', line 36

def initialize(api)
  @api = api
  @parameters = []
  @model_names = []
  @error_messages = []
end

Instance Attribute Details

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



4
5
6
# File 'lib/swagger_yard/operation.rb', line 4

def error_messages
  @error_messages
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



4
5
6
# File 'lib/swagger_yard/operation.rb', line 4

def http_method
  @http_method
end

#model_namesObject (readonly)

Returns the value of attribute model_names.



5
6
7
# File 'lib/swagger_yard/operation.rb', line 5

def model_names
  @model_names
end

#notesObject

Returns the value of attribute notes.



3
4
5
# File 'lib/swagger_yard/operation.rb', line 3

def notes
  @notes
end

#parametersObject (readonly)

Returns the value of attribute parameters.



5
6
7
# File 'lib/swagger_yard/operation.rb', line 5

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/swagger_yard/operation.rb', line 4

def path
  @path
end

#response_typeObject (readonly)

Returns the value of attribute response_type.



4
5
6
# File 'lib/swagger_yard/operation.rb', line 4

def response_type
  @response_type
end

#summaryObject

Returns the value of attribute summary.



3
4
5
# File 'lib/swagger_yard/operation.rb', line 3

def summary
  @summary
end

Class Method Details

.from_yard_object(yard_object, api) ⇒ Object

TODO: extract to operation builder?



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/swagger_yard/operation.rb', line 10

def self.from_yard_object(yard_object, api)
  new(api).tap do |operation|
    yard_object.tags.each do |tag|
      case tag.tag_name
      when "path"
        operation.add_path_params_and_method(tag)
      when "parameter"
        operation.add_parameter(tag)
      when "parameter_list"
        operation.add_parameter_list(tag)
      when "response_type"
        operation.add_response_type(Type.from_type_list(tag.types))
      when "error_message"
        operation.add_error_message(tag)
      when "summary"
        operation.summary = tag.text
      when "notes"
        operation.notes = tag.text.gsub("\n", "<br\>")
      end
    end

    operation.sort_parameters
    operation.append_format_parameter
  end
end

Instance Method Details

#add_error_message(tag) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/swagger_yard/operation.rb', line 107

def add_error_message(tag)
  @error_messages << {
    "code" => Integer(tag.name),
    "message" => tag.text,
    "responseModel" => Array(tag.types).first
  }.reject {|_,v| v.nil?}
end

#add_parameter(tag) ⇒ Object

Example: [Array] status Filter by status. (e.g. status[]=1&status=2&status[]=3) Example: [Array] status(required) Filter by status. (e.g. status[]=1&status=2&status[]=3) Example: [Array] status(required, body) Filter by status. (e.g. status[]=1&status=2&status[]=3) Example: [Integer] media ID of the desired media type.



79
80
81
# File 'lib/swagger_yard/operation.rb', line 79

def add_parameter(tag)
  @parameters << Parameter.from_yard_tag(tag, self)
end

#add_parameter_list(tag) ⇒ Object

Example: [String] sort_order Orders ownerships by fields. (e.g. sort_order=created_at)

[List]      id              
[List]      begin_at        
[List]      end_at          
[List]      created_at


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/swagger_yard/operation.rb', line 89

def add_parameter_list(tag)
  # TODO: switch to using Parameter.from_yard_tag
  data_type, name, required, description, list_string = parse_parameter_list(tag)
  allowable_values = parse_list_values(list_string)

  @parameters << Parameter.new(name, Type.new(data_type.downcase), description, {
    required: !!required,
    param_type: "query",
    allow_multiple: false,
    allowable_values: allowable_values
  })
end

#add_path_params_and_method(tag) ⇒ Object

Example: [GET] /api/v2/ownerships.format_type Example: [PUT] /api/v1/accounts/account_id.format_type



65
66
67
68
69
70
71
72
# File 'lib/swagger_yard/operation.rb', line 65

def add_path_params_and_method(tag)
  @path = tag.text
  @http_method = tag.types.first

  parse_path_params(tag.text).each do |name|
    @parameters << Parameter.from_path_param(name)
  end
end

#add_response_type(type) ⇒ Object



102
103
104
105
# File 'lib/swagger_yard/operation.rb', line 102

def add_response_type(type)
  model_names << type.model_name
  @response_type = type
end

#append_format_parameterObject



119
120
121
# File 'lib/swagger_yard/operation.rb', line 119

def append_format_parameter
  @parameters << format_parameter
end

#nicknameObject



43
44
45
# File 'lib/swagger_yard/operation.rb', line 43

def nickname
  @path[1..-1].gsub(/[^a-zA-Z\d:]/, '-').squeeze("-") + http_method.downcase
end

#ref?(data_type) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/swagger_yard/operation.rb', line 123

def ref?(data_type)
  @api.ref?(data_type)
end

#sort_parametersObject



115
116
117
# File 'lib/swagger_yard/operation.rb', line 115

def sort_parameters
  @parameters.sort_by! {|p| p.name}
end

#to_hObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/swagger_yard/operation.rb', line 47

def to_h
  {
    "httpMethod"        => http_method,
    "nickname"          => nickname,
    "type"              => "void",
    "produces"          => ["application/json", "application/xml"],
    "parameters"        => parameters.map(&:to_h),
    "summary"           => summary || @api.description,
    "notes"             => notes,
    "responseMessages"  => error_messages
  }.tap do |h|
    h.merge!(response_type.to_h) if response_type
  end
end