roar 0.12.9 → 1.0.0.beta1

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.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +8 -10
  4. data/CHANGES.markdown +24 -2
  5. data/Gemfile +5 -2
  6. data/README.markdown +132 -28
  7. data/TODO.markdown +9 -7
  8. data/examples/example.rb +2 -0
  9. data/examples/example_server.rb +19 -3
  10. data/gemfiles/Gemfile.representable-2.0 +5 -0
  11. data/gemfiles/Gemfile.representable-2.1 +5 -0
  12. data/lib/roar.rb +1 -1
  13. data/lib/roar/client.rb +38 -0
  14. data/lib/roar/{representer/feature/coercion.rb → coercion.rb} +2 -1
  15. data/lib/roar/decorator.rb +3 -11
  16. data/lib/roar/http_verbs.rb +88 -0
  17. data/lib/roar/hypermedia.rb +174 -0
  18. data/lib/roar/json.rb +55 -0
  19. data/lib/roar/json/collection.rb +3 -0
  20. data/lib/roar/{representer/json → json}/collection_json.rb +20 -20
  21. data/lib/roar/{representer/json → json}/hal.rb +33 -31
  22. data/lib/roar/json/hash.rb +3 -0
  23. data/lib/roar/json/json_api.rb +208 -0
  24. data/lib/roar/representer.rb +3 -36
  25. data/lib/roar/transport/faraday.rb +49 -0
  26. data/lib/roar/transport/net_http.rb +57 -0
  27. data/lib/roar/transport/net_http/request.rb +72 -0
  28. data/lib/roar/version.rb +1 -1
  29. data/lib/roar/xml.rb +54 -0
  30. data/roar.gemspec +5 -4
  31. data/test/client_test.rb +3 -3
  32. data/test/coercion_feature_test.rb +6 -3
  33. data/test/collection_json_test.rb +8 -10
  34. data/test/decorator_test.rb +27 -15
  35. data/test/faraday_http_transport_test.rb +13 -15
  36. data/test/hal_json_test.rb +16 -16
  37. data/test/hal_links_test.rb +3 -3
  38. data/test/http_verbs_test.rb +17 -22
  39. data/test/hypermedia_feature_test.rb +23 -45
  40. data/test/hypermedia_test.rb +11 -23
  41. data/test/integration/band_representer.rb +2 -2
  42. data/test/integration/runner.rb +4 -3
  43. data/test/integration/server.rb +13 -2
  44. data/test/integration/ssl_server.rb +1 -1
  45. data/test/json_api_test.rb +336 -0
  46. data/test/json_representer_test.rb +16 -12
  47. data/test/lib/runner.rb +134 -0
  48. data/test/lonely_test.rb +9 -0
  49. data/test/net_http_transport_test.rb +4 -4
  50. data/test/representer_test.rb +2 -2
  51. data/test/{lib/roar/representer/transport/net_http/request_test.rb → ssl_client_certs_test.rb} +43 -5
  52. data/test/test_helper.rb +12 -5
  53. data/test/xml_representer_test.rb +26 -166
  54. metadata +49 -29
  55. data/gemfiles/Gemfile.representable-1.7 +0 -6
  56. data/gemfiles/Gemfile.representable-1.8 +0 -6
  57. data/lib/roar/representer/feature/client.rb +0 -39
  58. data/lib/roar/representer/feature/http_verbs.rb +0 -95
  59. data/lib/roar/representer/feature/hypermedia.rb +0 -175
  60. data/lib/roar/representer/json.rb +0 -67
  61. data/lib/roar/representer/transport/faraday.rb +0 -50
  62. data/lib/roar/representer/transport/net_http.rb +0 -59
  63. data/lib/roar/representer/transport/net_http/request.rb +0 -75
  64. data/lib/roar/representer/xml.rb +0 -61
