braque 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df25bde58e0019d26eee97463a92e1b2cb4603db
|
4
|
+
data.tar.gz: 68d1599459c8aaa9dd3028b83fb8ba86b027a710
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c623a2dcf30dcb360c19de218020682932b07e4c269d9201acd863349ba7c3077bd1bc30dd44ed8e5ef7a5e95bce4456d980a809206ee7df9bee5b80d4757b8b
|
7
|
+
data.tar.gz: eed91132bcf818cca39b339cc673acc9393f2161df657d0614cd933a5fc67f186375b8e5892bb67e1cd7f887aa0357a26cacb7d4e27480ff4b22f328c08b5bf7
|
data/README.md
CHANGED
@@ -4,6 +4,8 @@ Braque aims to provide a simple and familiar interface for setting up clients to
|
|
4
4
|
|
5
5
|
Braque is an early-stage and exploratory project.
|
6
6
|
|
7
|
+
[ ![Codeship Status for dylanfareed/braque](https://codeship.com/projects/c41fd480-9f37-0132-ec49-02db7f3f2d41/status?branch=master)](https://codeship.com/projects/65077)
|
8
|
+
|
7
9
|
### Model setup
|
8
10
|
|
9
11
|
```Braque::Model``` is ActiveSupport concern. You can use Braque::Model to map a remote resource to a class in your application. Do so by including Braque::Model in the class, defining the API service's root url and authentication details, and listing attributes which we expect from the API.
|
@@ -27,7 +29,7 @@ In a Rails app, you can then use this model simply:
|
|
27
29
|
|
28
30
|
```ruby
|
29
31
|
class ArticlesController < ApplicationController
|
30
|
-
before_filter :find_article, except: :index
|
32
|
+
before_filter :find_article, except: [:index, :new]
|
31
33
|
|
32
34
|
def index
|
33
35
|
@articles = Article.list(page: params[:page], size: params[:size])
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Braque
|
2
|
+
module Helpers
|
3
|
+
module HypermediaResponsesHelper
|
4
|
+
def hypermedia_root_json_for(hyperclient_model_class)
|
5
|
+
root_hash = {}
|
6
|
+
root_hash[:_links] = {}
|
7
|
+
root_hash[:_links][:self] = { href: hyperclient_model_class.api_root }
|
8
|
+
root_hash[:_links][hyperclient_model_class.collection_method_name] = { href: "#{hyperclient_model_class.api_root}/#{hyperclient_model_class.collection_method_name}{?page,size}", templated: true }
|
9
|
+
root_hash[:_links][hyperclient_model_class.instance_method_name] = { href: "#{hyperclient_model_class.api_root}/#{hyperclient_model_class.collection_method_name}/{id}", templated: true }
|
10
|
+
JSON.parse root_hash.to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
def hypermedia_collection_json_for(hyperclient_model_collection)
|
14
|
+
hyperclient_model = hyperclient_model_collection.first
|
15
|
+
collection_hash = {}
|
16
|
+
collection_hash[:total_count] = 1
|
17
|
+
collection_hash[:total_pages] = 1
|
18
|
+
collection_hash[:current_page] = 1
|
19
|
+
collection_hash[:_links] = {}
|
20
|
+
collection_hash[:_links][:self] = { href: "#{hyperclient_model_collection.first.class.api_root}/#{hyperclient_model_collection.first.class.collection_method_name}" }
|
21
|
+
collection_hash[:_links][:next] = { href: "#{hyperclient_model_collection.first.class.api_root}/#{hyperclient_model_collection.first.class.collection_method_name}" }
|
22
|
+
collection_hash[:_links][:prev] = { href: "#{hyperclient_model_collection.first.class.api_root}/#{hyperclient_model_collection.first.class.collection_method_name}" }
|
23
|
+
collection_hash[:_embedded] = {}
|
24
|
+
collection_hash[:_embedded][hyperclient_model_collection.first.class.collection_method_name] = embedded_elements_for_collection(hyperclient_model_collection)
|
25
|
+
JSON.parse collection_hash.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
def hypermedia_resource_json_for(hyperclient_model)
|
29
|
+
resource_hash = embedded_element(hyperclient_model)
|
30
|
+
JSON.parse resource_hash.to_json
|
31
|
+
end
|
32
|
+
|
33
|
+
def embedded_elements_for_collection(hyperclient_model_collection)
|
34
|
+
embedded_elements = []
|
35
|
+
hyperclient_model_collection.each do |model|
|
36
|
+
embedded_elements << embedded_element(model)
|
37
|
+
end
|
38
|
+
embedded_elements
|
39
|
+
end
|
40
|
+
|
41
|
+
def embedded_element(model)
|
42
|
+
hyperclient_element = {}
|
43
|
+
model.attributes.keys.each do |key|
|
44
|
+
hyperclient_element[key] = model.send(key)
|
45
|
+
end
|
46
|
+
hyperclient_element[:_links] = {}
|
47
|
+
hyperclient_element[:_links][:self] = { href: "#{model.class.api_root}/#{model.class.collection_method_name}/#{model.id}" }
|
48
|
+
hyperclient_element
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/braque/version.rb
CHANGED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'braque/helpers'
|
3
|
+
|
4
|
+
RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
5
|
+
include Braque::Helpers::HypermediaResponsesHelper
|
6
|
+
|
7
|
+
context 'with a Braque::Model class' do
|
8
|
+
before(:all) do
|
9
|
+
class Article
|
10
|
+
include ::Braque::Model
|
11
|
+
self.api_root = 'http://localhost:9292'
|
12
|
+
self.api_token = 'replace-me'
|
13
|
+
attribute :id
|
14
|
+
attribute :title
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'hypermedia_root_json_for' do
|
19
|
+
before(:each) do
|
20
|
+
@root_json = hypermedia_root_json_for Article
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns the self link' do
|
24
|
+
expect(@root_json['_links']['self']['href']).to eq Article.api_root
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'returns the collection link' do
|
28
|
+
expect(@root_json['_links']['articles']['href']).to eq "#{Article.api_root}/articles{?page,size}"
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns the resource link' do
|
32
|
+
expect(@root_json['_links']['article']['href']).to eq "#{Article.api_root}/articles/{id}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'hypermedia_collection_json_for' do
|
37
|
+
before(:each) do
|
38
|
+
@article = Article.new(id: 1, title: 'Sculpture in the Expanded Field')
|
39
|
+
@collection_json = hypermedia_collection_json_for [@article]
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns the _links node' do
|
43
|
+
expect(@collection_json['_links']['self']['href']).to eq "#{Article.api_root}/articles"
|
44
|
+
expect(@collection_json['_links']['next']['href']).to eq "#{Article.api_root}/articles"
|
45
|
+
expect(@collection_json['_links']['prev']['href']).to eq "#{Article.api_root}/articles"
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'returns the _embedded node' do
|
49
|
+
expect(@collection_json['_embedded']['articles']).to eq [{ 'id' => @article.id, 'title' => @article.title, '_links' => { 'self' => { 'href' => 'http://localhost:9292/articles/1' } } }]
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns the current_page' do
|
53
|
+
expect(@collection_json['current_page']).to eq 1
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns the total_count' do
|
57
|
+
expect(@collection_json['total_count']).to eq 1
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'returns the total_pages' do
|
61
|
+
expect(@collection_json['total_pages']).to eq 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'hypermedia_resource_json_for' do
|
66
|
+
before(:each) do
|
67
|
+
@article = Article.new(id: 1, title: 'Sculpture in the Expanded Field')
|
68
|
+
@resource_json = hypermedia_resource_json_for @article
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'returns the _links node' do
|
72
|
+
expect(@resource_json['_links']['self']['href']).to eq "#{Article.api_root}/articles/1"
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns the resource attributes' do
|
76
|
+
expect(@resource_json['id']).to eq @article.id
|
77
|
+
expect(@resource_json['title']).to eq @article.title
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: braque
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dylan Fareed
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hyperclient
|
@@ -64,9 +64,12 @@ files:
|
|
64
64
|
- README.md
|
65
65
|
- lib/braque.rb
|
66
66
|
- lib/braque/collection.rb
|
67
|
+
- lib/braque/helpers.rb
|
68
|
+
- lib/braque/helpers/hypermedia_responses_helper.rb
|
67
69
|
- lib/braque/model.rb
|
68
70
|
- lib/braque/version.rb
|
69
71
|
- spec/braque/collection_spec.rb
|
72
|
+
- spec/braque/helpers/hypermedia_responses_helper_spec.rb
|
70
73
|
- spec/braque/model_spec.rb
|
71
74
|
- spec/fixtures/collection.json
|
72
75
|
- spec/fixtures/resource.json
|
@@ -92,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
95
|
version: '0'
|
93
96
|
requirements: []
|
94
97
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.2.2
|
96
99
|
signing_key:
|
97
100
|
specification_version: 4
|
98
101
|
summary: Braque provides a simple interface for interacting with Hypermedia API services
|