Permalink
Browse files

Switching to rest's post_file and cleanup.

  • Loading branch information...
1 parent 8e81980 commit 518f29b9b12c21743747ebec8d183cb3b8a0b5a2 Andrew Kirilenko committed Jul 4, 2012
View
@@ -1,9 +1,8 @@
source 'http://rubygems.org'
-gem 'rest-client'
-gem 'rest'
-gem 'bundler', '> 1.0.0'
+gem 'rest', '> 0.3.0'
group :development do
+ gem 'rake'
gem 'jeweler2'
end
View
@@ -5,6 +5,7 @@ GEM
jeweler2 (2.0.9)
git (>= 1.2.5)
mime-types (1.19)
+ rake (0.9.2.2)
rest (1.1.0)
rest-client (>= 0.3.0)
rest-client (1.6.7)
@@ -14,7 +15,6 @@ PLATFORMS
ruby
DEPENDENCIES
- bundler (> 1.0.0)
jeweler2
- rest
- rest-client
+ rake
+ rest (> 0.3.0)
View
@@ -1 +1 @@
-0.1.6
+0.2.0
View
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = "iron_core"
- s.version = "0.1.6"
+ s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Andrew Kirilenko", "Iron.io, Inc"]
@@ -20,9 +20,9 @@ Gem::Specification.new do |s|
"VERSION",
"lib/iron_core.rb",
"lib/iron_core/client.rb",
- "lib/iron_core/iron_error.rb",
- "lib/iron_core/iron_response_error.rb",
+ "lib/iron_core/error.rb",
"lib/iron_core/logger.rb",
+ "lib/iron_core/response_error.rb",
"lib/iron_core/version.rb"
]
s.homepage = "https://github.com/iron-io/iron_core_ruby"
@@ -34,20 +34,17 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<rest-client>, [">= 0"])
- s.add_runtime_dependency(%q<rest>, [">= 0"])
- s.add_runtime_dependency(%q<bundler>, ["> 1.0.0"])
+ s.add_runtime_dependency(%q<rest>, ["> 0.3.0"])
+ s.add_development_dependency(%q<rake>, [">= 0"])
s.add_development_dependency(%q<jeweler2>, [">= 0"])
else
- s.add_dependency(%q<rest-client>, [">= 0"])
- s.add_dependency(%q<rest>, [">= 0"])
- s.add_dependency(%q<bundler>, ["> 1.0.0"])
+ s.add_dependency(%q<rest>, ["> 0.3.0"])
+ s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<jeweler2>, [">= 0"])
end
else
- s.add_dependency(%q<rest-client>, [">= 0"])
- s.add_dependency(%q<rest>, [">= 0"])
- s.add_dependency(%q<bundler>, ["> 1.0.0"])
+ s.add_dependency(%q<rest>, ["> 0.3.0"])
+ s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<jeweler2>, [">= 0"])
end
end
View
@@ -1,37 +1,49 @@
-require 'rest-client'
require 'rest'
require 'json'
-require_relative 'iron_response_error'
+require_relative 'response_error'
module IronCore
class Client
- attr_accessor :token
- attr_accessor :project_id
+ attr_accessor :headers
+ attr_accessor :content_type
- attr_accessor :scheme
- attr_accessor :host
- attr_accessor :port
- attr_accessor :api_version
- attr_accessor :user_agent
+ def initialize(company, product, options = {}, default_options = {}, extra_options_list = [])
+ @options_list = [:scheme, :host, :port, :user_agent, :http_gem] + extra_options_list
- def initialize(product, options = {}, extra_options_list = [])
- @options_list = [:token, :project_id, :scheme, :host, :port, :api_version, :user_agent] + extra_options_list
+ metaclass = class << self
+ self
+ end
+
+ @options_list.each do |option|
+ metaclass.send(:define_method, option.to_s) do
+ instance_variable_get('@' + option.to_s)
+ end
+
+ metaclass.send(:define_method, option.to_s + '=') do |value|
+ instance_variable_set('@' + option.to_s, value)
+ end
+ end
load_from_hash('params', options)
- load_from_config(product, options[:config_file] || options['config_file'])
- load_from_config(product, '.iron.json')
- 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() # :gem=>:rest_client - can set specific backend if we want. Should allow user to set this too.
+ load_from_config(company, product, options[:config_file] || options['config_file'])
+ load_from_config(company, product, ".#{company}.json")
+ load_from_config(company, product, "#{company}.json")
+ load_from_env(company.upcase + '_' + product.upcase)
+ load_from_env(company.upcase)
+ load_from_config(company, product, "~/.#{company}.json")
+ load_from_hash('defaults', default_options)
+ load_from_hash('defaults', {:user_agent => 'iron_core_ruby-' + IronCore.version, :http_gem => :rest_client})
+
+ @headers = {}
+ @content_type = 'application/json'
+
+ @rest = Rest::Client.new(:gem => @http_gem.to_sym)
end
def set_option(source, name, value)
if send(name.to_s).nil? && (not value.nil?)
- IronCore::Logger.debug 'IronCore', "Setting #{name} to #{value} from #{source}"
+ IronCore::Logger.debug 'IronCore', "Setting #{name} to '#{value}' from #{source}"
send(name.to_s + '=', value)
end
@@ -51,43 +63,39 @@ def load_from_env(prefix)
end
end
- def load_from_config(product, config_file)
+ def load_from_config(company, 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_file, config['iron_' + product])
- load_from_hash(config_file, config['iron'])
+ load_from_hash(config_file, config["#{company}_#{product}"])
+ load_from_hash(config_file, config[company])
load_from_hash(config_file, config)
end
end
- def options
+ def options(return_strings = false)
res = {}
- @options_list.each do |o|
- res[o.to_sym] = send(o.to_s)
+ @options_list.each do |option|
+ res[return_strings ? option.to_s : option.to_sym] = send(option.to_s)
end
res
end
def common_headers
- {
- 'Content-Type' => 'application/json',
- 'Authorization' => "OAuth #{@token}",
- 'User-Agent' => @user_agent
- }
+ {'User-Agent' => @user_agent}
end
def url
- "#{scheme}://#{host}:#{port}/#{api_version}/"
+ "#{scheme}://#{host}:#{port}/"
end
def get(method, params = {})
request_hash = {}
- request_hash[:headers] = common_headers
+ request_hash[:headers] = common_headers.merge(@headers)
request_hash[:params] = params
IronCore::Logger.debug 'IronCore', "GET #{url + method} with params='#{request_hash.to_s}'"
@@ -97,7 +105,7 @@ def get(method, params = {})
def post(method, params = {})
request_hash = {}
- request_hash[:headers] = common_headers
+ request_hash[:headers] = common_headers.merge(@headers).merge({'Content-Type' => @content_type})
request_hash[:body] = params.to_json
IronCore::Logger.debug 'IronCore', "POST #{url + method} with params='#{request_hash.to_s}'"
@@ -107,7 +115,7 @@ def post(method, params = {})
def put(method, params={})
request_hash = {}
- request_hash[:headers] = common_headers
+ request_hash[:headers] = common_headers.merge(@headers).merge({'Content-Type' => @content_type})
request_hash[:body] = params.to_json
IronCore::Logger.debug 'IronCore', "PUT #{url + method} with params='#{request_hash.to_s}'"
@@ -117,41 +125,33 @@ def put(method, params={})
def delete(method, params = {})
request_hash = {}
- request_hash[:headers] = common_headers
+ request_hash[:headers] = common_headers.merge(@headers)
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 = {})
+ def post_file(method, file_field, file, params_field, params = {})
request_hash = {}
- request_hash[:data] = params.to_json
- request_hash[:file] = file
+ request_hash[:headers] = common_headers.merge(@headers)
+ request_hash[:body] = {params_field => params.to_json, file_field => file}
- IronCore::Logger.debug 'IronCore', "POST #{url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"
+ IronCore::Logger.debug 'IronCore', "POST #{url + method} with params='#{request_hash.to_s}'"
- begin
- RestClient.post(url + method + "?oauth=#{@token}", request_hash)
- rescue RestClient::Unauthorized => e
- raise IronCore::IronResponseError.new(e.response)
- end
+ @rest.post_file(url + method, request_hash)
end
def parse_response(response, parse_json = true)
IronCore::Logger.debug 'IronCore', "GOT #{response.code} with params='#{response.body}'"
- raise IronCore::IronResponseError.new(response) if response.code != 200
+ raise IronCore::ResponseError.new(response) if response.code != 200
- # response in rest_client gem is a confusing object, of class String,
- # but with 'to_i' redefined to return response code and
- # 'body' defined to return itself
body = String.new(response.body)
return body unless parse_json
+
JSON.parse(body)
end
end
View
@@ -0,0 +1,4 @@
+module IronCore
+ class Error < StandardError
+ end
+end
@@ -1,4 +0,0 @@
-module IronCore
- class IronError < StandardError
- end
-end
View
@@ -15,24 +15,40 @@ def self.logger=(logger)
@logger = logger
end
- def self.fatal(product, msg)
+ def self.fatal(product, msg, exception_class = nil)
self.logger.fatal(product) { msg }
+
+ self.raise_exception(msg, exception_class)
end
- def self.error(product, msg)
+ def self.error(product, msg, exception_class = nil)
self.logger.error(product) { msg }
+
+ self.raise_exception(msg, exception_class)
end
- def self.warn(product, msg)
+ def self.warn(product, msg, exception_class = nil)
self.logger.warn(product) { msg }
+
+ self.raise_exception(msg, exception_class)
end
- def self.info(product, msg)
+ def self.info(product, msg, exception_class = nil)
self.logger.info(product) { msg }
+
+ self.raise_exception(msg, exception_class)
end
- def self.debug(product, msg)
+ def self.debug(product, msg, exception_class = nil)
self.logger.debug(product) { msg }
+
+ self.raise_exception(msg, exception_class)
+ end
+
+ def self.raise_exception(msg, exception_class)
+ unless exception_class.nil?
+ raise excpetion_classs.new(msg)
+ end
end
end
end
@@ -1,7 +1,7 @@
-require_relative 'iron_error'
+require_relative 'error'
module IronCore
- class IronResponseError < IronCore::IronError
+ class ResponseError < IronCore::Error
def initialize(response)
super(response.body)

0 comments on commit 518f29b

Please sign in to comment.