Sha256: 7d240a5191bb45bfcbcfd4c1b609547a2871475421fdf98d1f3248309c9f39b7

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module Coinsetter
  class Collection
    attr_accessor :module, :name, :model, :path, :response, :headers, :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.join('/'), last.camelize(:lower)].join('/')
    end

    def create(route=path, options={}, 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? "\"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

1 entries across 1 versions & 1 rubygems

Version Path
coinsetter-0.0.5 lib/coinsetter/collection.rb