Class: Vericred::ApiResource

Inherits:
Object
  • Object
show all
Defined in:
lib/vericred/api_resource.rb

Direct Known Subclasses

County, Plan, Provider, State, ZipCode, ZipCounty

Constant Summary collapse

BASE_URL =
"https://api.vericred.com"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, full_data = {}) ⇒ ApiResource

Returns a new instance of ApiResource.



56
57
58
59
# File 'lib/vericred/api_resource.rb', line 56

def initialize(attrs, full_data = {})
  parse_relationships(attrs, full_data)
  @data = OpenStruct.new(attrs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)



84
85
86
87
# File 'lib/vericred/api_resource.rb', line 84

def method_missing(m, *args, &block)
  return @data.send(m, *args, &block) if @data.respond_to?(m)
  super
end

Class Method Details

.belongs_to(type) ⇒ Object



12
13
14
15
# File 'lib/vericred/api_resource.rb', line 12

def self.belongs_to(type)
  self.relationships[:belongs_to][type] =
    Vericred::Relationships::BelongsTo.new(self, type)
end

.connectionObject



17
18
19
# File 'lib/vericred/api_resource.rb', line 17

def self.connection
  @connection ||= Vericred::Connection.pool(size: 5)
end

.find(id) ⇒ Object



21
22
23
24
# File 'lib/vericred/api_resource.rb', line 21

def self.find(id)
  data = make_request(:get, uri(id), {}, headers)
  new(data[root_name] || {}, data)
end

.futureObject



26
27
28
# File 'lib/vericred/api_resource.rb', line 26

def self.future
  FutureProxy.new(self)
end

.has_many(type) ⇒ Object



30
31
32
33
# File 'lib/vericred/api_resource.rb', line 30

def self.has_many(type)
  self.relationships[:has_many][type] =
    Vericred::Relationships::HasMany.new(self, type)
end

.headersObject



41
42
43
44
45
# File 'lib/vericred/api_resource.rb', line 41

def self.headers
  {
    'Vericred-Api-Key' => Vericred.config.api_key
  }
end

.root_nameObject



47
48
49
# File 'lib/vericred/api_resource.rb', line 47

def self.root_name
  self.to_s.split("::").last.gsub(/([^^])([A-Z])/,'\1_\2').downcase
end

.search(query = {}) ⇒ Object



51
52
53
54
# File 'lib/vericred/api_resource.rb', line 51

def self.search(query = {})
  data = make_request(:get, uri, query, headers)
  (data[root_name.pluralize] || []).map { |row| new(row, data) }
end

.uri(id = nil) ⇒ Object



35
36
37
38
39
# File 'lib/vericred/api_resource.rb', line 35

def self.uri(id = nil)
  "/#{root_name.pluralize}".tap do |ret|
    ret << "/#{id}" if id.present?
  end
end