Sha256: b73da396f6b511cef0639f293f2a1195abc782847643e72da1d62ed59d3c11ab

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Postly
  class Connection
    
    include HTTParty
    
    base_uri Postly.base_uri
    
    def initialize attrs={}
      attrs.each_pair {|k,v| send("#{k}=".to_sym, v) if respond_to? "#{k}=".to_sym }
    end
    
    def self.defaults
      { :basic_auth => {:username => Postly.config["username"], 
        :password => Postly.config["password"]} }
    end
    
    # quack! quack!
    def self.conform response
      result = array_or_hash response
       case result
       when Hash
         self.new result
       when Array
         result.collect { |item| self.new item }
       end
    end
    
    def self.array_or_hash response
      response["rsp"][self.class_name.downcase]
    end
    
    # Split off the Namespace
    def self.class_name
      @class_name ||= self.to_s.split("::").last
    end
    
    
    def self.many klass
      define_method klass do
        ManyProxy.new self, singularized_class_name(klass)
      end
    end
    
    def singularized_class_name klass
      klass.to_s.singularize.camelize.constantize
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postly-0.2.1 lib/postly/connection.rb