wrest 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGELOG +34 -0
  2. data/README.rdoc +16 -10
  3. data/Rakefile +361 -123
  4. data/VERSION.yml +2 -2
  5. data/examples/delicious.rb +17 -7
  6. data/examples/facebook.rb +101 -0
  7. data/examples/keep_alive.rb +37 -0
  8. data/examples/twitter.rb +3 -3
  9. data/examples/twitter_public_timeline.rb +11 -4
  10. data/examples/wow_realm_status.rb +8 -2
  11. data/{spec/functional/sample_rails_app/public/favicon.ico → init.rb} +0 -0
  12. data/lib/wrest/components/{attributes_container → container}/alias_accessors.rb +4 -4
  13. data/lib/wrest/components/{attributes_container → container}/typecaster.rb +1 -1
  14. data/lib/wrest/components/{attributes_container.rb → container.rb} +46 -16
  15. data/lib/wrest/components/mutators.rb +4 -4
  16. data/lib/wrest/components/translators/json.rb +2 -2
  17. data/lib/wrest/components/translators/xml.rb +3 -2
  18. data/lib/wrest/components/translators.rb +3 -3
  19. data/lib/wrest/components.rb +3 -3
  20. data/lib/wrest/core_ext/hash.rb +1 -1
  21. data/lib/wrest/core_ext/string.rb +1 -1
  22. data/lib/wrest/curl/delete.rb +23 -0
  23. data/lib/wrest/curl/get.rb +23 -0
  24. data/lib/wrest/curl/options.rb +16 -0
  25. data/lib/wrest/curl/post.rb +23 -0
  26. data/lib/wrest/curl/put.rb +23 -0
  27. data/lib/wrest/curl/request.rb +95 -0
  28. data/lib/wrest/curl/response.rb +63 -0
  29. data/lib/wrest/curl/session.rb +57 -0
  30. data/lib/wrest/curl.rb +49 -0
  31. data/lib/wrest/exceptions.rb +16 -1
  32. data/lib/wrest/http_shared/headers.rb +350 -0
  33. data/lib/wrest/http_shared/standard_headers.rb +21 -0
  34. data/lib/wrest/http_shared/standard_tokens.rb +18 -0
  35. data/lib/wrest/http_shared.rb +24 -0
  36. data/lib/wrest/native/connection_factory.rb +23 -0
  37. data/lib/wrest/{http → native}/delete.rb +1 -1
  38. data/lib/wrest/{http → native}/get.rb +1 -1
  39. data/lib/wrest/{http → native}/options.rb +1 -1
  40. data/lib/wrest/{http → native}/post.rb +1 -1
  41. data/lib/wrest/{http → native}/put.rb +1 -1
  42. data/lib/wrest/{http → native}/redirection.rb +4 -1
  43. data/lib/wrest/{http → native}/request.rb +32 -20
  44. data/lib/wrest/{http → native}/response.rb +12 -4
  45. data/lib/wrest/native/session.rb +57 -0
  46. data/lib/wrest/native.rb +32 -0
  47. data/lib/wrest/resource/base.rb +1 -1
  48. data/lib/wrest/resource.rb +1 -1
  49. data/lib/wrest/test/request_patches.rb +5 -0
  50. data/lib/wrest/test.rb +1 -0
  51. data/lib/wrest/uri.rb +31 -3
  52. data/lib/wrest/version.rb +2 -2
  53. data/lib/wrest.rb +52 -16
  54. data/spec/unit/spec_helper.rb +12 -3
  55. data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +2 -2
  56. data/spec/unit/wrest/components/attributes_container/typecaster_spec.rb +6 -6
  57. data/spec/unit/wrest/components/attributes_container_spec.rb +44 -12
  58. data/spec/unit/wrest/components/translators/xml_spec.rb +7 -3
  59. data/spec/unit/wrest/curl/request_spec.rb +19 -0
  60. data/spec/unit/wrest/curl/response_spec.rb +16 -0
  61. data/spec/unit/wrest/http/response_spec.rb +17 -38
  62. data/spec/unit/wrest/{http → native}/redirection_spec.rb +5 -5
  63. data/spec/unit/wrest/{http → native}/request_spec.rb +15 -14
  64. data/spec/unit/wrest/native/response_spec.rb +72 -0
  65. data/spec/unit/wrest/native/session_spec.rb +74 -0
  66. data/spec/unit/wrest/resource/base_spec.rb +2 -2
  67. data/spec/unit/wrest/uri_spec.rb +51 -11
  68. data/wrest.gemspec +164 -0
  69. metadata +51 -165
  70. data/lib/wrest/http.rb +0 -25
  71. data/spec/functional/sample_rails_app/README +0 -3
  72. data/spec/functional/sample_rails_app/Rakefile +0 -10
  73. data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +0 -10
  74. data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +0 -7
  75. data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +0 -3
  76. data/spec/functional/sample_rails_app/app/models/bottle.rb +0 -3
  77. data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +0 -3
  78. data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +0 -3
  79. data/spec/functional/sample_rails_app/config/boot.rb +0 -110
  80. data/spec/functional/sample_rails_app/config/database.yml +0 -16
  81. data/spec/functional/sample_rails_app/config/environment.rb +0 -42
  82. data/spec/functional/sample_rails_app/config/environments/development.rb +0 -17
  83. data/spec/functional/sample_rails_app/config/environments/production.rb +0 -28
  84. data/spec/functional/sample_rails_app/config/environments/test.rb +0 -28
  85. data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +0 -7
  86. data/spec/functional/sample_rails_app/config/initializers/inflections.rb +0 -10
  87. data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +0 -5
  88. data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +0 -19
  89. data/spec/functional/sample_rails_app/config/initializers/session_store.rb +0 -15
  90. data/spec/functional/sample_rails_app/config/locales/en.yml +0 -5
  91. data/spec/functional/sample_rails_app/config/routes.rb +0 -3
  92. data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
  93. data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +0 -13
  94. data/spec/functional/sample_rails_app/db/schema.rb +0 -20
  95. data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
  96. data/spec/functional/sample_rails_app/log/development.log +0 -1
  97. data/spec/functional/sample_rails_app/public/404.html +0 -30
  98. data/spec/functional/sample_rails_app/public/422.html +0 -30
  99. data/spec/functional/sample_rails_app/public/500.html +0 -30
  100. data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
  101. data/spec/functional/sample_rails_app/public/index.html +0 -275
  102. data/spec/functional/sample_rails_app/public/robots.txt +0 -5
  103. data/spec/functional/sample_rails_app/script/about +0 -4
  104. data/spec/functional/sample_rails_app/script/autospec +0 -6
  105. data/spec/functional/sample_rails_app/script/console +0 -3
  106. data/spec/functional/sample_rails_app/script/dbconsole +0 -3
  107. data/spec/functional/sample_rails_app/script/destroy +0 -3
  108. data/spec/functional/sample_rails_app/script/generate +0 -3
  109. data/spec/functional/sample_rails_app/script/performance/benchmarker +0 -3
  110. data/spec/functional/sample_rails_app/script/performance/profiler +0 -3
  111. data/spec/functional/sample_rails_app/script/plugin +0 -3
  112. data/spec/functional/sample_rails_app/script/runner +0 -3
  113. data/spec/functional/sample_rails_app/script/server +0 -3
  114. data/spec/functional/sample_rails_app/script/spec +0 -10
  115. data/spec/functional/sample_rails_app/script/spec_server +0 -9
  116. data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +0 -9
  117. data/spec/functional/sample_rails_app/test/test_helper.rb +0 -38
  118. data/spec/functional/sample_rails_app/tmtags +0 -2559
  119. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +0 -20
  120. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +0 -100
  121. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +0 -18
  122. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +0 -5
  123. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +0 -1
  124. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +0 -140
  125. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +0 -16
  126. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +0 -26
  127. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +0 -16
  128. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +0 -26
  129. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +0 -23
  130. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +0 -15
  131. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +0 -235
  132. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +0 -84
  133. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +0 -337
  134. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +0 -50
  135. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +0 -75
  136. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +0 -65
  137. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +0 -63
  138. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +0 -74
  139. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +0 -9
  140. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +0 -14
  141. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +0 -88
  142. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +0 -29
  143. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +0 -262
  144. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +0 -62
  145. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +0 -57
  146. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +0 -462
  147. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +0 -4
  148. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +0 -107
  149. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +0 -98
  150. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +0 -5
  151. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +0 -173
  152. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +0 -98
  153. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +0 -1
  154. data/spec/functional/spec_helper.rb +0 -0
