resterl 0.0.6 → 0.0.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -3,7 +3,8 @@ require 'hashie'
3
3
  class Resterl::BaseObject #< Hashie::Mash
4
4
 
5
5
  include ClassLevelInheritableAttributes
6
- cattr_inheritable :resterl_client, :parser, :complete_mime_type, :mapper
6
+ cattr_inheritable :resterl_client, :parser, :composer, :complete_mime_type,
7
+ :mapper
7
8
  attr_reader :response
8
9
 
9
10
  #self.resterl_client = nil
@@ -30,14 +31,30 @@ class Resterl::BaseObject #< Hashie::Mash
30
31
  doc = mapper.map(doc) if @mapper
31
32
  new(doc, response)
32
33
  end
34
+
35
+ def self.post_to_object url, params = {}, data = {}
36
+ headers = {
37
+ 'Accept' => complete_mime_type,
38
+ 'Content-Type' => complete_mime_type
39
+ }
40
+ data = composer.call(data)
41
+ response = resterl_client.post(url, params, data, headers)
42
+ end
33
43
 
34
44
  def self.mime_type= t
35
- self.parser, self.complete_mime_type = case t
45
+ self.parser, self.composer, self.complete_mime_type = case t
36
46
  when :json
37
- [proc {|str| JSON.parse(str)}, 'application/json']
47
+ # TODO prio 2: Only works when Rails is loaded?
48
+ [ proc {|str| JSON.parse(str)},
49
+ proc(&:to_json),
50
+ 'application/json'
51
+ ]
38
52
  when :xml
39
- # TODO: Only works when Rails is loaded?
40
- [proc {|str| Hash.from_xml(str)}, 'application/xml']
53
+ # TODO prio 2: Only works when Rails is loaded?
54
+ [ proc {|str| Hash.from_xml(str)},
55
+ proc(&:to_xml),
56
+ 'application/xml'
57
+ ]
41
58
  end
42
59
  end
43
60
 
@@ -32,6 +32,15 @@ class Resterl::Client
32
32
  new_get_request url, cache_key, params, headers, old_response
33
33
  end
34
34
  end
35
+
36
+ def post url, params, data, headers
37
+ # Caching nicht notwendig
38
+ url = setup_url url
39
+ request = Resterl::PostRequest.new(self, url, params, data, headers)
40
+ response = request.perform.response
41
+
42
+ response
43
+ end
35
44
 
36
45
  private
37
46
 
@@ -47,7 +56,7 @@ class Resterl::Client
47
56
  end
48
57
 
49
58
  # Anfrage stellen, ggf. ETag mit übergeben
50
- request = Resterl::Request.new(self, url, params, headers)
59
+ request = Resterl::GetRequest.new(self, url, params, headers)
51
60
  new_response = request.perform.response
52
61
 
53
62
  response, max_age_seconds = case new_response
@@ -84,8 +93,12 @@ class Resterl::Client
84
93
  end
85
94
 
86
95
  def setup_url url
87
- bu = options[:base_uri]
88
- bu ? "#{bu}#{url}" : url
96
+ if url =~ /^http/
97
+ url
98
+ else
99
+ bu = options[:base_uri]
100
+ bu ? "#{bu}#{url}" : url
101
+ end
89
102
  end
90
103
 
91
104
  def data_to_cache_key *args
