iron_core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Iron.io, Inc
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,136 @@
1
+ require 'rest-client'
2
+ require 'rest'
3
+ require 'json'
4
+
5
+ require_relative 'iron_error'
6
+
7
+ module IronCore
8
+ class Client
9
+ attr_accessor :token
10
+ attr_accessor :project_id
11
+
12
+ attr_accessor :scheme
13
+ attr_accessor :host
14
+ attr_accessor :port
15
+ attr_accessor :api_version
16
+ attr_accessor :user_agent
17
+
18
+ def initialize(product, options = {}, extra_options_list = [])
19
+ @options_list = [:token, :project_id, :scheme, :host, :port, :api_version, :user_agent] + extra_options_list
20
+
21
+ load_from_hash(options)
22
+ load_from_config(product, options[:config_file] || options['config_file'])
23
+ load_from_config(product, 'iron.json')
24
+ load_from_env('IRON_' + product.upcase)
25
+ load_from_env('IRON')
26
+ load_from_config(product, '~/.iron.json')
27
+
28
+ @rest = Rest::Client.new
29
+ end
30
+
31
+ def set_option(name, value)
32
+ if send(name.to_s).nil?
33
+ send(name.to_s + '=', value)
34
+ end
35
+ end
36
+
37
+ def load_from_hash(hash)
38
+ return if hash.nil?
39
+
40
+ @options_list.each do |o|
41
+ set_option(o, hash[o.to_sym] || hash[o.to_s])
42
+ end
43
+ end
44
+
45
+ def load_from_env(prefix)
46
+ @options_list.each do |o|
47
+ set_option(o, ENV[prefix + '_' + o.to_s.upcase])
48
+ end
49
+ end
50
+
51
+ def load_from_config(product, config_file)
52
+ return if config_file.nil?
53
+
54
+ if File.exists?(File.expand_path(config_file))
55
+ config = JSON.load(File.read(File.expand_path(config_file)))
56
+
57
+ load_from_hash(config['iron_' + product])
58
+ load_from_hash(config['iron'])
59
+ load_from_hash(config)
60
+ end
61
+ end
62
+
63
+ def options
64
+ res = {}
65
+
66
+ @options_list.each do |o|
67
+ res[o.to_sym] = send(o.to_s)
68
+ end
69
+
70
+ res
71
+ end
72
+
73
+ def common_request_hash
74
+ {
75
+ 'Content-Type' => 'application/json',
76
+ 'Authorization' => "OAuth #{@token}",
77
+ 'User-Agent' => @user_agent
78
+ }
79
+ end
80
+
81
+ def url
82
+ "#{scheme}://#{host}:#{port}/#{api_version}/"
83
+ end
84
+
85
+ def get(method, params = {})
86
+ request_hash = {}
87
+ request_hash[:headers] = common_request_hash
88
+ request_hash[:params] = params
89
+
90
+ IronCore::Logger.debug 'IronCore', "GET #{url + method} with params='#{request_hash.to_s}'"
91
+
92
+ @rest.get(url + method, request_hash)
93
+ end
94
+
95
+ def post(method, params = {})
96
+ request_hash = {}
97
+ request_hash[:headers] = common_request_hash
98
+ request_hash[:body] = params.to_json
99
+
100
+ IronCore::Logger.debug 'IronCore', "POST #{url + method} with params='#{request_hash.to_s}'"
101
+
102
+ @rest.post(url + method, request_hash)
103
+ end
104
+
105
+ def delete(method, params = {})
106
+ request_hash = {}
107
+ request_hash[:headers] = common_request_hash
108
+ request_hash[:params] = params
109
+
110
+ IronCore::Logger.debug 'IronCore', "DELETE #{url + method} with params='#{request_hash.to_s}'"
111
+
112
+ @rest.delete(url + method, request_hash)
113
+ end
114
+
115
+ # FIXME: retries support
116
+ # FIXME: user agent support
117
+ def post_file(method, file, params = {})
118
+ request_hash = {}
119
+ request_hash[:data] = params.to_json
120
+ request_hash[:file] = file
121
+
122
+ IronCore::Logger.debug 'IronCore', "POST #{url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"
123
+
124
+ RestClient.post(url + method + "?oauth=#{@token}", request_hash)
125
+ end
126
+
127
+ def parse_response(response, parse_json = true)
128
+ IronCore::Logger.debug 'IronCore', "GOT #{response.code} with params='#{response.body}'"
129
+
130
+ raise IronCore::IronError.new(response.body) if response.code != 200
131
+
132
+ return response.body unless parse_json
133
+ JSON.parse(response.body)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,4 @@
1
+ module IronCore
2
+ class IronError < Exception
3
+ end
4
+ end
@@ -0,0 +1,38 @@
1
+ require 'logger'
2
+
3
+ module IronCore
4
+ module Logger
5
+ def self.logger
6
+ unless @logger
7
+ @logger = ::Logger.new(STDOUT)
8
+ @logger.level = ::Logger::INFO
9
+ end
10
+
11
+ @logger
12
+ end
13
+
14
+ def self.logger=(logger)
15
+ @logger = logger
16
+ end
17
+
18
+ def self.fatal(product, msg)
19
+ self.logger.fatal(product) { msg }
20
+ end
21
+
22
+ def self.error(product, msg)
23
+ self.logger.error(product) { msg }
24
+ end
25
+
26
+ def self.warn(product, msg)
27
+ self.logger.warn(product) { msg }
28
+ end
29
+
30
+ def self.info(product, msg)
31
+ self.logger.info(product) { msg }
32
+ end
33
+
34
+ def self.debug(product, msg)
35
+ self.logger.debug(product) { msg }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ module IronCore
2
+ @@version = nil
3
+
4
+ def self.version
5
+ @@version ||= File.read(File.dirname(__FILE__) + '/../../VERSION').strip
6
+ end
7
+ end
data/lib/iron_core.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'iron_core/version'
2
+ require_relative 'iron_core/logger'
3
+ require_relative 'iron_core/client'
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iron_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Kirilenko
9
+ - Iron.io, Inc
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-04-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rest-client
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rest
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: bundler
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>'
69
+ - !ruby/object:Gem::Version
70
+ version: 1.0.0
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>'
77
+ - !ruby/object:Gem::Version
78
+ version: 1.0.0
79
+ - !ruby/object:Gem::Dependency
80
+ name: jeweler
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 1.8.3
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 1.8.3
95
+ description: Core library for Iron products
96
+ email: info@iron.io
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE
101
+ files:
102
+ - LICENSE
103
+ - VERSION
104
+ - lib/iron_core.rb
105
+ - lib/iron_core/client.rb
106
+ - lib/iron_core/iron_error.rb
107
+ - lib/iron_core/logger.rb
108
+ - lib/iron_core/version.rb
109
+ homepage: https://github.com/iron-io/iron_core_ruby
110
+ licenses: []
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ segments:
122
+ - 0
123
+ hash: -480935439
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.19
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Core library for Iron products
136
+ test_files: []