@@ -12,10 +12,14 @@ require 'pp'
12
12
 
13
13
  Wrest.logger = Logger.new(STDOUT)
14
14
  Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
15
+ # Wrest.use_curl
15
16
 
16
17
  # This example demonstrates the usage of GET, POST, PUT and
17
18
  # DELETE over HTTPS. Its also shows how Wrest::Uris can have
18
- # paths extended making accessing an API easy as pie.
19
+ # paths extended making accessing an API easy as pie.
20
+ #
21
+ # Do remember to change the username and password on line 47
22
+ # before running this example.
19
23
  #
20
24
  # API reference: http://delicious.com/help/api
21
25
  class Delicious
@@ -32,7 +36,7 @@ class Delicious
32
36
  end
33
37
 
34
38
  def bookmark(parameters)
35
- @uri['/add'].post('', {}, parameters)
39
+ @uri['/add'].post_form(parameters)
36
40
  end
37
41
 
38
42
  def delete(parameters)
@@ -42,6 +46,8 @@ end
42
46
 
43
47
  account = Delicious.new :username => 'kaiwren', :password => 'fupupp1es'
44
48
 
49
+ puts '*'*20 + "Creating bookmark tagged Rails" + '*'*20
50
+
45
51
  pp account.bookmark(
46
52
  :url => 'http://blog.sidu.in/search/label/ruby',
47
53
  :description => 'The Ruby related posts on my blog!',
@@ -49,14 +55,18 @@ pp account.bookmark(
49
55
  :tags => 'ruby hacking'
50
56
  ).deserialise
51
57
 
52
- puts '', '*'*70, ''
58
+ puts '*'*20 + "Listing bookmarks tagged Rails on a certain date" + '*'*20
53
59
 
54
60
  pp account.bookmarks(:tag => 'rails', :dt => '20090712').deserialise
55
61
 
56
- puts '', '*'*70, ''
62
+ puts '*'*20 + "Listing recent bookmarks" + '*'*20
63
+
64
+ pp account.recent(:tag => 'ruby').deserialise["posts"]["post"].collect{|bookmark| bookmark['href']}
65
+
66
+ puts '*'*20 + "Deleting the bookmark we just created" + '*'*20
57
67
 
58
- pp recently_saved_uris = account.recent(:tag => 'ruby').deserialise["posts"]["post"].collect{|bookmark| bookmark['href']}
68
+ pp account.delete(:url => 'http://blog.sidu.in/search/label/ruby').deserialise
59
69
 
60
- puts '', '*'*70, ''
70
+ puts '*'*20 + "Listing recent bookmarks" + '*'*20
61
71
 
62
- pp account.delete(:url => 'http://blog.sidu.in/search/label/ruby').deserialise
72
+ pp account.recent(:tag => 'ruby').deserialise["posts"]["post"].collect{|bookmark| bookmark['href']}
@@ -0,0 +1,101 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")
11
+ require 'digest/md5'
12
+
13
+ Wrest.logger = Logger.new(STDOUT)
14
+ Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
15
+ # Wrest.use_curl
16
+
17
+ include Wrest
18
+
19
+ # This example demonstrates fetching a Facebook user's public profile
20
+ # given his or her Facebook UID. It also shows how strings in the
21
+ # deserialised response can be easily converted into other objects
22
+ # using Wrest's typecast feature.
23
+
24
+ module Facebook
25
+ # This key and secret are both fake. To get your own key and secret, create a
26
+ # Facebook application at http://www.facebook.com/developers/apps.php
27
+ Config ={
28
+ :key => '8be7dc2e480d8bf21a58915bad8c362e',
29
+ :secret => '1a4638b19c393b540fda796f936a25d4',
30
+ :restserver => 'http://api.facebook.com/restserver.php'
31
+ }
32
+
33
+ module API
34
+ Defaults = {
35
+ 'v' => '1.0',
36
+ 'format' => 'XML',
37
+ 'api_key' => Config[:key]
38
+ }
39
+
40
+ def self.signature_for(params)
41
+ # http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application
42
+ request_str = params.keys.sort.map{|k| "#{k}=#{params[k]}" }.join
43
+ Digest::MD5.hexdigest(request_str + Config[:secret])
44
+ end
45
+
46
+ def self.invoke(args)
47
+ args = API::Defaults.merge(args)
48
+ Config[:restserver].to_uri.post_form(args.merge('sig' => Facebook::API.signature_for(args))).deserialise
49
+ end
50
+ end
51
+
52
+ class Profile
53
+ include Components::Container
54
+ typecast :uid => as_integer,
55
+ :pic_square => lambda{|pic_square| pic_square.to_uri}
56
+
57
+ Fields = %w(
58
+ uid
59
+ first_name
60
+ last_name
61
+ name
62
+ pic_square
63
+ ).join(', ')
64
+
65
+ def self.find(fcbk_uid)
66
+ hash = Facebook::API.invoke({
67
+ 'method' => 'facebook.users.getInfo',
68
+ 'fields' => Profile::Fields,
69
+ 'uids' => fcbk_uid
70
+ })
71
+
72
+ if hash['error_response']
73
+ Facebook::Error.new hash['error_response']
74
+ else
75
+ self.new hash["users_getInfo_response"]["user"]
76
+ end
77
+ end
78
+ end
79
+
80
+ class Error
81
+ include Components::Container
82
+ typecast :error_code => as_integer
83
+ end
84
+ end
85
+
86
+ puts 'Fetching public profile for Facebook user 699497090.'
87
+ puts 'Note that we use Wrest\'s typecast feature to convert the uid and pic_square fields in the case of a valid result, and the error_msg and error_code fields in the case of an Error to appropriate types from simple strings.'
88
+ puts
89
+
90
+ profile = Facebook::Profile.find('699497090')
91
+
92
+ puts "Field\t\t\tData\t\t\tClass"
93
+
94
+ if profile.is_a? Facebook::Error
95
+ puts "Error\t\t\t#{profile.error_msg}\t\t\t#{profile.error_msg.class}"
96
+ puts "Code\t\t\t#{profile.error_code}\t\t\t#{profile.error_code.class}"
97
+ else
98
+ puts "UID\t\t\t#{profile.uid}\t\t\t#{profile.uid.class}"
99
+ puts "Name\t\t\t#{profile.first_name}\t\t\t#{profile.first_name.class}"
100
+ puts "Pic Url\t\t\t#{profile.pic_square}\t\t\t#{profile.pic_square.class}"
101
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")
11
+ require 'pp'
12
+
13
+ Wrest.logger = Logger.new(STDOUT)
14
+ Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
15
+ include Wrest
16
+
17
+
18
+ # This is a basic example demonstrating using keep-alive connections.
19
+ # Observe the requests logs - they will look something like this:
20
+ # --> (GET 12207030 12546830) http://github.com:80/api/v1/json/kaiwren
21
+ #
22
+ # The second number after the GET is a hash identifying the connection used.
23
+ # You will notice that all requests have the same hash therefore use the same connection.
24
+
25
+ Http::Session.new('http://github.com/api/v1/json') do |s|
26
+ puts "Response Connection Header - a response token of 'Keep-Alive' indicates that the server has created a keep-alive connection"
27
+ puts
28
+ puts s.get('/kaiwren')['Connection']
29
+ puts s.get('/niranjan')['Connection']
30
+
31
+ puts
32
+ puts '*' * 10
33
+ puts
34
+
35
+ pp s.get('/kaiwren').deserialise
36
+ pp s.get('/niranjan').deserialise
37
+ end
data/examples/twitter.rb CHANGED
@@ -31,7 +31,7 @@ class Twitter
31
31
  end
32
32
 
33
33
  def post(text)
34
- Tweet.new @uri['/statuses/update.json'].post('', {'User-Agent' => "Wrest/#{Wrest::VERSION::STRING}"}, {:status => text}).deserialise
34
+ Tweet.new @uri['/statuses/update.json'].post('', {'User-Agent' => "Wrest/#{Wrest::VERSION::STRING}"}, :status => text).deserialise
35
35
  end
36
36
  end
37
37
 
@@ -40,7 +40,7 @@ class TwitterUser
40
40
  # for the deserialised data from a response.
41
41
  #
42
42
  # All the keys in the hash are exposed as methods.
43
- include Wrest::Components::AttributesContainer
43
+ include Wrest::Components::Container
44
44
 
45
45
  # We'd prefer the user's profile url to be
46
46
  # a Wrest::Uri rather than a String, wouldn't we?
@@ -52,7 +52,7 @@ class TwitterUser
52
52
  end
53
53
 
54
54
  class Tweet
55
- include Wrest::Components::AttributesContainer
55
+ include Wrest::Components::Container
56
56
 
57
57
  typecast :user => lambda{|user| TwitterUser.new(user) }
58
58
  end
@@ -13,13 +13,20 @@ require 'pp'
13
13
  Wrest.logger = Logger.new(STDOUT)
14
14
  Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
15
15
 
16
+ # Optionally uncomment the following line to use the significantly faster libcurl library.
17
+ # This uses the patron gem - (sudo) gem install patron
18
+ # IMPORTANT: Libcurl support is currently in alpha and is both incomplete and unstable!
19
+ # Wrest.use_curl
16
20
 
17
- # This is a basic example demonstrating GET and json deserialisation.
21
+ # This is a basic example demonstrating GET and json deserialisation. The timeout field is optional, of course (it defaults to 60), but you can reduce it to a low number like 1 to force the request to timeout.
18
22
 
19
- response = 'http://twitter.com/statuses/public_timeline.json'.to_uri.get
23
+ response = 'http://twitter.com/statuses/public_timeline.json'.to_uri(:timeout => 5).get
20
24
 
21
25
  puts "Code: #{response.code}"
22
26
  puts "Message: #{response.message}"
23
27
  puts "Headers: #{response.headers.inspect}"
24
- puts "Body: #{response.body.inspect}"
25
- puts "Deserialised: #{response.deserialise.inspect}"
28
+ puts
29
+ puts "Deserialised Body: #{response.deserialise.inspect}"
30
+ puts '*' * 50
31
+ puts '*' * 50
32
+ puts "Body: #{response.body.inspect}"
@@ -12,10 +12,16 @@ require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")
12
12
  Wrest.logger = Logger.new(STDOUT)
13
13
  Wrest.logger.level = Logger::DEBUG # Set this to Logger::INFO or higher to disable request logging
14
14
 
15
+ # Optionally uncomment the following line to use the significantly faster libcurl library.
16
+ # This uses the patron gem - (sudo) gem install patron
17
+ # IMPORTANT: Libcurl support is currently in alpha and is both incomplete and unstable!
18
+ # Wrest.use_curl
19
+
20
+
15
21
  include Wrest
16
22
 
17
23
  class Realm
18
- include Components::AttributesContainer
24
+ include Components::Container
19
25
 
20
26
  typecast :t => lambda{|type|
21
27
  case type
@@ -54,7 +60,7 @@ realms = "http://www.worldofwarcraft.com/realmstatus/status.xml".to_uri.get.dese
54
60
 
55
61
  puts "Status of Nagrand: #{realms.find{|realm| realm.name == 'Nagrand'}.status}"
56
62
  puts
57
- puts "All Available Realms:"
63
+ puts "Listing All Available Realms:"
58
64
  puts
59
65
  puts "Realm\tLoad\tType"
60
66
  puts "-----------"
@@ -8,7 +8,7 @@
8
8
  # See the License for the specific language governing permissions and limitations under the License.
9
9
 
10
10
  module Wrest
11
- module Components::AttributesContainer
11
+ module Components::Container
12
12
  module AliasAccessors
13
13
  def self.included(klass) #:nodoc:
14
14
  klass.extend AliasAccessors::ClassMethods
@@ -33,7 +33,7 @@ module Wrest
33
33
  # sevice/resource.
34
34
  #
35
35
  # For example, lets say that a particular resource exposes a
36
- # User's age as 'a' and sex as s. Typically, you'd have to access it as
36
+ # User's age as 'a' and sex as 's'. Typically, you'd have to access it as
37
37
  # user.a and user.s whereas you's like to access it as user.age and user.sex.
38
38
  # This is where alias_accessors comes into the picture. Your User class would
39
39
  # look somethig like this:
@@ -44,8 +44,8 @@ module Wrest
44
44
  # alias_accessors :a => :age,
45
45
  # :s => :sex
46
46
  # end
47
- # This would create the methods user.age, user.age= and user.age? which delegate
48
- # to user.a, user.a= and user.a? respectively. Ditto for s to sex.
47
+ # This would create the methods user.age, user.age= and user.age? which delegates
48
+ # to user.a, user.a= and user.a? respectively.
49
49
  #
50
50
  # See examples/wow_realm_status.rb for a working example.
51
51
  #
@@ -8,7 +8,7 @@
8
8
  # See the License for the specific language governing permissions and limitations under the License.
9
9
 
10
10
  module Wrest
11
- module Components::AttributesContainer
11
+ module Components::Container
12
12
  # An extension to AttributesContainer that adds support for
13
13
  # specifying how the values associated with certain attribute keys
14
14
  # should be typecast.
@@ -8,12 +8,12 @@
8
8
  # See the License for the specific language governing permissions and limitations under the License.
9
9
 
10
10
  module Wrest
11
- module Components::AttributesContainer
11
+ module Components::Container
12
12
  end
13
13
  end
14
14
 
15
- require "#{WREST_ROOT}/wrest/components/attributes_container/typecaster"
16
- require "#{WREST_ROOT}/wrest/components/attributes_container/alias_accessors"
15
+ require "#{Wrest::Root}/wrest/components/container/typecaster"
16
+ require "#{Wrest::Root}/wrest/components/container/alias_accessors"
17
17
 
18
18
  module Wrest::Components
19
19
 
@@ -55,13 +55,13 @@ module Wrest::Components
55
55
  # coin = ShenCoin.new(:id => '5', :chi_count => 500, :owner => 'Kai Wren')
56
56
  # coin.id # => 5
57
57
  # coin.owner # => 'Kai Wren'
58
- module AttributesContainer
58
+ module Container
59
59
  def self.included(klass) #:nodoc:
60
- klass.extend AttributesContainer::ClassMethods
61
- klass.extend AttributesContainer::Typecaster::Helpers
60
+ klass.extend Container::ClassMethods
61
+ klass.extend Container::Typecaster::Helpers
62
62
  klass.class_eval do
63
- include AttributesContainer::InstanceMethods
64
- include AttributesContainer::AliasAccessors
63
+ include Container::InstanceMethods
64
+ include Container::AliasAccessors
65
65
  end
66
66
  end
67
67
 
@@ -88,9 +88,9 @@ module Wrest::Components
88
88
  def always_has(*attribute_names)
89
89
  attribute_names.each do |attribute_name|
90
90
  self.class_eval(
91
- AttributesContainer.build_attribute_getter(attribute_name) +
92
- AttributesContainer.build_attribute_setter(attribute_name) +
93
- AttributesContainer.build_attribute_queryer(attribute_name)
91
+ Container.build_attribute_getter(attribute_name) +
92
+ Container.build_attribute_setter(attribute_name) +
93
+ Container.build_attribute_queryer(attribute_name)
94
94
  )
95
95
  end
96
96
  end
@@ -104,9 +104,20 @@ module Wrest::Components
104
104
  # Remember that using typecast carries a performance penalty.
105
105
  # See Wrest::Components::AttributesContainer::Typecaster for the actual docs.
106
106
  def typecast(cast_map)
107
- self.class_eval{ include Wrest::Components::AttributesContainer::Typecaster }
107
+ self.class_eval{ include Wrest::Components::Container::Typecaster }
108
108
  self.typecast cast_map
109
109
  end
110
+
111
+ # This is the name of the class in snake-case, with any parent
112
+ # module names removed.
113
+ #
114
+ # The class will use as the root element when
115
+ # serialised to xml after replacing underscores with hyphens.
116
+ #
117
+ # This method can be overidden should you need a different name.
118
+ def element_name
119
+ @element_name ||= self.name.demodulize.underscore.underscore
120
+ end
110
121
  end
111
122
 
112
123
  module InstanceMethods
@@ -119,7 +130,26 @@ module Wrest::Components
119
130
  def initialize(attributes = {})
120
131
  @attributes = attributes.symbolize_keys
121
132
  end
122
-
133
+
134
+ # A translator is a anything that knows how to serialise a
135
+ # Hash. It must needs have a method named 'serialise' that
136
+ # accepts a hash and configuration options, and returns the serialised
137
+ # result (leaving the hash unchanged, of course).
138
+ #
139
+ # Examples for JSON and XML can be found under Wrest::Components::Translators.
140
+ # These serialised output of these translators will work out of the box for Rails
141
+ # applications; you may need to roll your own for anything else.
142
+ #
143
+ # Note: When serilising to XML, if you want the name of the class as the name of the root node
144
+ # then you should use the AttributesContainer#to_xml helper.
145
+ def serialise_using(translator, options = {})
146
+ translator.serialise(@attributes, options)
147
+ end
148
+
149
+ def to_xml(options = {})
150
+ serialise_using(Wrest::Components::Translators::Xml, {:root => self.class.element_name}.merge(options))
151
+ end
152
+
123
153
  def [](key)
124
154
  @attributes[key.to_sym]
125
155
  end
@@ -140,11 +170,11 @@ module Wrest::Components
140
170
  if @attributes.include?(attribute_name.to_sym) || method_name.last == '=' || method_name.last == '?'
141
171
  case method_name.last
142
172
  when '='
143
- self.instance_eval AttributesContainer.build_attribute_setter(attribute_name)
173
+ self.instance_eval Container.build_attribute_setter(attribute_name)
144
174
  when '?'
145
- self.instance_eval AttributesContainer.build_attribute_queryer(attribute_name)
175
+ self.instance_eval Container.build_attribute_queryer(attribute_name)
146
176
  else
147
- self.instance_eval AttributesContainer.build_attribute_getter(attribute_name)
177
+ self.instance_eval Container.build_attribute_getter(attribute_name)
148
178
  end
149
179
  send(method_sym, *arguments)
150
180
  else
@@ -33,7 +33,7 @@ module Wrest
33
33
  end
34
34
  end
35
35
 
36
- require "#{WREST_ROOT}/wrest/components/mutators/base"
37
- require "#{WREST_ROOT}/wrest/components/mutators/xml_simple_type_caster"
38
- require "#{WREST_ROOT}/wrest/components/mutators/xml_mini_type_caster"
39
- require "#{WREST_ROOT}/wrest/components/mutators/camel_to_snake_case"
36
+ require "#{Wrest::Root}/wrest/components/mutators/base"
37
+ require "#{Wrest::Root}/wrest/components/mutators/xml_simple_type_caster"
38
+ require "#{Wrest::Root}/wrest/components/mutators/xml_mini_type_caster"
39
+ require "#{Wrest::Root}/wrest/components/mutators/camel_to_snake_case"
@@ -17,8 +17,8 @@ module Wrest
17
17
  ActiveSupport::JSON.decode(response.body)
18
18
  end
19
19
 
20
- def serialise(hash)
21
- ActiveSupport::JSON.encode(hash)
20
+ def serialise(hash, options = {})
21
+ hash.to_json(options)
22
22
  end
23
23
  end
24
24
  end
@@ -13,10 +13,11 @@ module Wrest
13
13
  extend self
14
14
 
15
15
  def deserialise(response)
16
- ActiveSupport::XmlMini.parse(response.body)
16
+ Hash.from_xml(response.body)
17
17
  end
18
18
 
19
- def serialise(hash)
19
+ def serialise(hash, options = {})
20
+ hash.to_xml(options)
20
21
  end
21
22
  end
22
23
  end
@@ -23,6 +23,6 @@ module Wrest
23
23
  end
24
24
  end
25
25
 
26
- require "#{WREST_ROOT}/wrest/components/translators/xml"
27
- require "#{WREST_ROOT}/wrest/components/translators/json"
28
- require "#{WREST_ROOT}/wrest/components/translators/content_types"
26
+ require "#{Wrest::Root}/wrest/components/translators/xml"
27
+ require "#{Wrest::Root}/wrest/components/translators/json"
28
+ require "#{Wrest::Root}/wrest/components/translators/content_types"
@@ -15,6 +15,6 @@ module Wrest
15
15
  end
16
16
  end
17
17
 
18
- require "#{WREST_ROOT}/wrest/components/attributes_container"
19
- require "#{WREST_ROOT}/wrest/components/mutators"
20
- require "#{WREST_ROOT}/wrest/components/translators"
18
+ require "#{Wrest::Root}/wrest/components/container"
19
+ require "#{Wrest::Root}/wrest/components/mutators"
20
+ require "#{Wrest::Root}/wrest/components/translators"
@@ -1,4 +1,4 @@
1
- require "#{WREST_ROOT}/wrest/core_ext/hash/conversions"
1
+ require "#{Wrest::Root}/wrest/core_ext/hash/conversions"
2
2
 
3
3
  class Hash #:nodoc:
4
4
  include Wrest::CoreExt::Hash::Conversions
@@ -1,4 +1,4 @@
1
- require "#{WREST_ROOT}/wrest/core_ext/string/conversions"
1
+ require "#{Wrest::Root}/wrest/core_ext/string/conversions"
2
2
 
3
3
  class String #:nodoc:
4
4
  include Wrest::CoreExt::String::Conversions
@@ -0,0 +1,23 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest::Curl
11
+ class Delete < Request
12
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
+ super(
14
+ wrest_uri,
15
+ :delete,
16
+ parameters,
17
+ nil,
18
+ headers,
19
+ options
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest::Curl
11
+ class Get < Request
12
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
13
+ super(
14
+ wrest_uri,
15
+ :get,
16
+ parameters,
17
+ nil,
18
+ headers,
19
+ options
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest::Curl
11
+ class Options < Request
12
+ def initialize(*args)
13
+ raise Wrest::Exceptions::UnsupportedHttpVerb, 'Options'
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest::Curl
11
+ class Post < Request
12
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
+ super(
14
+ wrest_uri,
15
+ :post,
16
+ parameters,
17
+ body,
18
+ headers,
19
+ options
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest::Curl
11
+ class Put < Request
12
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
13
+ super(
14
+ wrest_uri,
15
+ :put,
16
+ parameters,
17
+ body,
18
+ headers,
19
+ options
20
+ )
21
+ end
22
+ end
23
+ end