Class: TryApi::ExampleResponse

Inherits:
Base
  • Object
show all
Defined in:
app/models/try_api/example_response.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#id, typesafe_accessor

Class Method Details

.descriptionsObject



19
20
21
22
23
24
25
26
# File 'app/models/try_api/example_response.rb', line 19

def descriptions
  {
      200 => :success,
      401 => :unauthorized,
      422 => :unprocessable_entity,
      500 => :internal_server_error,
  }
end

.parse(hash:, project:) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'app/models/try_api/example_response.rb', line 9

def parse(hash:, project:)
  return nil if hash.blank?
  instance = self.new
  instance.code = hash[:code]
  instance.project = project
  instance.response = hash[:response]
  instance.type = hash[:type]
  instance
end

Instance Method Details

#colorObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/try_api/example_response.rb', line 33

def color
  case self.code
    when 200
      'success'
    when 200...300
      'info'
    when 300...400
      'warning'
    when 400...500
      'warning'
    when 500
      'danger'
    else
      'default'
  end
end

#descriptionObject



29
30
31
# File 'app/models/try_api/example_response.rb', line 29

def description
  self.class.descriptions[self.code].to_s.humanize
end

#response=(input) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/try_api/example_response.rb', line 54

def response=(input)
  if input['var:']
    @response = self.project.variables[input.gsub('var:', '')]
  else
    @response = input
  end
end

#to_jsonObject



50
51
52
# File 'app/models/try_api/example_response.rb', line 50

def to_json
  super.merge color: color, description: description, isCollapsed: true
end