Sha256: 4b4257303f856846ce70a11079821448d9549bec903ea0b1eed4531c6f6963bb

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

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

    def initialize(uuid=nil)
      self.session_uuid = uuid ? {'coinsetter-client-session-id' => uuid} : {}
      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 + [last.camelize(:lower)]).join('/')
    end

    def create(options={}, route=path, headers={})
      headers.merge!(session_uuid)
      self.response = Coinsetter::Net.post(route, options, headers)

      parse
    end

    def list(route=path, options={}, headers={})
      headers.merge!(session_uuid)
      self.response = Coinsetter::Net.get(route, options, headers)

      parse_collection
    end

    def get(id, route=path, options={}, headers={})
      headers.merge!(session_uuid)
      self.response = Coinsetter::Net.get("#{route}/#{id}", options, headers)

      parse
    end

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

    private
    def parse
      if response.include? "\"response\":\"error\""
        Coinsetter::Helper.parse_message! response
      else
        Coinsetter::Helper.parse_object! response, model
      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.9 lib/coinsetter/collection.rb
coinsetter-0.0.8 lib/coinsetter/collection.rb