Permalink
Browse files

Basic ops using rest-client.

  • Loading branch information...
1 parent f503618 commit 2787ea11be517cb3ef1ed37af223a4c6f1ca58ec @treeder treeder committed Oct 6, 2011
Showing with 209 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +38 −0 Rakefile
  3. +5 −0 VERSION.yml
  4. +38 −0 ironmq.gemspec
  5. +1 −0 lib/ironmq.rb
  6. +92 −0 lib/ironmq/client.rb
  7. +30 −0 test/ironmq_tests.rb
View
@@ -0,0 +1,5 @@
+.DS_Store
+pkg
+doc
+.idea
+log
View
@@ -0,0 +1,38 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "ironmq"
+ gem.summary = "Ruby client for IronMQ"
+ gem.description = "Ruby client for IronMQ"
+ gem.email = "travis@iron.io"
+ gem.homepage = "http://www.iron.io"
+ gem.authors = ["Travis Reeder"]
+ gem.add_dependency 'rest-client'
+ gem.required_ruby_version = '>= 1.9'
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+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 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'doc'
+ rdoc.title = "beanstalk-client #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
@@ -0,0 +1,5 @@
+---
+:major: 1
+:minor: 1
+:patch: 3
+:build:
View
@@ -0,0 +1,38 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{ironmq}
+ s.version = "1.1.2"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = [%q{Keith Rarick}]
+ s.date = %q{2011-10-06}
+ s.description = %q{Ruby client for beanstalkd}
+ s.email = %q{kr@xph.us}
+ s.extra_rdoc_files = [
+ "README.markdown"
+ ]
+ s.files = [
+ "README.markdown",
+ "Rakefile",
+ "VERSION.yml",
+ "lib/ironmq.rb"
+ ]
+ s.homepage = %q{http://github.com/kr/beanstalk-client-ruby}
+ s.require_paths = [%q{lib}]
+ s.rubygems_version = %q{1.8.5}
+ s.summary = %q{Ruby client for beanstalkd}
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
+
View
@@ -0,0 +1 @@
+require_relative 'ironmq/client'
View
@@ -0,0 +1,92 @@
+require 'json'
+require 'rest_client'
+
+
+module IronMQ
+
+ class Client
+
+ attr_accessor :token, :project_id, :queue_name
+
+ def initialize(options={})
+ @token = options[:token] || options['token']
+ @project_id = options[:project_id] || options['project_id']
+ @queue_name = options[:queue_name] || options['queue_name']
+ @scheme = options[:scheme] || options['scheme'] || "http"
+ @host = options[:host] || options['host'] || "mq-aws-us-east-1.iron.io"
+ @port = options[:port] || options['port'] || 80
+ # todo: default https
+ @base_url = "#{@scheme}://#{@host}:#{@port}/1"
+ end
+
+ def messages
+ return Messages.new(self)
+ end
+
+
+ def post(path, params={})
+ url = "#{@base_url}#{path}"
+ puts 'url=' + url
+ response = RestClient.post(url, {'oauth' => @token}.merge(params),
+ :content_type => :json)
+ puts 'response: ' + response.inspect
+ res = JSON.parse(response)
+ res
+ end
+
+ def get(path, params={})
+ url = "#{@base_url}#{path}"
+ puts 'url=' + url
+ response = RestClient.get(url,
+ {:params => {'oauth'=>@token}.merge(params)})
+ res = JSON.parse(response)
+ res
+ end
+
+ def delete(path, params={})
+ url = "#{@base_url}#{path}"
+ puts 'url=' + url
+ response = RestClient.delete(url,
+ {:params => {'oauth'=>@token}.merge(params)})
+ res = JSON.parse(response)
+ puts 'response: ' + res.inspect
+ res
+ end
+
+ end
+
+ class Messages
+
+ attr_accessor :client
+
+ def initialize(client)
+ @client = client
+ end
+
+
+ # options:
+ # :queue_name => can specify an alternative queue name
+ def get(options={})
+ path = "/projects/#{@client.project_id}/queues/#{options[:queue_name] || @client.queue_name}/messages"
+ res = @client.get(path)
+ res
+ end
+
+ # options:
+ # :queue_name => can specify an alternative queue name
+ def post(payload, options={})
+ path = "/projects/#{@client.project_id}/queues/#{options[:queue_name] || @client.queue_name}/messages"
+ res = @client.post(path, :payload=>payload)
+ res
+ end
+
+ def delete(message_id, options={})
+ path = "/projects/#{@client.project_id}/queues/#{options[:queue_name] || @client.queue_name}/messages/#{message_id}"
+ res = @client.delete(path)
+ res
+ end
+
+ end
+
+end
+
View
@@ -0,0 +1,30 @@
+gem 'test-unit'
+require 'test/unit'
+require 'yaml'
+begin
+ require File.join(File.dirname(__FILE__), '../lib/ironmq')
+rescue Exception => ex
+ puts "Could NOT load current ironmq: " + ex.message
+ raise ex
+end
+
+class IronMQTests < Test::Unit::TestCase
+ def setup
+ puts 'setup'
+ @config = YAML::load_file(File.join(File.dirname(__FILE__), "config.yml"))
+ @client = IronMQ::Client.new(@config['ironmq'])
+ @client.queue_name = 'ironmq-gem-tests'
+ end
+
+ def test_basics
+ res = @client.messages.post("hello world!")
+ p res
+
+ res = @client.messages.get()
+ p res
+
+ res = @client.messages.delete(res["id"])
+ p res
+ end
+end
+

0 comments on commit 2787ea1

Please sign in to comment.