Sha256: 43d9cf27aedb6a3ade62ff7d000357819c9a94f593ad8d2107524efe81087051

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module PulpSimple
  class Base
    attr_accessor :pulp_resource_name

    self.class::DEFINED_METHODS = {}

    def initialize(pulp_resource_name=nil)
      @pulp_resource_name = pulp_resource_name
    end

    def method_missing(meth, *args, &block)
      method_name = meth.to_s
      args = args.first
      if method_def = self.class::DEFINED_METHODS[method_name]
        rest_url = full_url(method_name, method_def)
        call_rest_method(method_name, rest_url, method_def[:http_method], args)
      else
        super
      end
    end

    protected

    def call_rest_method(method_name, rest_url, http_method, args)
      args = args.to_json if args
      begin
        JSON.parse RestClient.send(http_method, rest_url, args)
      rescue JSON::ParserError
        ''
      end
    end

    def short_class_name
      self.class.name.to_s.sub(/^.*::/, '')
    end

    def base_url
      "#{PulpSimple.base_url}/#{short_class_name.downcase}"
    end

    def full_url(method_name, method_def)
      url = base_url
      url += "/#{@pulp_resource_name}" if @pulp_resource_name
      url_method_name = method_def[:url_method_name] || method_name
      url += "/#{url_method_name}" unless (url_method_name.nil? || url_method_name.empty?)
      url += '/'
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pulp_simple-0.1.0 lib/pulp_simple/base.rb