iron_core 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.2.0
@@ -1,37 +1,51 @@
1
- require 'rest-client'
2
1
  require 'rest'
3
2
  require 'json'
4
3
 
5
- require_relative 'iron_response_error'
4
+ require_relative 'response_error'
6
5
 
7
6
  module IronCore
8
7
  class Client
9
- attr_accessor :token
10
- attr_accessor :project_id
8
+ attr_accessor :content_type
11
9
 
12
- attr_accessor :scheme
13
- attr_accessor :host
14
- attr_accessor :port
15
- attr_accessor :api_version
16
- attr_accessor :user_agent
10
+ def initialize(company, product, options = {}, default_options = {}, extra_options_list = [])
11
+ @options_list = [:scheme, :host, :port, :user_agent, :http_gem] + extra_options_list
17
12
 
18
- def initialize(product, options = {}, extra_options_list = [])
19
- @options_list = [:token, :project_id, :scheme, :host, :port, :api_version, :user_agent] + extra_options_list
13
+ metaclass = class << self
14
+ self
15
+ end
16
+
17
+ @options_list.each do |option|
18
+ metaclass.send(:define_method, option.to_s) do
19
+ instance_variable_get('@' + option.to_s)
20
+ end
21
+
22
+ metaclass.send(:define_method, option.to_s + '=') do |value|
23
+ instance_variable_set('@' + option.to_s, value)
24
+ end
25
+ end
20
26
 
21
27
  load_from_hash('params', options)
22
- load_from_config(product, options[:config_file] || options['config_file'])
23
- load_from_config(product, '.iron.json')
24
- load_from_config(product, 'iron.json')
25
- load_from_env('IRON_' + product.upcase)
26
- load_from_env('IRON')
27
- load_from_config(product, '~/.iron.json')
28
-
29
- @rest = Rest::Client.new() # :gem=>:rest_client - can set specific backend if we want. Should allow user to set this too.
28
+ load_from_config(company, product, options[:config] || options['config'])
29
+ load_from_config(company, product, ENV[company.upcase + '_' + product.upcase + '_CONFIG'])
30
+ load_from_config(company, product, ENV[company.upcase + '_CONFIG'])
31
+ load_from_env(company.upcase + '_' + product.upcase)
32
+ load_from_env(company.upcase)
33
+ load_from_config(company, product, ".#{company}.json")
34
+ load_from_config(company, product, "#{company}.json")
35
+ load_from_config(company, product, "~/.#{company}.json")
36
+ load_from_hash('defaults', default_options)
37
+ load_from_hash('defaults', {:user_agent => 'iron_core_ruby-' + IronCore.version})
38
+
39
+ @content_type = 'application/json'
40
+
41
+ http_gem = @http_gem.nil? ? nil : @http_gem.to_sym
42
+
43
+ @rest = Rest::Client.new(:gem => http_gem)
30
44
  end
31
45
 
32
46
  def set_option(source, name, value)
33
47
  if send(name.to_s).nil? && (not value.nil?)
34
- IronCore::Logger.debug 'IronCore', "Setting #{name} to #{value} from #{source}"
48
+ IronCore::Logger.debug 'IronCore', "Setting #{name} to '#{value}' from #{source}"
35
49
 
36
50
  send(name.to_s + '=', value)
37
51
  end
@@ -51,43 +65,39 @@ module IronCore
51
65
  end
52
66
  end
53
67
 
54
- def load_from_config(product, config_file)
68
+ def load_from_config(company, product, config_file)
55
69
  return if config_file.nil?
56
70
 
57
71
  if File.exists?(File.expand_path(config_file))
58
72
  config = JSON.load(File.read(File.expand_path(config_file)))
59
73
 
60
- load_from_hash(config_file, config['iron_' + product])
61
- load_from_hash(config_file, config['iron'])
74
+ load_from_hash(config_file, config["#{company}_#{product}"])
75
+ load_from_hash(config_file, config[company])
62
76
  load_from_hash(config_file, config)
63
77
  end
64
78
  end
65
79
 
66
- def options
80
+ def options(return_strings = false)
67
81
  res = {}
68
82
 
