eipiai 0.3.0 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0629a9a7ce30f972af6469dccc21f41afb350bbd
4
- data.tar.gz: 9cedc4a8d4edebf85ca77daac499dc60ce07aed1
3
+ metadata.gz: 9ae6cef8a01a687e9de4ca1a6cf889b9ce3f476f
4
+ data.tar.gz: bd8cd1327cb8b859c8b5f09518ccf0700fcbb3c5
5
5
  SHA512:
6
- metadata.gz: e40edbacb00400a7feeda253f4d211a06246db177d306d6aea94919a8efee8d6188cfdba7a3767f687c2bf9cbd085c3400157886113767ddd0a981c39cf9b815
7
- data.tar.gz: f8e8583a01787c39a248025f435fa5fa9f93b112638624fcf3a446ce262a511ee4225af0f7ef4ad229d54c25582453f77748ce78f3e5847430de8f60b80b238a
6
+ metadata.gz: b5971360e6f2b6f1c220dab71e7ecb7cd647406477b25310fdffef98655db324450e930f4018596263fd72099423bf27d8733babf46e1392af8cd36d334ef59d
7
+ data.tar.gz: cbf9e28a0ce0e4df1eba0465ba4bf18a7ef6f6455a3fda7536c19d9074bb1267b4cd7c05d6e88078c4a7e7038032c71c17737df7c26df48411dd62048970e1fb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## [v0.3.0](https://github.com/blendle/eipiai/tree/v0.3.0) (2015-12-13)
4
+ [Full Changelog](https://github.com/blendle/eipiai/compare/v0.2.0...v0.3.0)
5
+
6
+ **Merged pull requests:**
7
+
8
+ - initial Roar representers abstraction [\#4](https://github.com/blendle/eipiai/pull/4) ([JeanMertz](https://github.com/JeanMertz))
9
+
3
10
  ## [v0.2.0](https://github.com/blendle/eipiai/tree/v0.2.0) (2015-12-13)
4
11
  [Full Changelog](https://github.com/blendle/eipiai/compare/v0.1.0...v0.2.0)
5
12
 
@@ -27,3 +27,8 @@ end
27
27
  def app
28
28
  WebmachineApp.adapter
29
29
  end
30
+
31
+ Eipiai.configure do |config|
32
+ config.base_uri = 'https://example.org'
33
+ config.curie_uris = { b: 'https://api.example.org/rel/{rel}' }
34
+ end
@@ -118,6 +118,13 @@ Feature: Webmachine CRUD operations
118
118
  {
119
119
  "href": "https://example.org/item/hello"
120
120
  }
121
+ ],
122
+ "curies": [
123
+ {
124
+ "name": "b",
125
+ "href": "https://api.example.org/rel/{rel}",
126
+ "templated": true
127
+ }
121
128
  ]
122
129
  }
123
130
  }
@@ -0,0 +1,59 @@
1
+ require 'roar/json'
2
+ require 'set'
3
+
4
+ module Roar
5
+ module JSON
6
+ module HAL
7
+ # @see http://git.io/v085U
8
+ module Resources
9
+ # ToHashExt
10
+ #
11
+ # Adds a superset of functions on top of the default
12
+ # `Roar::JSON::HAL::Resources#to_hash`.
13
+ #
14
+ module ToHashExt
15
+ # to_hash
16
+ #
17
+ # taps into the default `Roar::JSON::HAL::Resources#to_hash` and adds
18
+ # `curies` links to the hash, if a curied object is present.
19
+ #
20
+ # The configuration value `curie_uris` needs to be defined, and be a
21
+ # hash of `{ curie => url }`. If your links contains a key `b:items`,
22
+ # then the curie link will be taken from the `curie_uris` hash, with
23
+ # key matching `b`:
24
+ #
25
+ # Eipiai.configuration.curie_uris = { b: 'https://example.com/' }
26
+ #
27
+ # `_links` key `b:items` will result in a curie pointing to the above
28
+ # address.
29
+ #
30
+ # @example
31
+ # post('/items', '{ "uid": "hello" }', 'Content-Type': 'application/json')
32
+ # items = Struct.new(:dataset).new(Item.dataset)
33
+ # Eipiai.configuration.curie_uris = { b: 'https://example.com/{rel}' }
34
+ #
35
+ # ItemsRepresenter.new(items).to_hash['_links']['curies']
36
+ # # => [{ name: "b", href: "https://example.com/{rel}", templated: true }]
37
+ #
38
+ # @return [Hash] hash representation of the represented object
39
+ #
40
+ def to_hash(*)
41
+ super.tap do |hash|
42
+ curies = Set.new
43
+ hash.fetch('_links', {}).each do |name, _|
44
+ next unless (curie = name[/(\S+):/, 1])
45
+ next unless (curie_href = Eipiai.configuration.curie_uris[curie.to_sym])
46
+
47
+ curies.add(name: curie, href: curie_href, templated: true)
48
+ end
49
+
50
+ hash['_links']['curies'] = curies.to_a if curies.any?
51
+ end
52
+ end
53
+ end
54
+
55
+ prepend ToHashExt
56
+ end
57
+ end
58
+ end
59
+ end
@@ -23,8 +23,9 @@ module Eipiai
23
23
  end
24
24
 
25
25
  def self.add_route(route, options)
26
- link(options) do |request|
27
- path = '/' + route.path_spec.map { |e| e.is_a?(Symbol) ? "{#{e}}" : e }.join('/')
26
+ link(options) do |context|
27
+ request = context[:request]
28
+ path = '/' + route.path_spec.map { |e| e.is_a?(Symbol) ? "{#{e}}" : e }.join('/')
28
29
 
29
30
  request.present? ? Addressable::URI.parse(request.uri).merge(path: path).to_s : path