@@ -1,59 +0,0 @@
1
- require 'roar/representer/transport/net_http/request'
2
-
3
- module Roar
4
- module Representer
5
- # Implements the (HTTP) transport interface with Net::HTTP.
6
- module Transport
7
- # Low-level interface for HTTP. The #get_uri and friends accept an options and an optional block, invoke
8
- # the HTTP request and return the request object.
9
- #
10
- # The following options are available:
11
- class NetHTTP
12
-
13
- def get_uri(*options, &block)
14
- call(Net::HTTP::Get, *options, &block)
15
- end
16
-
17
- def post_uri(*options, &block)
18
- call(Net::HTTP::Post, *options, &block)
19
- end
20
-
21
- def put_uri(*options, &block)
22
- call(Net::HTTP::Put, *options, &block)
23
- end
24
-
25
- def delete_uri(*options, &block)
26
- call(Net::HTTP::Delete, *options, &block)
27
- end
28
-
29
- def patch_uri(*options, &block)
30
- call(Net::HTTP::Patch, *options, &block)
31
- end
32
-
33
- private
34
- def call(what, *args, &block)
35
- options = handle_deprecated_args(args)
36
- # TODO: generically handle return codes.
37
- Request.new(options).call(what, &block)
38
- end
39
-
40
- def handle_deprecated_args(args) # TODO: remove in 1.0.
41
- if args.size > 1
42
- warn %{DEPRECATION WARNING: #get_uri, #post_uri, #put_uri, #delete_uri and #patch_uri no longer accept positional arguments. Please call them as follows:
43
- get_uri(uri: "http://localhost/songs", as: "application/json")
44
- post_uri(uri: "http://localhost/songs", as: "application/json", body: "{'id': 1}")
45
- Thank you and have a lovely day.}
46
- return {:uri => args[0], :as => args[1]} if args.size == 2
47
- return {:uri => args[0], :as => args[2], :body => args[1]}
48
- end
49
-
50
- args.first
51
- end
52
-
53
- end
54
-
55
- class UnauthorizedError < RuntimeError # TODO: raise this from Faraday, too.
56
- end
57
- end
58
- end
59
- end
@@ -1,75 +0,0 @@
1
- require "net/http"
2
- require "uri"
3
- require "openssl"
4
-
5
- module Roar
6
- module Representer
7
- module Transport
8
- class NetHTTP
9
- class Request # TODO: implement me.
10
- def initialize(options)
11
- @uri = parse_uri(options[:uri]) # TODO: add :uri.
12
- @as = options[:as]
13
- @body = options[:body]
14
- @options = options
15
-
16
- @http = Net::HTTP.new(uri.host, uri.port)
17
- unless options[:pem_file].nil?
18
- pem = File.read(options[:pem_file])
19
- @http.use_ssl = true
20
- @http.cert = OpenSSL::X509::Certificate.new(pem)
21
- @http.key = OpenSSL::PKey::RSA.new(pem)
22
- @http.verify_mode = options[:ssl_verify_mode].nil? ? OpenSSL::SSL::VERIFY_PEER : options[:ssl_verify_mode]
23
- end
24
- end
25
-
26
- def call(what)
27
- @req = what.new(uri.request_uri)
28
-
29
- # if options[:ssl]
30
- # uri.port = Net::HTTP.https_default_port()
31
- # end
32
- https!
33
- basic_auth!
34
-
35
- req.content_type = as
36
- req["accept"] = as # TODO: test me. # DISCUSS: if Accept is not set, rails treats this request as as "text/html".
37
- req.body = body if body
38
-
39
- yield req if block_given?
40
-
41
- http.request(req).tap do |res|
42
- raise UnauthorizedError if res.is_a?(Net::HTTPUnauthorized) # FIXME: make this better. # DISCUSS: abstract all that crap here?
43
- end
44
- end
45
-
46
- def get
47
- call(Net::HTTP::Get)
48
- end
49
-
50
- private
51
- attr_reader :uri, :as, :body, :options, :req, :http
52
-
53
- def parse_uri(url)
54
- uri = URI(url)
55
- raise "Incorrect URL `#{url}`. Maybe you forgot http://?" if uri.instance_of?(URI::Generic)
56
- uri
57
- end
58
-
59
- def https!
60
- return unless uri.scheme == 'https'
61
-
62
- @http.use_ssl = true
63
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
64
- end
65
-
66
- def basic_auth!
67
- return unless options[:basic_auth]
68
-
69
- @req.basic_auth(*options[:basic_auth])
70
- end
71
- end
72
- end
73
- end
74
- end
75
- end
@@ -1,61 +0,0 @@
1
- require 'roar/representer'
2
- require 'roar/representer/feature/hypermedia'
3
- require 'representable/xml'
4
-
5
- module Roar
6
- # Includes #from_xml and #to_xml into your represented object.
7
- # In addition to that, some more options are available when declaring properties.
8
- module Representer
9
- module XML
10
- def self.included(base)
11
- base.class_eval do
12
- include Representer
13
- include Representable::XML
14
-
15
- extend ClassMethods
16
- include InstanceMethods # otherwise Representable overrides our #to_xml.
17
- end
18
- end
19
-
20
- module InstanceMethods
21
- def to_node(*args)
22
- before_serialize(*args)
23
- super
24
- end
25
-
26
- # Generic entry-point for rendering.
27
- def serialize(*args)
28
- to_xml(*args)
29
- end
30
-
31
- def deserialize(*args)
32
- from_xml(*args)
33
- end
34
- end
35
-
36
-
37
- module ClassMethods
38
- include Representable::XML::ClassMethods
39
-
40
- def links_definition_options
41
- # FIXME: this doesn't belong into the generic XML representer.
42
- [:links_array, {:as => :link, :class => Feature::Hypermedia::Hyperlink, :collection => true, :extend => XML::HyperlinkRepresenter,
43
- :decorator_scope => true}] # TODO: merge with JSON.
44
- end
45
-
46
- # Generic entry-point for parsing.
47
- def deserialize(*args)
48
- from_xml(*args)
49
- end
50
- end
51
-
52
-
53
- require 'representable/xml/hash'
54
- module HyperlinkRepresenter
55
- include Representable::XML::AttributeHash
56
-
57
- self.representation_wrap = :link
58
- end
59
- end
60
- end
61
- end