Class: TryApi::Method

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#id, typesafe_accessor

Class Method Details

.parse(hash:, project:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'app/models/try_api/method.rb', line 13

def parse(hash:, project:)
  return nil if hash.blank?
  instance = self.new
  instance.project = project
  instance.title = hash[:title]
  instance.html = hash[:html]
  instance.parameters = hash[:parameters]
  instance.headers = hash[:headers]
  instance.method = hash[:method].try(:upcase)
  instance.path = hash[:path]

  instance.example_responses = []
  if hash[:example_responses].is_a? Array
    hash[:example_responses].each do |example|
      instance.example_responses << TryApi::ExampleResponse.parse(example)
    end
  else
  #   TODO raise exception ?
  end

  instance.parameters = []
  if hash[:parameters].is_a? Array
    hash[:parameters].each do |parameter|
      instance.parameters << TryApi::Parameter.parse(parameter)
    end
  else
  #   TODO raise exception ?
  end

  instance.headers = []
  if hash[:headers].is_a? Array
    hash[:headers].each do |header|
      instance.headers << TryApi::Header.parse(header)
    end
  else
  #   TODO raise exception ?
  end
  instance
end

Instance Method Details

#full_pathObject



58
59
60
# File 'app/models/try_api/method.rb', line 58

def full_path
  "#{ project.host }:#{ project.port }/#{ project.api_prefix }#{ self.path }"
end

#local_pathObject



62
63
64
# File 'app/models/try_api/method.rb', line 62

def local_path
  "/#{ project.api_prefix }#{ self.path }"
end

#to_jsonObject



54
55
56
# File 'app/models/try_api/method.rb', line 54

def to_json
  super.merge local_path: local_path, full_path: full_path
end