30
31
  end
@@ -39,6 +39,35 @@ module Eipiai
39
39
  File.join(*parts)
40
40
  end
41
41
 
42
+ # url
43
+ #
44
+ # The url of the object.
45
+ #
46
+ # Based on the input, the result can vary:
47
+ #
48
+ # @example as a relative path
49
+ # user = User.new(uid: 'bartsimpson')
50
+ # UserRepresenter.new(user).url # => '/user/bartsimpson'
51
+ #
52
+ # @example with url given as argument
53
+ # user = User.new(uid: 'bartsimpson')
54
+ # UserRepresenter.new(user).url('https://example.org')
55
+ # # => 'https://example.org/user/bartsimpson'
56
+ #
57
+ # @example with url as global configuration
58
+ # Eipiai.configuration.base_uri = 'https://base.org'
59
+ # user = User.new(uid: 'bartsimpson')
60
+ # UserRepresenter.new(user).url # => 'https://base.org/user/bartsimpson'
61
+ #
62
+ # @param [String] uri string to prepend to the object path
63
+ # @return [String] uri with path of object appended
64
+ #
65
+ def url(uri = nil)
66
+ uri ||= Eipiai.configuration.base_uri
67
+
68
+ File.join uri.to_s, path
69
+ end
70
+
42
71
  private
43
72
 
44
73
  # part_path
@@ -79,7 +108,8 @@ module Eipiai
79
108
  module ClassMethods
80
109
  include Roar::JSON::HAL
81
110
 
82
- link(:self) do |request|
111
+ link(:self) do |context|
112
+ request = context[:request]
83
113
  request.present? ? Addressable::URI.parse(request.uri).merge(path: path).to_s : path
84
114
  end
85
115
  end
data/lib/eipiai/roar.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'roar'
2
2
 
3
+ require 'eipiai/roar/ext/hal'
4
+
3
5
  require 'eipiai/roar/representers/api'
4
6
  require 'eipiai/roar/representers/base'
@@ -3,5 +3,5 @@
3
3
  # The current version of the Eipiai library.
4
4
  #
5
5
  module Eipiai
6
- VERSION = '0.3.0'
6
+ VERSION = '0.4.0'
7
7
  end
@@ -101,14 +101,18 @@ module Eipiai
101
101
 
102
102
  # params
103
103
  #
104
- # Returns a Hash object, representing the JSOM payload sent in the request
105
- # object. Returns an empty hash if request body contains invalid JSON.
104
+ # Given a string in JSON format, returns the hash representation of that
105
+ # object.
106
+ #
107
+ # If the input is invalid JSON, an empty hash is returned.
108
+ #
109
+ # If no argument is given, `request.body` is used as the JSON input.
106
110
  #
107
111
  # @example Parse valid JSON request
108
- # resource.send :params, ('{ "hello": "world" }') # => { 'hello' => 'world' }
112
+ # resource.params('{ "hello": "world" }') # => { 'hello' => 'world' }
109
113
  #
110
114
  # @example Parse invalid JSON request
111
- # resource.send :params, 'invalid' #=> {}
115
+ # resource.params('invalid') #=> {}
112
116
  #
113
117
  # @param [String] body JSON provided as a string
114
118
  # @return [Hash] JSON string, converted to a hash
@@ -124,7 +128,7 @@ module Eipiai
124
128
  # Given an object, calls `#to_hash` on that object,
125
129
  #
126
130
  # If the object's `to_hash` implementation accepts any arguments, the
127
- # `request` object is sent as its first argument.
131
+ # hash `{ request: request }` is sent as its first argument.
128
132
  #
129
133
  # In practice, this method is used without any parameters, causing the
130
134
  # method to call `represented`, which represents a Roar representer. This in
@@ -139,7 +143,7 @@ module Eipiai
139
143
  # @return [Hash] hash representation of the object
140
144
  #
141
145
  def to_hash(o = represented)
142
- o.method(:to_hash).arity.zero? ? o.to_hash : o.to_hash(request)
146
+ o.method(:to_hash).arity.zero? ? o.to_hash : o.to_hash(request: request)
143
147
  end
144
148
 
145
149
  def to_json
data/lib/eipiai.rb CHANGED
@@ -1,3 +1,41 @@
1
1
  require 'eipiai/roar'
2
2
  require 'eipiai/validation'
3
3
  require 'eipiai/webmachine'
4
+
5
+ # Eipiai
6
+ #
7
+ # The main module for the library.
8
+ #
9
+ module Eipiai
10
+ class << self
11
+ attr_accessor :configuration
12
+
13
+ # configure
14
+ #
15
+ # Configure the library using this `configure` method, as follows:
16
+ #
17
+ # Eipiai.configure do |config|
18
+ # config.curie_uris = { b: 'https://api.blendle.com/rel/{rel}' }
19
+ # end
20
+ #
21
+ # @return [void]
22
+ #
23
+ def configure
24
+ yield(configuration)
25
+ end
26
+ end
27
+
28
+ # Configuration
29
+ #
30
+ # The configuration store for the Eipiai library.
31
+ #
32
+ class Configuration
33
+ attr_accessor :base_uri, :curie_uris
34
+
35
+ def initialize
36
+ @curie_uris = {}
37
+ end
38
+ end
39
+
40
+ @configuration = Configuration.new
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eipiai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Mertz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-13 00:00:00.000000000 Z
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -243,6 +243,7 @@ files:
243
243
  - features/webmachine.feature
244
244
  - lib/eipiai.rb
245
245
  - lib/eipiai/roar.rb
246
+ - lib/eipiai/roar/ext/hal.rb
246
247
  - lib/eipiai/roar/representers/api.rb
247
248
  - lib/eipiai/roar/representers/base.rb
248
249
  - lib/eipiai/validation.rb