cloudcheckr 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dec582152815019c9671a80a39292bb06e20a3aa
4
- data.tar.gz: 91019358515155970f75a17af21282dd3f0a2743
3
+ metadata.gz: fc03c92dad276e512d647ec770c032e3744a0a90
4
+ data.tar.gz: 51e06d2c96c2fbc45d2c30413ac551ebd8a55d8b
5
5
  SHA512:
6
- metadata.gz: d23ddc22acb2a9a37ec7e0aa5de661abe3b16416e9e1bb69c032126d06316b7abdb5b22ab514f7dcf0ffdc7a2e8078fcc13a4510cdfcebd8ecda3282849b0902
7
- data.tar.gz: 7f817f70d6d89f74321cfb83d53b9ad2d1380b889a033efa09c66fc7f76c08ca2fd3c5e2e59efbabba23d04cdfb10410d666240bd4793057a1dc8583790af134
6
+ metadata.gz: 1a91207b4415e44b58749935a7fca2e677f674b13d6ee44f7fdb005ec236a2cc48d423b8a19eeea08af33be5b11d706d5643c26ee67f6438beafc683b0984279
7
+ data.tar.gz: 73f6e0268e4aadc1ec63bbbd75414929fa07dfb6e48b247b5573acbe55f8a7d085c1e8425f61b162f8e8c52f296d240d7215074b78f5f72b1fcd4a24200f185b
data/README.md CHANGED
@@ -32,6 +32,10 @@ client = CloudCheckr::API::Client.new
32
32
  ENV['CLOUDCHECKR_ACCESS_KEY'] = "146D0Y8R...W6U0463K"
33
33
  client = CloudCheckr::API::Client.new
34
34
 
35
+ # JSON responses automatically convert keys from camel case to snake case.
36
+ # You can disable key conversion globally
37
+ CloudCheckr::API.snake_case_json_keys(false)
38
+
35
39
  # Use XML instead (not fully implemented)
36
40
  client = CloudCheckr::API::Client.new(format: :xml)
37
41
  ```
@@ -105,13 +109,13 @@ The API client uses [Faraday](https://github.com/lostisland/faraday) under the h
105
109
  It's easy to customize Faraday to your liking.
106
110
 
107
111
  ```ruby
108
- # Globally apply Faraday settings
112
+ # Globally apply Faraday settings (middleware evaluated in reverse!)
109
113
  CloudCheckr::API.connection_builder do |faraday|
110
114
  faraday.request :url_encoded
111
115
 
116
+ faraday.response :mashify
112
117
  faraday.response :xml, content_type: /\bxml$/
113
118
  faraday.response :json, content_type: /\bjson$/
114
- faraday.response :mashify, content_type: /\bjson$/
115
119
  faraday.response :logger, nil, bodies: true
116
120
 
117
121
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
@@ -121,9 +125,9 @@ end
121
125
  client = CloudCheckr::API::Client.new do
122
126
  faraday.request :url_encoded
123
127
 
128
+ faraday.response :mashify
124
129
  faraday.response :xml, content_type: /\bxml$/
125
130
  faraday.response :json, content_type: /\bjson$/
126
- faraday.response :mashify, content_type: /\bjson$/
127
131
  faraday.response :logger, nil, bodies: true
128
132
 
129
133
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
data/cloudcheckr.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "faraday"
22
22
  spec.add_dependency "faraday_middleware"
23
+ spec.add_dependency "hashie"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.6"
25
26
  spec.add_development_dependency "rake"
@@ -7,24 +7,41 @@ module CloudCheckr
7
7
  module API
8
8
  CONTROLLERS = CloudCheckr::API::Controllers.build_controller_classes!
9
9
 
10
+ @@access_key = ENV['CLOUDCHECKR_ACCESS_KEY']
11
+ @@use_account = ENV['CLOUDCHECKR_USE_ACCOUNT']
12
+ @@connection_builder = nil
13
+ @@snake_case_json_keys = true
14
+
10
15
  def self.access_key(new_access_key = nil)
11
- if new_access_key.nil?
12
- @@access_key ||= ENV['CLOUDCHECKR_ACCESS_KEY']
13
- else
16
+ unless @@access_key.nil?
14
17
  @@access_key = new_access_key
15
18
  end
19
+
20
+ @@access_key
16
21
  end
17
22
 
18
23
  def self.use_account(new_use_account = nil)
19
- if new_use_account.nil?
20
- @@use_account ||= ENV['CLOUDCHECKR_USE_ACCOUNT']
21
- else
24
+ unless new_use_account.nil?
22
25
  @@use_account = new_use_account
