Class: Platform::Api::Proxy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/platform/api/proxy/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Base

Returns a new instance of Base.



14
15
16
# File 'lib/platform/api/proxy/base.rb', line 14

def initialize(instance)
  @instance = instance
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



8
9
10
# File 'lib/platform/api/proxy/base.rb', line 8

def instance
  @instance
end

Class Method Details

.<=>(other) ⇒ Object



32
33
34
# File 'lib/platform/api/proxy/base.rb', line 32

def self.<=>(other)
  api_version <=> other.api_version
end

.api_versionObject



36
37
38
39
40
# File 'lib/platform/api/proxy/base.rb', line 36

def self.api_version
  @api_version ||= begin
    $1.to_i if name =~ /_(\d+)$/
  end
end

.ensure_valid_class_nameObject

Raises:

  • (NameError)


46
47
48
# File 'lib/platform/api/proxy/base.rb', line 46

def self.ensure_valid_class_name
  raise NameError.new("Proxy class name (#{name}) must end in _<version>") if api_version.nil?
end

.instance_classObject



42
43
44
# File 'lib/platform/api/proxy/base.rb', line 42

def self.instance_class
  @klass
end

.proxy_for(klass) ⇒ Object



10
11
12
# File 'lib/platform/api/proxy/base.rb', line 10

def self.proxy_for(klass)
  Proxy.add(klass, self)
end

Instance Method Details

#to_api_hash(opts = {}) ⇒ Object

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/platform/api/proxy/base.rb', line 50

def to_api_hash(opts = {})
  raise NotImplementedError, 'Must be implemented in descendant class'
end

#to_api_path(opts = {}) ⇒ Object



54
55
56
# File 'lib/platform/api/proxy/base.rb', line 54

def to_api_path(opts = {})
  "#{Platform::Config.api_scheme}://#{Platform::Config.api_base_url}/#{instance.class.name.underscore}/#{instance.id}"
end

#to_json(opts = {}) ⇒ Object



22
23
24
# File 'lib/platform/api/proxy/base.rb', line 22

def to_json(opts={})
  Api::AlreadyJsonedString.new(to_api_hash(opts).to_json(opts))
end

#to_xml(opts = {}) ⇒ Object



26
27
28
29
30
# File 'lib/platform/api/proxy/base.rb', line 26

def to_xml(opts={})
  opts = opts.dup
  opts[:root] ||= instance.class.name.underscore.downcase
  to_api_hash(opts).to_xml(opts)
end

#update_attributes!(attrs) ⇒ Object



18
19
20
# File 'lib/platform/api/proxy/base.rb', line 18

def update_attributes!(attrs)
  instance.update_attributes!(attrs)
end