Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial implementation.

  • Loading branch information...
commit 8a794440309e2211a04be0fdf2a855c5606c74be 0 parents
@iced iced authored
3  .gitignore
@@ -0,0 +1,3 @@
+.idea
+.bundle
+pkg
10 Gemfile
@@ -0,0 +1,10 @@
+source 'http://rubygems.org'
+
+gem 'rest-client'
+gem 'rest'
+gem 'json'
+gem 'bundler', '> 1.0.0'
+
+group :development do
+ gem 'jeweler', '~> 1.8.3'
+end
29 Gemfile.lock
@@ -0,0 +1,29 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ git (1.2.5)
+ jeweler (1.8.3)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ json (1.6.6)
+ mime-types (1.18)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rest (0.2.0)
+ rest-client
+ rest-client
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (> 1.0.0)
+ jeweler (~> 1.8.3)
+ json
+ rest
+ rest-client
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Iron.io, Inc
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rubygems'
+require 'bundler'
+require 'jeweler'
+
+Jeweler::Tasks.new do |gem|
+ begin
+ Bundler.setup(:default, :development)
+ rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+ end
+
+ gem.name = "iron_core"
+ gem.homepage = "https://github.com/iron-io/iron_core_ruby"
+ gem.description = %Q{Core library for Iron products}
+ gem.summary = %Q{Core library for Iron products}
+ gem.email = "info@iron.io"
+ gem.authors = ["Andrew Kirilenko", "Iron.io, Inc"]
+ gem.files.exclude('.document', 'Gemfile', 'Gemfile.lock', 'Rakefile', 'iron_core.gemspec')
+end
+
+Jeweler::RubygemsDotOrgTasks.new
1  VERSION
@@ -0,0 +1 @@
+0.1.0
3  lib/iron_core.rb
@@ -0,0 +1,3 @@
+require_relative 'iron_core/version'
+require_relative 'iron_core/logger'
+require_relative 'iron_core/client'
136 lib/iron_core/client.rb
@@ -0,0 +1,136 @@
+require 'rest-client'
+require 'rest'
+require 'json'
+
+require_relative 'iron_error'
+
+module IronCore
+ class Client
+ attr_accessor :token
+ attr_accessor :project_id
+
+ attr_accessor :scheme
+ attr_accessor :host
+ attr_accessor :port
+ attr_accessor :api_version
+ attr_accessor :user_agent
+
+ def initialize(product, options = {}, extra_options_list = [])
+ @options_list = [:token, :project_id, :scheme, :host, :port, :api_version, :user_agent] + extra_options_list
+
+ load_from_hash(options)
+ load_from_config(product, options[:config_file] || options['config_file'])
+ load_from_config(product, 'iron.json')
+ load_from_env('IRON_' + product.upcase)
+ load_from_env('IRON')
+ load_from_config(product, '~/.iron.json')
+
+ @rest = Rest::Client.new
+ end
+
+ def set_option(name, value)
+ if send(name.to_s).nil?
+ send(name.to_s + '=', value)
+ end
+ end
+
+ def load_from_hash(hash)
+ return if hash.nil?
+
+ @options_list.each do |o|
+ set_option(o, hash[o.to_sym] || hash[o.to_s])
+ end
+ end
+
+ def load_from_env(prefix)
+ @options_list.each do |o|
+ set_option(o, ENV[prefix + '_' + o.to_s.upcase])
+ end
+ end
+
+ def load_from_config(product, config_file)
+ return if config_file.nil?
+
+ if File.exists?(File.expand_path(config_file))
+ config = JSON.load(File.read(File.expand_path(config_file)))
+
+ load_from_hash(config['iron_' + product])
+ load_from_hash(config['iron'])
+ load_from_hash(config)
+ end
+ end
+
+ def options
+ res = {}
+
+ @options_list.each do |o|
+ res[o.to_sym] = send(o.to_s)
+ end
+
+ res
+ end
+
+ def common_request_hash
+ {
+ 'Content-Type' => 'application/json',
+ 'Authorization' => "OAuth #{@token}",
+ 'User-Agent' => @user_agent
+ }
+ end
+
+ def url
+ "#{scheme}://#{host}:#{port}/#{api_version}/"
+ end
+
+ def get(method, params = {})
+ request_hash = {}
+ request_hash[:headers] = common_request_hash
+ request_hash[:params] = params
+
+ IronCore::Logger.debug 'IronCore', "GET #{url + method} with params='#{request_hash.to_s}'"
+
+ @rest.get(url + method, request_hash)
+ end
+
+ def post(method, params = {})
+ request_hash = {}
+ request_hash[:headers] = common_request_hash
+ request_hash[:body] = params.to_json
+
+ IronCore::Logger.debug 'IronCore', "POST #{url + method} with params='#{request_hash.to_s}'"
+
+ @rest.post(url + method, request_hash)
+ end
+
+ def delete(method, params = {})
+ request_hash = {}
+ request_hash[:headers] = common_request_hash
+ request_hash[:params] = params
+
+ IronCore::Logger.debug 'IronCore', "DELETE #{url + method} with params='#{request_hash.to_s}'"
+
+ @rest.delete(url + method, request_hash)
+ end
+
+ # FIXME: retries support
+ # FIXME: user agent support
+ def post_file(method, file, params = {})
+ request_hash = {}
+ request_hash[:data] = params.to_json
+ request_hash[:file] = file
+
+ IronCore::Logger.debug 'IronCore', "POST #{url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"
+
+ RestClient.post(url + method + "?oauth=#{@token}", request_hash)
+ end
+
+ def parse_response(response, parse_json = true)
+ IronCore::Logger.debug 'IronCore', "GOT #{response.code} with params='#{response.body}'"
+
+ raise IronCore::IronError.new(response.body) if response.code != 200
+
+ return response.body unless parse_json
+ JSON.parse(response.body)
+ end
+ end
+end
4 lib/iron_core/iron_error.rb
@@ -0,0 +1,4 @@
+module IronCore
+ class IronError < Exception
+ end
+end
38 lib/iron_core/logger.rb
@@ -0,0 +1,38 @@
+require 'logger'
+
+module IronCore
+ module Logger
+ def self.logger
+ unless @logger
+ @logger = ::Logger.new(STDOUT)
+ @logger.level = ::Logger::INFO
+ end
+
+ @logger
+ end
+
+ def self.logger=(logger)
+ @logger = logger
+ end
+
+ def self.fatal(product, msg)
+ self.logger.fatal(product) { msg }
+ end
+
+ def self.error(product, msg)
+ self.logger.error(product) { msg }
+ end
+
+ def self.warn(product, msg)
+ self.logger.warn(product) { msg }
+ end
+
+ def self.info(product, msg)
+ self.logger.info(product) { msg }
+ end
+
+ def self.debug(product, msg)
+ self.logger.debug(product) { msg }
+ end
+ end
+end
7 lib/iron_core/version.rb
@@ -0,0 +1,7 @@
+module IronCore
+ @@version = nil
+
+ def self.version
+ @@version ||= File.read(File.dirname(__FILE__) + '/../../VERSION').strip
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.