Permalink
Browse files

Boom, basics work!

  • Loading branch information...
0 parents commit 3f00d1555932edfc0c82dc192ab13e9917dd598d @treeder treeder committed Feb 27, 2012
Showing with 567 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +56 −0 README.md
  3. +40 −0 Rakefile
  4. +5 −0 VERSION.yml
  5. +3 −0 lib/iron_cache.rb
  6. +80 −0 lib/iron_cache/caches.rb
  7. +152 −0 lib/iron_cache/client.rb
  8. +93 −0 lib/iron_cache/items.rb
  9. +4 −0 test/Gemfile
  10. +33 −0 test/quick_run.rb
  11. +52 −0 test/test_base.rb
  12. +42 −0 test/test_iron_cache.rb
@@ -0,0 +1,7 @@
+.DS_Store
+pkg/
+doc
+.idea/
+log
+test/config.yml
+*.sublime*
@@ -0,0 +1,56 @@
+IronMQ Ruby Client
+-------------
+
+Getting Started
+==============
+
+Install the gem:
+
+ gem install iron_cache
+
+Create an IronMQ client object:
+
+ @ironmq = IronCache::Client.new('token'=>'MYTOKEN', 'project_id'=>'MYPROJECTID')
+
+You can get your `token` and `project_id` at http://www.iron.io .
+
+
+The Basics
+=========
+
+**Push** a message on the queue:
+
+ msg = @ironmq.messages.post("hello world!")
+ p msg
+
+**Pop** a message off the queue:
+
+ msg = @ironmq.messages.get()
+ p msg
+
+When you pop/get a message from the queue, it will NOT be deleted. It will eventually go back onto the queue after
+a timeout if you don't delete it (default timeout is 10 minutes).
+
+**Delete** a message from the queue:
+
+ res = msg.delete # or @ironmq.messages.delete(msg["id"])
+ p res
+
+Delete a message from the queue when you're done with it.
+
+Queue Selection
+===============
+
+One of the following:
+
+1. Pass `:queue_name=>'my_queue'` into IronMQ::Client.new
+1. `@client.queue_name = 'my_queue'`
+1. Pass `:queue_name=>'my_queue'` into any post(), get(), or delete()
+
+Queue Information
+=================
+
+ queue = @client.queues.get(:name=>@client.queue_name)
+ puts "size: #{queue.size}"
+
+
@@ -0,0 +1,40 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler2'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "iron_cache"
+ gem.summary = "Ruby client for IronCache by www.iron.io"
+ gem.description = "Ruby client for IronCache by www.iron.io"
+ gem.email = "travis@iron.io"
+ gem.homepage = "http://www.iron.io"
+ gem.authors = ["Travis Reeder"]
+ gem.add_dependency 'rest-client'
+ gem.add_dependency 'rest', '>= 0.1.2'
+ #gem.add_dependency 'typhoeus'
+ gem.required_ruby_version = '>= 1.9'
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler2"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+task :default => :test
+
+require 'rdoc/task'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'doc'
+ rdoc.title = "iron_cache #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,5 @@
+---
+:major: 0
+:minor: 0
+:patch: 1
+:build:
@@ -0,0 +1,3 @@
+require_relative 'iron_cache/caches'
+require_relative 'iron_cache/items'
+require_relative 'iron_cache/client'
@@ -0,0 +1,80 @@
+module IronCache
+ class Caches
+
+ attr_accessor :client
+
+ def initialize(client)
+ @client = client
+ end
+
+ def path(options={})
+ path = "/projects/#{@client.project_id}/caches"
+ end
+
+ def list(options={})
+ ret = []
+ res, status = @client.get("#{path(options)}", options)
+ res.each do |q|
+ #p q
+ q = Cache.new(self, q)
+ ret << q
+ end
+ ret
+ end
+
+ # options:
+ # :name => can specify an alternative queue name
+ def get(options={})
+ res, status = @client.get("#{path(options)}/#{options[:name]}")
+ return Cache.new(self, res)
+ end
+
+
+ end
+
+ class Cache
+
+ def initialize(queues, res)
+ @queues = queues
+ @data = res
+ end
+
+ def raw
+ @data
+ end
+
+ def [](key)
+ raw[key]
+ end
+
+ def id
+ raw["id"]
+ end
+
+ def name
+ raw["name"]
+ end
+
+ def size
+ return raw["size"] if raw["size"]
+ return @size if @size
+ q = @queues.get(:name=>name)
+ @size = q.size
+ @size
+ end
+
+ def total_messages
+ return raw["total_messages"] if raw["total_messages"]
+ return @total_messages if @total_messages
+ q = @queues.get(:name=>name)
+ @total_messages = q.total_messages
+ @total_messages
+ end
+
+ # def delete
+ # @messages.delete(self.id)
+ # end
+ end
+
+end
+
@@ -0,0 +1,152 @@
+require 'json'
+require 'logger'
+require 'rest'
+
+module IronCache
+
+ class Client
+
+ attr_accessor :token, :project_id, :cache_name, :logger,
+ :scheme, :host, :port
+
+ def initialize(options={})
+ @logger = Logger.new(STDOUT)
+ @logger.level=Logger::INFO
+
+ @token = options[:token] || options['token']
+ @project_id = options[:project_id] || options['project_id']
+ @cache_name = options[:cache_name] || options['cache_name'] || "default"
+ @scheme = options[:scheme] || options['scheme'] || "https"
+ @host = options[:host] || options['host'] || "cache-aws-us-east-1.iron.io"
+ @port = options[:port] || options['port'] || 443
+
+ @base_url = "#{@scheme}://#{@host}:#{@port}/1"
+
+ @rest = Rest::Client.new
+
+ end
+
+ def items
+ return Items.new(self)
+ end
+
+ def cache
+
+ end
+
+ def caches
+ return Caches.new(self)
+ end
+
+ def base_url
+ #"#{scheme}://#{host}:#{port}/1"
+ @base_url
+ end
+
+
+ def full_url(path)
+ url = "#{base_url}#{path}"
+ url
+ end
+
+ def common_req_hash
+ {
+ :headers=>{"Content-Type" => 'application/json',
+ "Authorization"=>"OAuth #{@token}",
+ "User-Agent"=>"IronCache Ruby Client"}
+ }
+ end
+
+ def get(path, params={})
+ url = full_url(path)
+ @logger.debug 'url=' + url
+ req_hash = common_req_hash
+ req_hash[:params] = params if params
+ @logger.debug 'req_hash=' + req_hash.inspect
+ response = @rest.get(url, req_hash)
+ @logger.debug 'GET response=' + response.inspect
+ res = check_response(response)
+ return res, response.code
+ end
+
+ def post(path, params={})
+ url = full_url(path)
+ @logger.debug 'url=' + url
+ #response = @http_sess.post(path + "?oauth=#{@token}", {'oauth' => @token}.merge(params).to_json, {"Content-Type" => 'application/json'})
+ req_hash = common_req_hash
+ req_hash[:body] = params.to_json
+ response = @rest.post(url, req_hash)
+ @logger.debug 'POST response=' + response.inspect
+ res = check_response(response)
+ #@logger.debug 'response: ' + res.inspect
+ #body = response.body
+ #res = JSON.parse(body)
+ return res, response.code
+ end
+
+ def put(path, params={})
+ url = full_url(path)
+ @logger.debug 'url=' + url
+ #response = @http_sess.post(path + "?oauth=#{@token}", {'oauth' => @token}.merge(params).to_json, {"Content-Type" => 'application/json'})
+ req_hash = common_req_hash
+ req_hash[:body] = params.to_json
+ response = @rest.put(url, req_hash)
+ @logger.debug 'POST response=' + response.inspect
+ res = check_response(response)
+ #@logger.debug 'response: ' + res.inspect
+ #body = response.body
+ #res = JSON.parse(body)
+ return res, response.code
+ end
+
+
+
+ def delete(path, params={})
+ url = "#{base_url}#{path}"
+ @logger.debug 'url=' + url
+ req_hash = common_req_hash
+ req_hash[:params] = params
+ response = @rest.delete(url, req_hash)
+ res = check_response(response)
+ #body = response.body
+ #res = JSON.parse(body)
+ #@logger.debug 'response: ' + res.inspect
+ return res, response.code
+ end
+
+ def check_response(response)
+ # response.code # http status code
+ #response.time # time in seconds the request took
+ #response.headers # the http headers
+ #response.headers_hash # http headers put into a hash
+ #response.body # the response body
+ status = response.code
+ body = response.body
+ # todo: check content-type == application/json before parsing
+ @logger.debug "response code=" + status.to_s
+ @logger.debug "response body=" + body.inspect
+ res = JSON.parse(body)
+ if status < 400
+
+ else
+ raise IronCache::Error.new(res["msg"], :status=>status)
+ end
+ res
+ end
+
+ end
+
+ class Error < StandardError
+ def initialize(msg, options={})
+ super(msg)
+ @options = options
+ end
+
+ def status
+ @options[:status]
+ end
+ end
+
+
+end
+
Oops, something went wrong.

0 comments on commit 3f00d15

Please sign in to comment.