bazil_client 1.0.0 → 1.0.1

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 (5) hide show
  1. data/ChangeLog.md +8 -0
  2. data/Gemfile +1 -1
  3. data/VERSION +1 -1
  4. data/lib/bazil/client.rb +43 -2
  5. metadata +3 -3
data/ChangeLog.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## Version 1.0.1 (2013-02-28) ##
2
+
3
+ Feature
4
+
5
+ - Support API Key file
6
+ -- Client read ./.bazil/api_keys or ~/.bazil/api_keys if exists
7
+ - fix Gemfile
8
+
1
9
  ## Version 1.0.0 (2013-02-14) ##
2
10
 
3
11
  Released!
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/lib/bazil/client.rb CHANGED
@@ -13,7 +13,7 @@ module Bazil
13
13
  extend Forwardable
14
14
 
15
15
  class Options
16
- attr_reader :host, :port, :scheme, :ca_file, :ssl_version, :verify_mode
16
+ attr_reader :host, :port, :scheme, :ca_file, :ssl_version, :verify_mode, :api_key, :secret_key
17
17
 
18
18
  def initialize(options)
19
19
  if options.kind_of? String
@@ -21,29 +21,58 @@ module Bazil
21
21
  end
22
22
  options = symbolize_keys(options)
23
23
 
24
+ load_url_option(options)
25
+ load_ca_file_option(options)
26
+ load_ssl_version_option(options)
27
+ load_verify_option(options)
28
+ load_api_keys_option(options)
29
+ end
30
+
31
+ private
32
+
33
+ def load_url_option(options)
24
34
  url = URI::parse(options[URL_KEY] || DEFAULT_URL)
25
35
  @host = url.host or raise "Failed to obtain host name from given url: url = #{url.to_s}"
26
36
  @port = url.port or raise "Failed to obtain port number from given url: url = #{url.to_s}"
27
37
  @scheme = url.scheme or raise "Failed to obtain scheme from given url: url = #{url.to_s}"
28
38
  raise "Unsupported scheme '#{@scheme}'" unless AVAILABLE_SCHEMA.include? @scheme
39
+ end
29
40
 
41
+ def load_ca_file_option(options)
30
42
  @ca_file = options[CA_FILE_KEY] || DEFAULT_CA_FILE
31
43
  if @ca_file
32
44
  raise "ca_file option must be string value" unless @ca_file.is_a? String
33
45
  raise "ca_file option must be absolute path" unless @ca_file[0] == '/'
34
46
  raise "ca_file '#{@ca_file}' doesn't exist" unless File::exists? @ca_file
35
47
  end
48
+ end
36
49
 
50
+ def load_ssl_version_option(options)
37
51
  ssl_version = options[SSL_VERSION_KEY] || DEFAULT_SSL_VERSION
38
52
  raise "Unsupported SSL version '#{ssl_version}'" unless AVAILABLE_SSL_VERSIONS.has_key? ssl_version
39
53
  @ssl_version = AVAILABLE_SSL_VERSIONS[ssl_version]
54
+ end
40
55
 
56
+ def load_verify_option(options)
41
57
  skip_verify = options[SKIP_VERIFY_KEY] || DEFAULT_SKIP_VERIFY
42
58
  raise "skip_verify option must be boolean value" unless skip_verify.is_a?(TrueClass) || skip_verify.is_a?(FalseClass)
43
59
  @verify_mode = skip_verify ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
44
60
  end
45
61
 
46
- private
62
+ def load_api_keys_option(options)
63
+ api_keys_file = options[API_KEYS_FILE_KEY] || DEFAULT_API_KEYS_FILES.find{|file|
64
+ File::readable?(file) && File::file?(file)
65
+ }
66
+ raise "API keys file is not found" if api_keys_file.nil?
67
+
68
+ api_keys = symbolize_keys(JSON::parse(File::read(api_keys_file)))
69
+ @api_key = api_keys[API_KEYS_API_KEY_KEY]
70
+ @secret_key = api_keys[API_KEYS_SECRET_KEY_KEY]
71
+ rescue SystemCallError, RuntimeError => e
72
+ STDERR.puts ""
73
+ STDERR.puts "WARNING: Failed to read api_keys file. Check your api_keys file, or set api_keys manually by set_api_keys(API_KEY, SECRET_KEY): ERROR = #{e.to_s}"
74
+ STDERR.puts ""
75
+ end
47
76
 
48
77
  def symbolize_keys(hash)
49
78
  {}.tap{|new_hash|
@@ -66,6 +95,16 @@ module Bazil
66
95
 
67
96
  SKIP_VERIFY_KEY = :skip_verify
68
97
  DEFAULT_SKIP_VERIFY = false
98
+
99
+ BAZIL_CONFIG_DIRS = [
100
+ File::join(ENV['PWD'], '.bazil'),
101
+ File::join(ENV['HOME'], '.bazil')
102
+ ]
103
+
104
+ API_KEYS_FILE_KEY = :api_keys
105
+ DEFAULT_API_KEYS_FILES = BAZIL_CONFIG_DIRS.map{|dir| File::join(dir, 'api_keys') }
106
+ API_KEYS_API_KEY_KEY = :api_key
107
+ API_KEYS_SECRET_KEY_KEY = :secret_key
69
108
  end
70
109
 
71
110
  def set_ssl_options(http, options)
@@ -79,7 +118,9 @@ module Bazil
79
118
  opt = Options.new(options)
80
119
  http = Net::HTTP.new(opt.host, opt.port)
81
120
  set_ssl_options(http,opt)
121
+
82
122
  @http_cli = REST.new(http)
123
+ set_api_keys(opt.api_key, opt.secret_key)
83
124
  end
84
125
 
85
126
  def_delegators :@http_cli, :read_timeout, :read_timeout=, :set_api_keys
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bazil_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-14 00:00:00.000000000 Z
12
+ date: 2013-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  segments:
104
104
  - 0
105
- hash: -4262472802805615291
105
+ hash: -2294157025898643807
106
106
  requirements: []
107
107
  rubyforge_project:
108
108
  rubygems_version: 1.8.23