Class: BlockScore::Base

Inherits:
Object
  • Object
show all
Extended by:
Connection
Defined in:
lib/blockscore/base.rb

Direct Known Subclasses

Candidate, Company, Person, QuestionSet, WatchlistHit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

delete, get, patch, post

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/blockscore/base.rb', line 9

def initialize(options = {})
  @attributes = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blockscore/base.rb', line 75

def method_missing(method, *args, &block)
  if respond_to_missing?(method)
    if setter?(method)
      add_setter(method, args)
    else
      add_accessor(method, args)
    end
    send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/blockscore/base.rb', line 7

def attributes
  @attributes
end

Class Method Details

.api_urlObject



43
44
45
# File 'lib/blockscore/base.rb', line 43

def self.api_url
  'https://api.blockscore.com/'
end

.endpointObject



47
48
49
50
51
52
53
# File 'lib/blockscore/base.rb', line 47

def self.endpoint
  if self == Base
    fail NotImplementedError, 'Base is an abstract class, not an API resource'
  end

  "#{api_url}#{Util.to_plural(resource)}"
end

.resourceObject



39
40
41
# File 'lib/blockscore/base.rb', line 39

def self.resource
  @resource ||= Util.to_underscore(to_s.split('::').last)
end

Instance Method Details

#inspectObject



13
14
15
# File 'lib/blockscore/base.rb', line 13

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} JSON: " + JSON.pretty_generate(attributes)
end

#refreshObject



17
18
19
20
21
22
23
24
# File 'lib/blockscore/base.rb', line 17

def refresh
  r = self.class.retrieve(id)
  @attributes = r.attributes

  true
rescue Error
  false
end

#saveObject



26
27
28
29
30
# File 'lib/blockscore/base.rb', line 26

def save
  save!
rescue
  false
end

#save!Object



32
33
34
35
36
37
# File 'lib/blockscore/base.rb', line 32

def save!
  response = self.class.post(self.class.endpoint, attributes)
  @attributes = response.attributes

  true
end