Class: TryApi::Base

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

Direct Known Subclasses

ExampleResponse, Header, MenuItem, Method, Parameter, Project

Constant Summary collapse

@@instances_count =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Base

Returns a new instance of Base.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/try_api/base.rb', line 66

def initialize(hash)
  self.id = @@instances_count += 1
  result_hash = {}
  hash.to_h.each do |k, v|
    if k.to_s == 'include!'
      result_hash.merge! load_inclusion(v)
    else
      result_hash[k] = v
    end
  end

  result_hash.to_h.each do |k, v|
    v = self.project.variables[v.gsub('var:', '')] if v.is_a?(String) && v.start_with?('var:')
    send("#{k}=", v) if respond_to?("#{k}=")
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



38
39
40
# File 'app/models/try_api/base.rb', line 38

def id
  @id
end

Class Method Details

.load_inclusion(filename) ⇒ Object



83
84
85
86
87
88
89
90
# File 'app/models/try_api/base.rb', line 83

def self.load_inclusion(filename)
  if File.exist?("#{ Rails.root }/config/try_api/#{ filename }.yml")
    hash = YAML.load_file("#{ Rails.root }/config/try_api/#{ filename }.yml")
    hash.with_indifferent_access
  else
    raise ConfigFileNotFound
  end
end

.typesafe_accessor(name, type, options = {}) ⇒ Object



8
9
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
35
36
# File 'app/models/try_api/base.rb', line 8

def self.typesafe_accessor(name, type, options={})

  define_method(name) do
    instance_variable_get("@#{name}")
  end

  define_method("#{name}=") do |value|
    if value.is_a?(type) || value.nil?
      if options[:items_type].try(:<, TryApi::Base)

        items = []
        if value.is_a? Array
          value.each do |item|
            item = load_inclusion(item[:include!]) if item[:include!].present?

            items << options[:items_type].new({ parent: self }.merge(item))
          end
        end

        instance_variable_set("@#{name}", items)
      else
        value = options[:default] if value.nil? && !options[:default].nil?
        instance_variable_set("@#{name}", value)
      end
    else
      raise TryApi::ArgumentError.new
    end
  end
end

Instance Method Details

#load_inclusion(filename) ⇒ Object



92
93
94
# File 'app/models/try_api/base.rb', line 92

def load_inclusion(filename)
  self.class.load_inclusion filename
end

#projectObject



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

def project
  self.is_a?(TryApi::Project) ? self : self.parent.try(:project)
end

#to_jsonObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/try_api/base.rb', line 42

def to_json
  result = {}

  self.instance_variables.each do |i|
    value = self.instance_variable_get(i)
    if value.instance_of?(Array)
      result[i.to_s.delete('@')] = value.map(&:to_json)
    else
      if i == :@parent

      else
        result[i.to_s.delete('@')] = value
      end
    end
  end

  result.merge!({id: self.id})
  result.with_indifferent_access
end