Class: Plangrade::Resources::Company

Inherits:
Base
  • Object
show all
Defined in:
lib/plangrade/resources/company.rb

Instance Attribute Summary

Attributes inherited from Base

#attrs, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#api_handler, base_name, #base_name, #changes, #delete!, fetch, identity_map, #initialize, #load!, #loaded?, model_attributes, #modified?, #new_record?, #persisted?, #reload!, #save

Methods included from ApiHandler

#api_handler, #establish_api_handler

Constructor Details

This class inherits a constructor from Plangrade::Resources::Base

Class Method Details

.all(*opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/plangrade/resources/company.rb', line 19

def self.all(*opts)
  if opts && opts != nil && opts != {}
    result = api_handler.all_companies(opts)
  else
    result = api_handler.all_companies
  end
  parsed_result = JSON.parse(result.body)
  raise parsed_result.to_yaml
  companies ||= begin
    parsed_result.map do |company|
      new(:id => company[:id], :name => company[:name], :ein => company[:ein], :name => company[:name])
    end
  end
  companies
end

.create(ein, name) ⇒ Object



7
8
9
10
11
12
# File 'lib/plangrade/resources/company.rb', line 7

def self.create(ein, name)
  result = api_handler.create_company(:ein => ein, :name => name)
  return nil unless result.created?
  id = result.headers[:location].split('/').last.to_i
  new(:id => id)
end

.get(id) ⇒ Object



14
15
16
17
# File 'lib/plangrade/resources/company.rb', line 14

def self.get(id)
  result = api_handler.get_company(id)
  new(result.body)
end

Instance Method Details

#update!(params) ⇒ Object



35
36
37
# File 'lib/plangrade/resources/company.rb', line 35

def update!(params)
  api_handler.update_company(@id, params)
end