maxcdn 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -96,6 +96,14 @@ bundle exec ruby ./test/integration.rb # requires host's IP be whitelisted
96
96
 
97
97
  # Change Log
98
98
 
99
+ ##### 0.1.3
100
+
101
+ * Requested changes for debugging and development support. (See issue #2).
102
+
103
+ ##### 0.1.2
104
+
105
+ * Fixing an issue with lib loading in `0.1.1`.
106
+
99
107
  ##### 0.1.1
100
108
 
101
109
  * Fixing POST, DELETE and PUT to send data via request body.
data/lib/ext/hash.rb CHANGED
@@ -36,6 +36,27 @@ class Hash
36
36
  params
37
37
  end
38
38
 
39
+ def case_indifferent_delete key
40
+ existing_keys = self.keys.map { |k| k.downcase }
41
+ index = existing_keys.index(key.downcase)
42
+ unless index.nil?
43
+ self.delete(self.keys[index])
44
+ end
45
+ end
46
+
47
+ def case_indifferent_merge incoming_hash
48
+ existing_keys = self.keys.map { |k| k.downcase }
49
+
50
+ incoming_hash.each do |key, value|
51
+ index = existing_keys.index(key.downcase)
52
+ if index.nil?
53
+ self[key] = value
54
+ else
55
+ self[self.keys[index]] = value
56
+ end
57
+ end
58
+ end
59
+
39
60
  def self.from_array(array = [])
40
61
  h = Hash.new
41
62
  array.size.times do |t|
data/lib/maxcdn.rb CHANGED
@@ -10,8 +10,9 @@ module MaxCDN
10
10
 
11
11
  class Client
12
12
  attr_accessor :client, :debug
13
- def initialize(company_alias, key, secret, server="rws.maxcdn.com", _debug=false)
13
+ def initialize(company_alias, key, secret, server="rws.maxcdn.com", secure_connection=true, _debug=false)
14
14
  @debug = _debug
15
+ @secure_connection = secure_connection
15
16
  @company_alias = company_alias
16
17
  @server = server
17
18
  @request_signer = Signet::OAuth1::Client.new(
@@ -22,6 +23,7 @@ module MaxCDN
22
23
  end
23
24
 
24
25
  def _connection_type
26
+ return "http" unless @secure_connection
25
27
  "https"
26
28
  end
27
29
 
@@ -45,7 +47,17 @@ module MaxCDN
45
47
  req_opts[:body] = data.to_params if options[:body]
46
48
 
47
49
  request = @request_signer.generate_authenticated_request(req_opts)
48
- request.headers["User-Agent"] = "Ruby MaxCDN API Client"
50
+
51
+ # crazyness for headers
52
+ headers = options.delete(:headers) || {}
53
+ headers["User-Agent"] = "Ruby MaxCDN API Client"
54
+
55
+ # because CurbFu overwrites 'content-type' header, strip it
56
+ # to set it later
57
+ content_type = headers.case_indifferent_delete("Content-Type") || (options[:body] ? "application/json" : "application/x-www-form-urlencoded")
58
+
59
+ # merge headers with request headers
60
+ request.headers.case_indifferent_merge(headers)
49
61
 
50
62
  begin
51
63
  curb_opts = {
@@ -58,15 +70,22 @@ module MaxCDN
58
70
  response = CurbFu.send method, curb_opts, request.body do |curb|
59
71
  curb.verbose = debug
60
72
 
61
- # Because CurbFu overwrite the content-type header passed
62
- # to it
63
- curb.headers["Content-Type"] = "application/json" if request.body
73
+ # Because CurbFu overwrites the content-type header passed
74
+ # to it. so we'll be setting our own.
75
+ #
76
+ # First, remove any existing 'Content-Type' header.
77
+ curb.headers.case_indifferent_delete("Content-Type")
78
+
79
+ # Second, set 'Content-Type' to our desired value.
80
+ curb.headers["Content-Type"] = content_type
64
81
  end
65
82
 
83
+ return response if options[:debug_request]
66
84
  pp response if debug
67
85
 
68
86
  response_json = JSON.load(response.body)
69
87
 
88
+ return response_json if options[:debug_json]
70
89
  pp response_json if debug
71
90
 
72
91
  unless response.success? or response.redirect?
@@ -80,10 +99,10 @@ module MaxCDN
80
99
  response_json
81
100
  end
82
101
 
83
- [ :post, :get, :put, :delete ].each do |meth|
84
- define_method(meth) do |uri, data={}, options={}|
85
- options[:body] ||= true if meth != :get
86
- self._response_as_json meth.to_s, uri, options, data
102
+ [ :post, :get, :put, :delete ].each do |method|
103
+ define_method(method) do |uri, data={}, options={}|
104
+ options[:body] ||= true if method != :get
105
+ self._response_as_json method.to_s, uri, options, data
87
106
  end
88
107
  end
89
108
 
@@ -1,3 +1,3 @@
1
1
  module MaxCDN
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -29,4 +29,45 @@ class Client < Minitest::Test
29
29
  }.to_params
30
30
  end
31
31
 
32
+ def test_case_indifferent_delete
33
+ original_hash = {
34
+ # using realistic content types to test real use cases
35
+ "content-type" => "1",
36
+ "X-Forwarded-For" => "2",
37
+ "CACHE-CONTROL" => "3"
38
+ }
39
+
40
+ assert_equal "1", original_hash.case_indifferent_delete("CONTENT-TYPE"), "delete 'CONTENT-TYPE'"
41
+ refute original_hash.has_key?("content-type"), "deleted 'content-type'"
42
+
43
+ assert_equal "3", original_hash.case_indifferent_delete("cache-control"), "delete 'cache-control'"
44
+ refute original_hash.has_key?("CACHE-CONTROL"), "delete 'CACHE-CONTROL'"
45
+ end
46
+
47
+ def test_case_indifferent_merge
48
+ original_hash = {
49
+ # using realistic content types to test real use cases
50
+ "content-type" => "1",
51
+ "X-Forwarded-For" => "2",
52
+ "CACHE-CONTROL" => "3",
53
+ "unchanged-original" => "4"
54
+ }
55
+
56
+ new_hash = {
57
+ # using realistic content types to test real use cases
58
+ "Content-Type" => "5",
59
+ "X-FORWARDED-FOR" => "6",
60
+ "cache-control" => "7",
61
+ "new-header" => "8"
62
+ }
63
+
64
+ assert original_hash.case_indifferent_merge(new_hash)
65
+
66
+ assert_equal "4", original_hash["unchanged-original"] ,"unchanged-original"
67
+ assert_equal "5", original_hash["content-type"] ,"content-type"
68
+ assert_equal "6", original_hash["X-Forwarded-For"] ,"X-Forwarded-For"
69
+ assert_equal "7", original_hash["CACHE-CONTROL"] ,"CACHE-CONTROL"
70
+ assert_equal "8", original_hash["new-header"] ,"new-header"
71
+ end
72
+
32
73
  end
data/test/maxcdn_test.rb CHANGED
@@ -11,45 +11,51 @@ include WebMock::API
11
11
 
12
12
  host = "https://rws.maxcdn.com/alias"
13
13
 
14
- headers = {
14
+ expected_headers = {
15
15
  'Authorization' => /.+/,
16
16
  'Cache-Control' => /.+/,
17
- 'Content-Type' => /application.+/,
17
+ 'Content-Type' => "application/json",
18
18
  'Expect' => /.*/,
19
19
  'User-Agent' => "Ruby MaxCDN API Client"
20
20
  }
21
21
 
22
+ # requests with :body
22
23
  stub_request(:post, host+"/zones/pull.json")
23
- .with(:body => 'foo=bar&bar=foo', :headers => headers)
24
- .to_return(:body => '{"foo": "bar"}')
25
-
26
- stub_request(:post, host+"/zones/pull.json?foo=bar")
27
- .with(:headers => headers)
24
+ .with(:body => "foo=bar&bar=foo", :headers => expected_headers)
28
25
  .to_return(:body => '{"foo": "bar"}')
29
26
 
30
27
  stub_request(:put, host+"/account.json")
31
- .with(:body => "foo=bar", :headers => headers)
28
+ .with(:body => "foo=bar", :headers => expected_headers)
32
29
  .to_return(:body => '{"foo":"bar"}')
33
30
 
34
31
  stub_request(:delete, host+"/zones/pull.json/12345/cache")
35
- .with(:body => "files=foo.txt", :headers => headers)
32
+ .with(:body => "files=foo.txt", :headers => expected_headers)
36
33
  .to_return(:body => '{"foo":"bar"}')
37
34
 
38
35
  stub_request(:delete, host+"/zones/pull.json/12345/cache")
39
- .with(:body => "files[0]=foo.txt&files[1]=bar.txt", :headers => headers)
36
+ .with(:body => "files[0]=foo.txt&files[1]=bar.txt", :headers => expected_headers)
40
37
  .to_return(:body => '{"foo":"bar"}')
41
38
 
42
- # stubs below this don't send content-type header
43
- headers.delete('Content-Type')
39
+ stub_request(:delete, host+"/zones/pull.json/12345/cache")
40
+ .with(:headers => expected_headers)
41
+ .to_return(:body => '{"foo":"bar"}')
44
42
 
43
+ # requests without :body
44
+ expected_headers['Content-Type'] = "application/x-www-form-urlencoded"
45
45
  stub_request(:get, host+"/account.json")
46
- .with(:headers => headers)
46
+ .with(:headers => expected_headers)
47
47
  .to_return(:body => '{"foo":"bar"}')
48
48
 
49
- stub_request(:delete, host+"/zones/pull.json/12345/cache")
50
- .with(:headers => headers)
49
+ # test custom content-type
50
+ expected_headers['Content-Type'] = "application/custom"
51
+ stub_request(:get, host+"/account.json/address")
52
+ .with(:headers => expected_headers)
51
53
  .to_return(:body => '{"foo":"bar"}')
52
54
 
55
+ # ingore headers
56
+ stub_request(:post, host+"/zones/pull.json?foo=bar")
57
+ .to_return(:body => '{"foo": "bar"}')
58
+
53
59
  class Client < Minitest::Test
54
60
 
55
61
  def setup
@@ -63,6 +69,9 @@ class Client < Minitest::Test
63
69
 
64
70
  def test__connection_type
65
71
  assert_equal "https", @max._connection_type
72
+
73
+ max = MaxCDN::Client.new("alias", "key", "secret", "rws.maxcdn.com", false)
74
+ assert_equal "http", max._connection_type
66
75
  end
67
76
 
68
77
  def test__get_url
@@ -85,6 +94,20 @@ class Client < Minitest::Test
85
94
  assert_equal({ "foo" => "bar" }, res)
86
95
  end
87
96
 
97
+ def test__response_as_json_debug_json
98
+ res = @max._response_as_json("post", "zones/pull.json", { :body => true, :debug_json => true }, { "foo"=> "bar", "bar" => "foo" })
99
+ assert_equal({ "foo" => "bar" }, res)
100
+ end
101
+
102
+ def test__response_as_json_debug_request
103
+ res = @max._response_as_json("post", "zones/pull.json", { :body => true, :debug_request => true }, { "foo"=> "bar", "bar" => "foo" })
104
+ assert_equal(CurbFu::Response::Base, res.class)
105
+ end
106
+
107
+ def test_custom_header
108
+ assert_equal({ "foo" => "bar" }, @max.get("account.json/address", {}, { :headers => { 'content-type' => 'application/custom' }}))
109
+ end
110
+
88
111
  def test_get
89
112
  assert_equal({ "foo" => "bar" }, @max.get("account.json"))
90
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maxcdn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
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: 2014-02-13 00:00:00.000000000 Z
12
+ date: 2014-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: signet
@@ -83,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  segments:
85
85
  - 0
86
- hash: -869872735616605413
86
+ hash: -4324918256587917915
87
87
  required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  none: false
89
89
  requirements:
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  segments:
94
94
  - 0
95
- hash: -869872735616605413
95
+ hash: -4324918256587917915
96
96
  requirements: []
97
97
  rubyforge_project:
98
98
  rubygems_version: 1.8.23