Sha256: 2dabf5339c3ee0f4c4fe11f259421ef713d86a81aec07c10bf28c21c97661730

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require 'hashie'
require 'httparty'
require 'cic/errors'
require 'active_support/inflector'

module ActiveCic
  class Base
    attr_accessor :attributes
    attr_reader :raw_attributes

    BASE_URL = "api.nl.cic.mx/0/nl"

    include HTTParty

    base_uri BASE_URL

    def initialize(hash)
      self.attributes = Hashie::Mash.new(hash) 
      @raw_attributes = hash
    end

    def method_missing(name, *args)
      self.attributes.send(name, args)
    rescue NoMethodError
      super
    end

    def self.all
      class_name = self.pluralize_child_class_name
      response = self.get("/#{class_name}.json") 
      if response.success?
        response.parsed_response[class_name].map { |hash| self.new(hash) } 
      else
        raise_exception(response.code, response.body)
      end
    end

    private

      def self.raise_exception(code, body)
        raise Cic::Exception::ServerError.new(code, body) if code >= 500
        raise Cic::Exception::ClientError.new(code, body) if code < 500
      end

      def self.pluralize_child_class_name
        self.to_s.split('::').last.downcase.pluralize
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cic-1.0.2 lib/cic/active_cic.rb
cic-1.0.1 lib/cic/active_cic.rb
cic-1.0.0 lib/cic/active_cic.rb