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.



12
13
14
# File 'lib/platform/api/proxy/base.rb', line 12

def initialize(instance)
  @instance = instance
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



6
7
8
# File 'lib/platform/api/proxy/base.rb', line 6

def instance
  @instance
end

Class Method Details

.<=>(other) ⇒ Object



30
31
32
# File 'lib/platform/api/proxy/base.rb', line 30

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

.api_versionObject



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

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

.ensure_valid_class_nameObject

Raises:

  • (NameError)


44
45
46
# File 'lib/platform/api/proxy/base.rb', line 44

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

.instance_classObject



40
41
42
# File 'lib/platform/api/proxy/base.rb', line 40

def self.instance_class
  @klass
end

.proxy_for(klass) ⇒ Object



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

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

Instance Method Details

#full_url(url) ⇒ Object



60
61
62
63
# File 'lib/platform/api/proxy/base.rb', line 60

def full_url(url)
  return url if url.index('http')
  "http://#{Platform::Config.site_base_url}#{url}"
end

#sanitize_api_hash(hash) ⇒ Object



52
53
54
# File 'lib/platform/api/proxy/base.rb', line 52

def sanitize_api_hash(hash)
  hash.select{|key, value| !value.blank?}
end

#to_api_hash(opts = {}) ⇒ Object

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/platform/api/proxy/base.rb', line 48

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

#to_api_path(opts = {}) ⇒ Object



56
57
58
# File 'lib/platform/api/proxy/base.rb', line 56

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



20
21
22
# File 'lib/platform/api/proxy/base.rb', line 20

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

#to_xml(opts = {}) ⇒ Object



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

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



16
17
18
# File 'lib/platform/api/proxy/base.rb', line 16

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