iron_worker_ng 0.3.6 → 0.3.7

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.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/iron_worker_ng/api_client.rb +45 -54
  3. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
@@ -1,7 +1,6 @@
1
1
  require 'rest-client'
2
2
  require 'rest'
3
3
  require 'json'
4
- require 'yaml'
5
4
  require 'time'
6
5
 
7
6
  require_relative 'api_client_error'
@@ -19,67 +18,55 @@ module IronWorkerNG
19
18
  attr_accessor :user_agent
20
19
 
21
20
  def initialize(options = {})
22
- @token = options[:token] || options['token']
23
- @project_id = options[:project_id] || options['project_id']
24
-
25
- if (@token.nil? || @project_id.nil?) && ((not options[:yaml_config_file].nil?) || (not options['yaml_config_file'].nil?))
26
- load_yaml_config(options[:yaml_config_file] || options['yaml_config_file'])
27
- end
28
-
29
- if (@token.nil? || @project_id.nil?) && ((not options[:json_config_file].nil?) || (not options['json_config_file'].nil?))
30
- load_json_config(options[:json_config_file] || options['json_config_file'])
31
- end
32
-
33
- if @token.nil? || @project_id.nil?
34
- load_yaml_config('iron.yml')
35
- end
36
-
37
- if @token.nil? || @project_id.nil?
38
- load_json_config('iron.json')
39
- end
40
-
41
- @token ||= ENV['IRON_TOKEN']
42
- @project_id ||= ENV['IRON_PROJECT_ID']
43
-
44
- if @token.nil? || @project_id.nil?
45
- load_yaml_config('~/.iron.yml')
46
- end
47
-
48
- if @token.nil? || @project_id.nil?
49
- load_json_config('~/.iron.json')
50
- end
21
+ load_from_hash(options)
22
+ load_from_config(options[:config_file] || options['config_file'])
23
+ load_from_config('iron.json')
24
+ load_from_env('IRON_WORKER')
25
+ load_from_env('IRON')
26
+ load_from_config('~/.iron.json')
27
+ load_from_hash(:scheme => 'https', :host => IronWorkerNG::APIClient::AWS_US_EAST_HOST, :port => 443, :api_version => 2, :user_agent => 'iron_worker_ng-' + IronWorkerNG.version)
51
28
 
52
29
  if (not @token) || (not @project_id)
53
30
  IronWorkerNG::Logger.error 'Both iron.io token and project_id must be specified'
54
31
  raise 'Both iron.io token and project_id must be specified'
55
32
  end
56
33
 
57
- @scheme = options[:scheme] || options['scheme'] || 'https'
58
- @host = options[:host] || options['host'] || IronWorkerNG::APIClient::AWS_US_EAST_HOST
59
- @port = options[:port] || options['port'] || 443
60
- @api_version = options[:api_version] || options['api_version'] || 2
61
- @user_agent = options[:user_agent] || options['user_agent'] || 'iron_worker_ng-' + IronWorkerNG.version
34
+ @rest = Rest::Client.new
35
+ end
36
+
37
+ def load_from_hash(hash)
38
+ return if hash.nil?
62
39
 
63
- @url = "#{scheme}://#{host}:#{port}/#{api_version}/"
40
+ @token ||= hash[:token] || hash['token']
41
+ @project_id ||= hash[:project_id] || hash['project_id']
64
42
 
65
- @rest = Rest::Client.new
43
+ @scheme ||= hash[:scheme] || hash['scheme']
44
+ @host ||= hash[:host] || hash['host']
45
+ @port ||= hash[:port] || hash['port']
46
+ @api_version ||= hash[:api_version] || hash['api_version']
47
+ @user_agent ||= hash[:user_agent] || hash['user_agent']
66
48
  end
67
49
 
68
- def load_yaml_config(config_file)
69
- if File.exists?(File.expand_path(config_file))
70
- config = YAML.load_file(File.expand_path(config_file))
50
+ def load_from_env(prefix)
51
+ @token ||= ENV[prefix + '_TOKEN']
52
+ @project_id ||= ENV[prefix + '_PROJECT_ID']
71
53
 