23
26
  end
27
+
28
+ @@use_account
24
29
  end
25
30
 
26
31
  def self.connection_builder(&builder)
27
- @@connection_builder = builder || @@connection_builder
32
+ if block_given?
33
+ @@connection_builder = builder
34
+ end
35
+
36
+ @@connection_builder
37
+ end
38
+
39
+ def self.snake_case_json_keys(enable = nil)
40
+ unless enable.nil?
41
+ @@snake_case_json_keys = enable
42
+ end
43
+
44
+ @@snake_case_json_keys
28
45
  end
29
46
  end
30
47
  end
@@ -13,8 +13,6 @@ module CloudCheckr
13
13
  attr_reader :access_key, :url, :format
14
14
 
15
15
  def initialize(options = {}, &connection_builder)
16
- super()
17
-
18
16
  @access_key = options.fetch(:access_key, API.access_key)
19
17
  @use_account = options.fetch(:use_account, API.use_account)
20
18
  @url = options.fetch(:url, DEFAULT_URL)
@@ -37,16 +35,9 @@ module CloudCheckr
37
35
  end
38
36
 
39
37
  def default_params
40
- {access_key: @access_key}
41
- end
42
-
43
- def require_params!(required_params, params)
44
- missing_required_params = required_params.map(&:to_sym) - prepare_params(params).keys.map(&:to_sym)
45
-
46
- if missing_required_params.any?
47
- raise "Missing required parameters: #{missing_required_params.join(', ')}"
48
- else
49
- nil
38
+ {}.tap do |params|
39
+ params[:access_key] = @access_key unless @access_key.nil?
40
+ params[:use_account] = @use_account unless @use_account.nil?
50
41
  end
51
42
  end
52
43
 
@@ -70,11 +61,29 @@ module CloudCheckr
70
61
  if response.status != 200
71
62
  # TODO: Currently assumes JSON response
72
63
  raise ::CloudCheckr::API::ResponseError.new(data)
64
+ elsif @format == :json && ::CloudCheckr::API.snake_case_json_keys
65
+ convert_keys_to_snake_case(data)
73
66
  else
74
67
  data
75
68
  end
76
69
  end
77
70
 
71
+ def convert_keys_to_snake_case(hash)
72
+ hash_class = hash.class
73
+
74
+ if hash.is_a?(Hash)
75
+ hash_class.new.tap do |new_hash|
76
+ hash.map do |k, v|
77
+ new_hash[k.gsub(/(.)([A-Z])/,'\1_\2').downcase] = convert_keys_to_snake_case(v)
78
+ end
79
+ end
80
+ elsif hash.is_a?(Array)
81
+ hash.map{|item| convert_keys_to_snake_case(item)}
82
+ else
83
+ hash
84
+ end
85
+ end
86
+
78
87
  def build_connection
79
88
  Faraday.new(url: @url) do |faraday|
80
89
  apply_connection_settings(faraday)
@@ -91,13 +100,12 @@ module CloudCheckr
91
100
  # form-encode POST params
92
101
  faraday.request :url_encoded
93
102
 
94
- faraday.response :xml, content_type: /\bxml$/
95
- faraday.response :json, content_type: /\bjson$/
96
- faraday.response :mashify, content_type: /\bjson$/
97
- # faraday.response @format
98
- # faraday.response :mashify if @format == :json
103
+ faraday.response :mashify
104
+ faraday.response :xml, content_type: /\bxml$/
105
+ faraday.response :json, content_type: /\bjson$/
99
106
 
100
107
  # faraday.response :logger, nil, bodies: true
108
+ # require 'faraday/detailed_logger'
101
109
  # faraday.response :detailed_logger
102
110
 
103
111
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
@@ -25,12 +25,10 @@ module CloudCheckr
25
25
 
26
26
  if api_call['method_name'].start_with?('get_')
27
27
  define_method(method_name) do |params = {}, headers = nil, &request_builder|
28
- @client.require_params!(required_params, params)
29
28
  @client.get(controller_name, method_name, params, headers, &request_builder)
30
29
  end
31
30
  else
32
31
  define_method(method_name) do |params = {}, headers = nil, &request_builder|
33
- @client.require_params!(required_params, params)
34
32
  @client.post(controller_name, method_name, params, headers, &request_builder)
35
33
  end
36
34
  end
@@ -1,3 +1,3 @@
1
1
  module Cloudcheckr
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudcheckr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Van Horn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-12 00:00:00.000000000 Z
11
+ date: 2015-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement