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-0.12.1 lib/maestrano/api/resource.rb
maestrano-0.12.0 lib/maestrano/api/resource.rb
maestrano-0.11.0 lib/maestrano/api/resource.rb
maestrano-0.10.0 lib/maestrano/api/resource.rb
maestrano-0.9.2 lib/maestrano/api/resource.rb
maestrano-0.9.1 lib/maestrano/api/resource.rb
maestrano-0.9.0 lib/maestrano/api/resource.rb
maestrano-ruby-test-0.8.3 lib/maestrano/api/resource.rb
maestrano-0.8.2 lib/maestrano/api/resource.rb
maestrano-0.8.1 lib/maestrano/api/resource.rb
maestrano-0.8.0 lib/maestrano/api/resource.rb
maestrano-0.7.0 lib/maestrano/api/resource.rb
maestrano-0.6.0 lib/maestrano/api/resource.rb
maestrano-0.5.0 lib/maestrano/api/resource.rb
maestrano-0.4.0 lib/maestrano/api/resource.rb