72
- @token ||= config['token']
73
- @project_id ||= config['project_id']
74
- end
54
+ @scheme ||= ENV[prefix + '_SCHEME']
55
+ @host ||= ENV[prefix + '_HOST']
56
+ @port ||= ENV[prefix + '_PORT']
57
+ @api_version ||= ENV[prefix + '_API_VERSION']
58
+ @user_agent ||= ENV[prefix + '_USER_AGENT']
75
59
  end
76
60
 
77
- def load_json_config(config_file)
61
+ def load_from_config(config_file)
62
+ return if config_file.nil?
63
+
78
64
  if File.exists?(File.expand_path(config_file))
79
65
  config = JSON.load(File.read(File.expand_path(config_file)))
80
66
 
81
- @token ||= config['token']
82
- @project_id ||= config['project_id']
67
+ load_from_hash(config['iron_worker'])
68
+ load_from_hash(config['iron'])
69
+ load_from_hash(config)
83
70
  end
84
71
  end
85
72
 
@@ -91,14 +78,18 @@ module IronWorkerNG
91
78
  }
92
79
  end
93
80
 
81
+ def url
82
+ "#{scheme}://#{host}:#{port}/#{api_version}/"
83
+ end
84
+
94
85
  def get(method, params = {})
95
86
  request_hash = {}
96
87
  request_hash[:headers] = common_request_hash
97
88
  request_hash[:params] = params
98
89
 
99
- IronWorkerNG::Logger.debug "GET #{@url + method} with params='#{request_hash.to_s}'"
90
+ IronWorkerNG::Logger.debug "GET #{url + method} with params='#{request_hash.to_s}'"
100
91
 
101
- @rest.get(@url + method, request_hash)
92
+ @rest.get(url + method, request_hash)
102
93
  end
103
94
 
104
95
  def post(method, params = {})
@@ -106,9 +97,9 @@ module IronWorkerNG
106
97
  request_hash[:headers] = common_request_hash
107
98
  request_hash[:body] = params.to_json
108
99
 
109
- IronWorkerNG::Logger.debug "POST #{@url + method} with params='#{request_hash.to_s}'"
100
+ IronWorkerNG::Logger.debug "POST #{url + method} with params='#{request_hash.to_s}'"
110
101
 
111
- @rest.post(@url + method, request_hash)
102
+ @rest.post(url + method, request_hash)
112
103
  end
113
104
 
114
105
  def delete(method, params = {})
@@ -116,9 +107,9 @@ module IronWorkerNG
116
107
  request_hash[:headers] = common_request_hash
117
108
  request_hash[:params] = params
118
109
 
119
- IronWorkerNG::Logger.debug "DELETE #{@url + method} with params='#{request_hash.to_s}'"
110
+ IronWorkerNG::Logger.debug "DELETE #{url + method} with params='#{request_hash.to_s}'"
120
111
 
121
- @rest.delete(@url + method, request_hash)
112
+ @rest.delete(url + method, request_hash)
122
113
  end
123
114
 
124
115
  # FIXME: retries support
@@ -128,9 +119,9 @@ module IronWorkerNG
128
119
  request_hash[:data] = params.to_json
129
120
  request_hash[:file] = file
130
121
 
131
- IronWorkerNG::Logger.debug "POST #{@url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"
122
+ IronWorkerNG::Logger.debug "POST #{url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"
132
123
 
133
- RestClient.post(@url + method + "?oauth=#{@token}", request_hash)
124
+ RestClient.post(url + method + "?oauth=#{@token}", request_hash)
134
125
  end
135
126
 
136
127
  def parse_response(response, parse_json = true)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_worker_ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -156,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
156
  version: '0'
157
157
  segments:
158
158
  - 0
159
- hash: -4205457
159
+ hash: -452150865
160
160
  required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  none: false
162
162
  requirements: