netdnarws 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "addressable"
4
3
  gem "curb-fu"
5
4
  gemspec
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ # We're Hiring!
2
+ Do you like building cool stuff? Do APIs keep you up at night? We're looking for our next superstar hacker and you could be it. Interested? Check out our job posting on [stackoverflow](http://careers.stackoverflow.com/jobs/37078/senior-web-engineer-for-fun-growing-la-startup-maxcdn&a=JdFbT4OY).
3
+
1
4
  # NetDNA REST Web Services Ruby Client
2
5
 
3
6
  ## Installation
@@ -1,3 +1,3 @@
1
1
  module NetDNARWS
2
- VERSION = "0.2.8"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/netdnarws.rb CHANGED
@@ -1,132 +1,147 @@
1
1
  require 'signet/oauth_1/client'
2
- require 'addressable/uri'
3
2
  require 'curb-fu'
4
3
  require 'json'
5
4
 
6
5
  module NetDNARWS
7
6
  class NetDNA
8
- attr_accessor :client
9
- def initialize company_alias, key, secret,
10
- server='rws.netdna.com', secure_connection=true, *options
11
- @company_alias = company_alias
12
- @server = server
13
- @secure_connection = secure_connection
14
- @request_signer = Signet::OAuth1::Client.new(
15
- :client_credential_key => key,
16
- :client_credential_secret => secret,
17
- :two_legged => true
18
- )
7
+ include NetDNARWS::Utils
8
+
9
+ attr_accessor :client
10
+ def initialize company_alias, key, secret,
11
+ server='rws.netdna.com', secure_connection=true, *options
12
+ @company_alias = company_alias
13
+ @server = server
14
+ @secure_connection = secure_connection
15
+ @request_signer = Signet::OAuth1::Client.new(
16
+ :client_credential_key => key,
17
+ :client_credential_secret => secret,
18
+ :two_legged => true
19
+ )
20
+ end
21
+
22
+ def _connection_type
23
+ return "http" unless @secure_connection
24
+ "https"
25
+ end
26
+
27
+ def _encode_params params={}
28
+ encode_params(params).map { |k, v|
29
+ "#{k}=#{v}"
30
+ }.join "&"
31
+ end
32
+
33
+ def _get_url uri, params={}
34
+ url = "#{_connection_type}://#{@server}/#{@company_alias}#{uri}"
35
+ if not attributes.empty?
36
+ url += "?#{_encode_params(params)}"
19
37
  end
20
38
 
21
- def _connection_type
22
- return "http" unless @secure_connection
23
- "https"
24
- end
39
+ url
40
+ end
25
41
 
26
- def _encode_params values
27
- uri = Addressable::URI.new
28
- uri.query_values = values
29
- uri.query
42
+ def _response_as_json method, uri, options={}, *attributes
43
+ if options.delete(:debug)
44
+ puts "Making #{method.upcase} request to #{_get_url uri}"
30
45
  end
31
46
 
32
- def _get_url uri, attributes
33
- url = "#{_connection_type}://#{@server}/#{@company_alias}#{uri}"
34
- if not attributes.empty?
35
- url += "?#{_encode_params(attributes[0])}"
36
- end
47
+ request_options = {
48
+ :uri => _get_url(uri, options[:body] ? attributes[0] : {}),
49
+ :method => method
50
+ }
37
51
 
38
- url
39
- end
52
+ request_options[:body] = _encode_params(attributes[0]) if options[:body]
53
+ request = @request_signer.generate_authenticated_request(request_options)
54
+ request.headers["User-Agent"] = "Ruby NetDNA API Client"
40
55
 
41
- def _response_as_json method, uri, options={}, *attributes
42
- if options.delete(:debug)
43
- puts "Making #{method.upcase} request to #{_get_url uri}"
56
+ begin
57
+ curb_options = {}
58
+ curb_options[:url] = _get_url(uri, attributes)
59
+ curb_options[:headers] = request.headers
60
+
61
+ if not options[:body]
62
+ response = CurbFu.send method, curb_options
63
+ else
64
+ response = CurbFu.send method, curb_options, request.body
44
65
  end
45
66
 
46
- request_options = {
47
- :uri => _get_url(uri, attributes),
48
- :method => method
49
- }
67
+ return response if options[:debug_request]
50
68
 
51
- request_options[:body] = _encode_params(attributes[0]) if options[:body]
52
- request = @request_signer.generate_authenticated_request(request_options)
53
- request.headers["User-Agent"] = "Ruby NetDNA API Client"
69
+ response_json = JSON.load(response.body)
54
70
 
55
- begin
56
- curb_options = {}
57
- curb_options[:url] = _get_url(uri, attributes)
58
- curb_options[:headers] = request.headers
71
+ return response_json if options[:debug_json]
59
72
 