69
- @options_list.each do |o|
70
- res[o.to_sym] = send(o.to_s)
83
+ @options_list.each do |option|
84
+ res[return_strings ? option.to_s : option.to_sym] = send(option.to_s)
71
85
  end
72
86
 
73
87
  res
74
88
  end
75
89
 
76
- def common_headers
77
- {
78
- 'Content-Type' => 'application/json',
79
- 'Authorization' => "OAuth #{@token}",
80
- 'User-Agent' => @user_agent
81
- }
90
+ def headers
91
+ {'User-Agent' => @user_agent}
82
92
  end
83
93
 
84
94
  def url
85
- "#{scheme}://#{host}:#{port}/#{api_version}/"
95
+ "#{scheme}://#{host}:#{port}/"
86
96
  end
87
97
 
88
98
  def get(method, params = {})
89
99
  request_hash = {}
90
- request_hash[:headers] = common_headers
100
+ request_hash[:headers] = headers
91
101
  request_hash[:params] = params
92
102
 
93
103
  IronCore::Logger.debug 'IronCore', "GET #{url + method} with params='#{request_hash.to_s}'"
@@ -97,7 +107,7 @@ module IronCore
97
107
 
98
108
  def post(method, params = {})
99
109
  request_hash = {}
100
- request_hash[:headers] = common_headers
110
+ request_hash[:headers] = headers.merge({'Content-Type' => @content_type})
101
111
  request_hash[:body] = params.to_json
102
112
 
103
113
  IronCore::Logger.debug 'IronCore', "POST #{url + method} with params='#{request_hash.to_s}'"
@@ -107,7 +117,7 @@ module IronCore
107
117
 
108
118
  def put(method, params={})
109
119
  request_hash = {}
110
- request_hash[:headers] = common_headers
120
+ request_hash[:headers] = headers.merge({'Content-Type' => @content_type})
111
121
  request_hash[:body] = params.to_json
112
122
 
113
123
  IronCore::Logger.debug 'IronCore', "PUT #{url + method} with params='#{request_hash.to_s}'"
@@ -117,7 +127,7 @@ module IronCore
117
127
 
118
128
  def delete(method, params = {})
119
129
  request_hash = {}
120
- request_hash[:headers] = common_headers
130
+ request_hash[:headers] = headers
121
131
  request_hash[:params] = params
122
132
 
123
133
  IronCore::Logger.debug 'IronCore', "DELETE #{url + method} with params='#{request_hash.to_s}'"
@@ -125,34 +135,32 @@ module IronCore
125
135
  @rest.delete(url + method, request_hash)
126
136
  end
127
137
 
128
- # FIXME: retries support
129
- # FIXME: user agent support
130
- def post_file(method, file, params = {})
138
+ def post_file(method, file_field, file, params_field, params = {})
131
139
  request_hash = {}
132
- request_hash[:data] = params.to_json
133
- request_hash[:file] = file
140
+ request_hash[:headers] = headers
141
+ request_hash[:body] = {params_field => params.to_json, file_field => file}
134
142
 
135
- IronCore::Logger.debug 'IronCore', "POST #{url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"
143
+ IronCore::Logger.debug 'IronCore', "POST #{url + method} with params='#{request_hash.to_s}'"
136
144
 
137
- begin
138
- RestClient.post(url + method + "?oauth=#{@token}", request_hash)
139
- rescue RestClient::Unauthorized => e
140
- raise IronCore::IronResponseError.new(e.response)
141
- end
145
+ @rest.post_file(url + method, request_hash)
142
146
  end
143
147
 
144
148
  def parse_response(response, parse_json = true)
145
149
  IronCore::Logger.debug 'IronCore', "GOT #{response.code} with params='#{response.body}'"
146
150
 
147
- raise IronCore::IronResponseError.new(response) if response.code != 200
151
+ raise IronCore::ResponseError.new(response) if response.code != 200
148
152
 
149
- # response in rest_client gem is a confusing object, of class String,
150
- # but with 'to_i' redefined to return response code and
151
- # 'body' defined to return itself
152
153
  body = String.new(response.body)
153
154
 
154
155
  return body unless parse_json
156
+
155
157
  JSON.parse(body)
156
158
  end