@@ -0,0 +1,45 @@
1
+ class Resterl::GenericRequest
2
+ attr_accessor :rest_client, :url, :body, :response
3
+ DEFAULT_HEADERS = {}
4
+
5
+ def initialize client, url, query_params, headers
6
+ @rest_client = client
7
+ @url = url
8
+ @query_params = query_params
9
+ @headers = DEFAULT_HEADERS.merge headers
10
+ end
11
+
12
+ private
13
+
14
+ def get_http_object_and_query_path
15
+ uri = URI.parse(url)
16
+ http = Net::HTTP.new(uri.host, uri.port)
17
+ apply_ssl http, uri
18
+
19
+ path_with_query = uri.path
20
+ path_with_query << "?#{query_param_string}" unless query_param_string.blank?
21
+
22
+ [http, path_with_query]
23
+ end
24
+
25
+ def query_param_string
26
+ @query_param_string ||= begin
27
+ @query_params.collect do |key, value|
28
+ "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}"
29
+ end.join('&')
30
+ end
31
+ end
32
+ def apply_basic_auth request
33
+ ba = rest_client.options[:basic_auth]
34
+ request.basic_auth(ba[:username], ba[:password]) if ba
35
+ end
36
+ def apply_ssl http, uri
37
+ if uri.is_a? URI::HTTPS
38
+ http.use_ssl = true
39
+ http.verify_mode = rest_client.options[:ssl_verify_mode]
40
+ end
41
+ end
42
+ def redirect_url
43
+ response['location'] || response.body.match(/<a href=\"([^>]+)\">/i)[1]
44
+ end
45
+ end
@@ -0,0 +1,29 @@
1
+ # Redirect code from:
2
+ # http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/
3
+ class Resterl::GetRequest < Resterl::GenericRequest
4
+ attr_accessor :redirect_limit
5
+
6
+ def initialize client, url, query_params, headers
7
+ super client, url, query_params, headers
8
+ @redirect_limit = @rest_client.options[:max_redirect_depth]
9
+ end
10
+
11
+ def perform
12
+ raise TooManyRedirects if redirect_limit < 0
13
+
14
+ http, path = get_http_object_and_query_path
15
+ request = Net::HTTP::Get.new path, @headers
16
+ apply_basic_auth request
17
+ self.response = http.request(request)
18
+
19
+ # Follow redirects
20
+ if response.is_a?(Net::HTTPRedirection) &&
21
+ !response.is_a?(Net::HTTPNotModified)
22
+ self.url = redirect_url
23
+ self.redirect_limit -= 1
24
+ perform
25
+ end
26
+ self
27
+ end
28
+
29
+ end
@@ -0,0 +1,16 @@
1
+ class Resterl::PostRequest < Resterl::GenericRequest
2
+ def initialize client, url, query_params, data, headers
3
+ super client, url, query_params, headers
4
+ @data = data
5
+ end
6
+ def perform
7
+ http, path = get_http_object_and_query_path
8
+ request = Net::HTTP::Post.new path, @headers
9
+ apply_basic_auth request
10
+ request.body = @data
11
+ self.response = http.request(request)
12
+
13
+ self
14
+ end
15
+
16
+ end
data/lib/resterl.rb CHANGED
@@ -15,7 +15,9 @@ require 'resterl/caches/rails_memcached_cache'
15
15
  require 'resterl/caches/key_prefix_decorator'
16
16
 
17
17
  require 'resterl/client'
18
- require 'resterl/request'
18
+ require 'resterl/generic_request'
19
+ require 'resterl/get_request'
20
+ require 'resterl/post_request'
19
21
  require 'resterl/response'
20
22
  require 'resterl/base_object'
21
23
 
data/resterl.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{resterl}
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Florian D\303\274tsch"]
12
- s.date = %q{2011-01-26}
12
+ s.date = %q{2011-02-08}
13
13
  s.email = %q{florian.duetsch@nix-wie-weg.de}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -31,7 +31,9 @@ Gem::Specification.new do |s|
31
31
  "lib/resterl/caches/simple_cache.rb",
32
32
  "lib/resterl/class_level_inheritable_attributes.rb",
33
33
  "lib/resterl/client.rb",
34
- "lib/resterl/request.rb",
34
+ "lib/resterl/generic_request.rb",
35
+ "lib/resterl/get_request.rb",
36
+ "lib/resterl/post_request.rb",
35
37
  "lib/resterl/response.rb",
36
38
  "resterl.gemspec",
37
39
  "test/helper.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resterl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Florian D\xC3\xBCtsch"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-26 00:00:00 +01:00
18
+ date: 2011-02-08 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -89,7 +89,9 @@ files:
89
89
  - lib/resterl/caches/simple_cache.rb
90
90
  - lib/resterl/class_level_inheritable_attributes.rb
91
91
  - lib/resterl/client.rb
92
- - lib/resterl/request.rb
92
+ - lib/resterl/generic_request.rb
93
+ - lib/resterl/get_request.rb
94
+ - lib/resterl/post_request.rb
93
95
  - lib/resterl/response.rb
94
96
  - resterl.gemspec
95
97
  - test/helper.rb
@@ -1,72 +0,0 @@
1
- # Redirect code from:
2
- # http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/
3
- class Resterl::Request
4
- attr_accessor :rest_client, :url, :body, :redirect_limit, :response
5
- DEFAULT_HEADERS = {}
6
-
7
- def initialize client, url, query_params, headers
8
- @rest_client = client
9
- @url = url
10
- @query_params = query_params
11
- @redirect_limit = @rest_client.options[:max_redirect_depth]
12
- @headers = DEFAULT_HEADERS.merge headers
13
- end
14
- def perform
15
- raise TooManyRedirects if redirect_limit < 0
16
-
17
- # build URL
18
- #complete_url = url.dup
19
- #complete_url << "?#{query_param_string}" unless query_param_string.blank?
20
- #puts complete_url
21
- uri = URI.parse(url)#complete_url)
22
- #puts uri.path
23
-
24
-
25
- http = Net::HTTP.new(uri.host, uri.port)
26
- apply_ssl http, uri
27
-
28
- path_with_query = uri.path
29
- path_with_query << "?#{query_param_string}" unless query_param_string.blank?
30
- request = Net::HTTP::Get.new path_with_query, @headers
31
- apply_basic_auth request
32
- self.response = http.request(request)
33
-
34
- # Follow redirects
35
- if response.is_a?(Net::HTTPRedirection) &&
36
- !response.is_a?(Net::HTTPNotModified)
37
- self.url = redirect_url
38
- self.redirect_limit -= 1
39
- perform
40
- end
41
- self
42
- end
43
-
44
- private
45
-
46
- def query_param_string
47
- @query_param_string ||= begin
48
- @query_params.collect do |key, value|
49
- "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}"
50
- end.join('&')
51
- end
52
- end
53
-
54
- def redirect_url
55
- if response['location'].nil?
56
- response.body.match(/<a href=\"([^>]+)\">/i)[1]
57
- else
58
- response['location']
59
- end
60
- end
61
-
62
- def apply_ssl http, uri
63
- if uri.is_a? URI::HTTPS
64
- http.use_ssl = true
65
- http.verify_mode = rest_client.options[:ssl_verify_mode]
66
- end
67
- end
68
- def apply_basic_auth request
69
- ba = rest_client.options[:basic_auth]
70
- request.basic_auth(ba[:username], ba[:password]) if ba
71
- end
72
- end