Sha256: ddd0886c0a7719866b5d2aa3dbcf97bbd68f22552c33cd45c05f7c0f4b8092ca

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Coinsetter
  class Collection
    attr_accessor :module, :name, :model, :path, :response

    def initialize
      self.module = self.class.name.singularize.underscore
      split_module = self.module.split('/')
      split_module.shift
      self.name   = split_module.join('/')
      self.model  = self.module.camelize.constantize
      last = split_module.pop
      self.path   = split_module.join('/') + last.camelize(:lower)
    end

    def create(options={}, route=path)
      if Coinsetter.configured?
        self.response = Coinsetter::Net.post(route, options)
      else
        self.response = example
      end

      parse
    end

    def list(route=path, options={})
      if Coinsetter.configured?
        self.response = Coinsetter::Net.get(route, options)
      else
        self.response = example
      end

      parse_collection
    end

    def get(id, route=path, options={})
      self.response = Coinsetter::Net.get("#{route}/#{id}", options)
      parse
    end

    def example
      JSON.generate({error: "Something went wrong."})
    end

    private
    def parse
      @parsed ||=
        if response.include? "\"requestStatus\":\"SUCCESS\""
          Coinsetter::Helper.parse_object! response, model
        else
          Coinsetter::Helper.parse_message! response
        end
    end

    def parse_collection
      @collection ||= Coinsetter::Helper.parse_objects! response, model
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
coinsetter-0.0.4 lib/coinsetter/collection.rb
coinsetter-0.0.3 lib/coinsetter/collection.rb