159
+
160
+ def check_id(id, name = 'id', length = 24)
161
+ if (not id.is_a?(String)) || id.length != length
162
+ IronCore::Logger.error 'IronCore', "Expecting #{length} symbol #{name} string", IronCore::Error
163
+ end
164
+ end
157
165
  end
158
166
  end
@@ -0,0 +1,4 @@
1
+ module IronCore
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -15,24 +15,40 @@ module IronCore
15
15
  @logger = logger
16
16
  end
17
17
 
18
- def self.fatal(product, msg)
18
+ def self.fatal(product, msg, exception_class = nil)
19
19
  self.logger.fatal(product) { msg }
20
+
21
+ self.raise_exception(msg, exception_class)
20
22
  end
21
23
 
22
- def self.error(product, msg)
24
+ def self.error(product, msg, exception_class = nil)
23
25
  self.logger.error(product) { msg }
26
+
27
+ self.raise_exception(msg, exception_class)
24
28
  end
25
29
 
26
- def self.warn(product, msg)
30
+ def self.warn(product, msg, exception_class = nil)
27
31
  self.logger.warn(product) { msg }
32
+
33
+ self.raise_exception(msg, exception_class)
28
34
  end
29
35
 
30
- def self.info(product, msg)
36
+ def self.info(product, msg, exception_class = nil)
31
37
  self.logger.info(product) { msg }
38
+
39
+ self.raise_exception(msg, exception_class)
32
40
  end
33
41
 
34
- def self.debug(product, msg)
42
+ def self.debug(product, msg, exception_class = nil)
35
43
  self.logger.debug(product) { msg }
44
+
45
+ self.raise_exception(msg, exception_class)
46
+ end
47
+
48
+ def self.raise_exception(msg, exception_class)
49
+ unless exception_class.nil?
50
+ raise exception_class.new(msg)
51
+ end
36
52
  end
37
53
  end
38
54
  end
@@ -1,7 +1,7 @@
1
- require_relative 'iron_error'
1
+ require_relative 'error'
2
2
 
3
3
  module IronCore
4
- class IronResponseError < IronCore::IronError
4
+ class ResponseError < IronCore::Error
5
5
  def initialize(response)
6
6
  super(response.body)
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,16 +10,16 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-04 00:00:00.000000000 Z
13
+ date: 2012-07-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rest-client
16
+ name: rest
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
- version: '0'
22
+ version: 2.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,16 +27,16 @@ dependencies:
27
27
  requirements:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
- version: '0'
30
+ version: 2.0.0
31
31
  - !ruby/object:Gem::Dependency
32
- name: rest
32
+ name: rake
33
33
  requirement: !ruby/object:Gem::Requirement
34
34
  none: false
35
35
  requirements:
36
36
  - - ! '>='
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
- type: :runtime
39
+ type: :development
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
42
42
  none: false
@@ -44,22 +44,6 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
- - !ruby/object:Gem::Dependency
48
- name: bundler
49
- requirement: !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>'
53
- - !ruby/object:Gem::Version
54
- version: 1.0.0
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ! '>'
61
- - !ruby/object:Gem::Version
62
- version: 1.0.0
63
47
  - !ruby/object:Gem::Dependency
64
48
  name: jeweler2
65
49
  requirement: !ruby/object:Gem::Requirement
@@ -87,9 +71,9 @@ files:
87
71
  - VERSION
88
72
  - lib/iron_core.rb
89
73
  - lib/iron_core/client.rb
90
- - lib/iron_core/iron_error.rb
91
- - lib/iron_core/iron_response_error.rb
74
+ - lib/iron_core/error.rb
92
75
  - lib/iron_core/logger.rb
76
+ - lib/iron_core/response_error.rb
93
77
  - lib/iron_core/version.rb
94
78
  homepage: https://github.com/iron-io/iron_core_ruby
95
79
  licenses: []
@@ -105,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
89
  version: '0'
106
90
  segments:
107
91
  - 0
108
- hash: 1936258264107303313
92
+ hash: -839868627
109
93
  required_rubygems_version: !ruby/object:Gem::Requirement
110
94
  none: false
111
95
  requirements:
@@ -1,4 +0,0 @@
1
- module IronCore
2
- class IronError < StandardError
3
- end
4
- end