60
- if not options[:body]
61
- response = CurbFu.send method, curb_options
62
- else
63
- response = CurbFu.send method, curb_options, request.body
64
- end
73
+ if not (response.success? or response.redirect?)
74
+ error_message = response_json['error']['message']
75
+ raise Exception.new("#{response.status}: #{error_message}")
76
+ end
77
+ rescue TypeError
78
+ raise Exception.new(
79
+ "#{response.status}: No information supplied by the server"
80
+ )
81
+ end
65
82
 
66
- return response if options[:debug_request]
83
+ response_json
84
+ end
67
85
 
68
- response_json = JSON.load(response.body)
86
+ def get uri, data={}, options={}
87
+ options[:body] = false
88
+ self._response_as_json 'get', uri, options, data
89
+ end
69
90
 
70
- return response_json if options[:debug_json]
91
+ def post uri, data={}, options={}
92
+ options[:body] = true
93
+ self._response_as_json 'post', uri, options, data
94
+ end
71
95
 
72
- if not (response.success? or response.redirect?)
73
- error_message = response_json['error']['message']
74
- raise Exception.new("#{response.status}: #{error_message}")
75
- end
76
- rescue TypeError
77
- raise Exception.new(
78
- "#{response.status}: No information supplied by the server"
79
- )
80
- end
96
+ def put uri, data={}, options={}
97
+ options[:body] = true
98
+ self._response_as_json 'put', uri, options, data
99
+ end
81
100
 
82
- response_json
83
- end
101
+ def delete uri, data={}, options={}
102
+ options[:body] = false
103
+ self._response_as_json 'delete', uri, options, data
104
+ end
84
105
 
85
- def get uri, data={}, options={}
86
- options[:body] = false
87
- self._response_as_json 'get', uri, options, data
88
- end
106
+ def purge zone_id, file_or_files=nil, options={}
107
+ if file_or_files.respond_to? :each
108
+ options[:debug_json] = true if options[:debug_json].nil?
109
+ responses = Hash.new
89
110
 
90
- def post uri, data={}, options={}
91
- options[:body] = true
92
- self._response_as_json 'post', uri, options, data
111
+ file_or_files.each { |e|
112
+ responses[e] = self.delete(
113
+ "/zones/pull.json/#{zone_id}/cache",
114
+ {'file' => e},
115
+ options
116
+ )
117
+ }
118
+ return responses
93
119
  end
94
120
 
95
- def put uri, data={}, options={}
96
- options[:body] = true
97
- self._response_as_json 'put', uri, options, data
98
- end
99
121
 
100
- def delete uri, data={}, options={}
101
- options[:body] = false
102
- self._response_as_json 'delete', uri, options, data
122
+ unless file_or_files.nil?
123
+ return self.delete(
124
+ "/zones/pull.json/#{zone_id}/cache",
125
+ {'file' => file_or_files},
126
+ options
127
+ )
103
128
  end
104
129
 
105
- def purge zone_id, file_or_files=nil, options={}
106
- if file_or_files.respond_to? :each
107
- options[:debug_json] = true if options[:debug_json].nil?
108
- responses = Hash.new
109
-
110
- file_or_files.each { |e|
111
- responses[e] = self.delete(
112
- "/zones/pull.json/#{zone_id}/cache",
113
- {'file' => e},
114
- options
115
- )
116
- }
117
- return responses
118
- end
130
+ self.delete("/zones/pull.json/#{zone_id}/cache", {}, options)
131
+ end
132
+ end
119
133
 
134
+ module Utils
135
+ RESERVED_CHARACTERS = /[^a-zA-Z0-9\-\.\_\~]/
120
136
 
121
- unless file_or_files.nil?
122
- return self.delete(
123
- "/zones/pull.json/#{zone_id}/cache",
124
- {'file' => file_or_files},
125
- options
126
- )
127
- end
137
+ def escape(value)
138
+ URI::escape(value.to_s, NetDNARWS::Utils::RESERVED_CHARACTERS)
139
+ rescue ArgumentError
140
+ URI::escape(value.to_s.force_encoding(Encoding::UTF_8), NetDNARWS::Utils::RESERVED_CHARACTERS)
141
+ end
128
142
 
129
- self.delete("/zones/pull.json/#{zone_id}/cache", {}, options)
130
- end
143
+ def encode_params params={}
144
+ Hash[params.map { |k, v| [NetDNARWS::Utils::escape(k), NetDNARWS::Utils::escape(v)] }]
145
+ end
131
146
  end
132
147
  end
data/netdnarws.gemspec CHANGED
@@ -13,7 +13,6 @@ Gem::Specification.new do |gem|
13
13
  gem.email = "devteam@netdna.com"
14
14
  gem.authors = ["NetDNA Developer Team"]
15
15
  gem.add_dependency 'json' if RUBY_VERSION.start_with? "1.8"
16
- gem.add_dependency 'addressable'
17
16
  gem.add_dependency 'signet'
18
17
  gem.add_dependency 'curb-fu'
19
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netdnarws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-13 00:00:00.000000000 Z
12
+ date: 2013-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: addressable
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: signet
32
16
  requirement: !ruby/object:Gem::Requirement