Sha256: 714fbe393539730bb172497402aa5b55eaafd15fd3208f8d12b65a0d5b150b1c

Contents?: true

Size: 1.44 KB

Versions: 35

Compression:

Stored size: 1.44 KB

Contents

module Maestrano
  module API
    class Resource < Maestrano::API::Object
      def self.class_name
        self.name.split('::').reject { |w| w.to_s == "Maestrano" }
      end

      def self.url
        if self == Maestrano::API::Resource
          raise NotImplementedError.new('Maestrano::API::Resource is an abstract class.  You should perform actions on its subclasses (Bill, Customer, etc.)')
        end
        if class_name.is_a?(Array)
          class_name.map { |w| CGI.escape(self.underscore(w)) }.join("/") + 's'
        else
          "#{CGI.escape(self.underscore(class_name))}s"
        end
      end

      def url
        unless id = self['id']
          raise Maestrano::API::Error::InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
        end
        "#{self.class.url}/#{CGI.escape(id)}"
      end

      def refresh
        response, api_token = Maestrano::API::Operation::Base.request(:get, url, @api_token, @retrieve_options)
        refresh_from(response, api_token)
        self
      end

      def self.retrieve(id, api_token=nil)
        instance = self.new(id, api_token)
        instance.refresh
        instance
      end
      
      def self.underscore(string_val)
        string_val.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
maestrano-1.0.6 lib/maestrano/api/resource.rb
maestrano-1.0.5 lib/maestrano/api/resource.rb
maestrano-1.0.4 lib/maestrano/api/resource.rb
maestrano-1.0.3 lib/maestrano/api/resource.rb
maestrano-1.0.2 lib/maestrano/api/resource.rb
maestrano-1.0.1 lib/maestrano/api/resource.rb
maestrano-1.0.0 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC9 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC8 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC7 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC6 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC5 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC4 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC3 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC2 lib/maestrano/api/resource.rb
maestrano-1.0.0.pre.RC1 lib/maestrano/api/resource.rb
maestrano-0.12.5 lib/maestrano/api/resource.rb
maestrano-0.12.4 lib/maestrano/api/resource.rb
maestrano-0.12.3 lib/maestrano/api/resource.rb
maestrano-0.12.2 lib/maestrano/api/resource.rb