Sha256: b324f828696ce158ed143592e64877c88e49fa3f1bc67771f178066ce8774de5

Contents?: true

Size: 819 Bytes

Versions: 7

Compression:

Stored size: 819 Bytes

Contents

class Api::BaseApi
  
  attr_accessor :api_version
  attr_accessor :params
  
  API_VERSION_REGEX = /v[0-9]+(_[0-9]+)*/i

  def initialize(version=nil)
    set_api_version(version)
  end
  
  def render(data)
    respond_to?(api_version) ? self.send(api_version, data) : nil
  end
  
  def timestamps(json, data)
    json.id data.id.to_s
    json.created_at data.created_at
    json.updated_at data.updated_at
  end
  
  private
  def latest_version
    api_versions.sort.last
  end

  def api_versions
    @@api_versions ||= self.class.instance_methods.grep(API_VERSION_REGEX)
  end
  
  def api_version?(version)
    return false if version.nil?
    api_versions.include?(version.to_sym)
  end

  def set_api_version(version)
    self.api_version = (api_version?(version) ? version.to_sym : latest_version)
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
api_versioning-0.0.9 app/models/api/base_api.rb
api_versioning-0.0.8 app/models/api/base_api.rb
api_versioning-0.0.7 app/models/api/base_api.rb
api_versioning-0.0.6 app/models/api/base_api.rb
api_versioning-0.0.5 app/models/api/base_api.rb
api_versioning-0.0.4 app/models/api/base_api.rb
api_versioning-0.0.3 app/models/api/base_api.rb