Class: Platform::Api::Proxy::Base
- Defined in:
- lib/platform/api/proxy/base.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
Returns the value of attribute instance.
Class Method Summary collapse
- .<=>(other) ⇒ Object
- .api_version ⇒ Object
- .ensure_valid_class_name ⇒ Object
- .instance_class ⇒ Object
- .proxy_for(klass) ⇒ Object
Instance Method Summary collapse
- #full_url(url) ⇒ Object
-
#initialize(instance) ⇒ Base
constructor
A new instance of Base.
- #sanitize_api_hash(hash) ⇒ Object
- #to_api_hash(opts = {}) ⇒ Object
- #to_api_path(opts = {}) ⇒ Object
- #to_json(opts = {}) ⇒ Object
- #to_xml(opts = {}) ⇒ Object
- #update_attributes!(attrs) ⇒ Object
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
#instance ⇒ Object
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_version ⇒ Object
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_name ⇒ Object
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_class ⇒ Object
40 41 42 |
# File 'lib/platform/api/proxy/base.rb', line 40 def self.instance_class @klass 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
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 |