braque 0.2.1 → 0.2.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: d7a9f3bb1b0fa79bccb708a0bf57e0b6a52f9e2b
4
- data.tar.gz: 1e41b55a3b55791b8e5b66aacdffadddee401427
3
+ metadata.gz: 70e62e768e37c2ede2e426564c4751b5f35fb672
4
+ data.tar.gz: cec0db7ad8f7ff1e3da6f2be88827898ba20009e
5
5
  SHA512:
6
- metadata.gz: 69f0e4507806a662441bbde237a173b17bd5d4067ef31d11208aa40f77efb437e0578e6f48d7c22af29849dac032760da27bafc45b50d5b97747268a067e9336
7
- data.tar.gz: b0c7e6a4e5a7dd55ce7d39f48227edf1efd1280ff7e0579ea47230cabd099a4288f3b06adbe9e356428ba70b49ef9feb8fd5b8efa20e1a851449a985555f388b
6
+ metadata.gz: 1718329d5f9e8545eadeae1edc06d432950f216e7eb36e8fb35890c9354ae9e64ee1c53856b513dabd18d5bc88f638c70029a694299fd8468538a8bb306f63fc
7
+ data.tar.gz: ae99d0fcbd624392b30b8f0b441993631747ea2950172e4f37deba0c2563e98510096321f20b100157d6194baa8f5a4e8703df84b90f6d76bc8350cd0fadf60c
data/README.md CHANGED
@@ -26,6 +26,51 @@ end
26
26
 
27
27
  Braque::Model adds familiar "Active Record"-like `create`, `find`, and `list` class methods to the embedding class (`Article` in the example above) as well as `save` and `destroy` instance methods. These methods wrap Hyperclient to make calls to a remote hypermedia API.
28
28
 
29
+ ### Usage
30
+
31
+ In a Rails app, once you've set up your model, you can use the following familiar syntax to query the remote API:
32
+
33
+ ```ruby
34
+ class ArticlesController < ApplicationController
35
+ before_filter :find_article, except: [:index, :new]
36
+
37
+ def index
38
+ @articles = Article.list(page: params[:page], size: params[:size])
39
+ end
40
+
41
+ def new
42
+ end
43
+
44
+ def create
45
+ @article = Article.create params[:article]
46
+ redirect_to article_path(@article)
47
+ end
48
+
49
+ def show
50
+ end
51
+
52
+ def edit
53
+ end
54
+
55
+ def update
56
+ @article = @article.save params[:article]
57
+ redirect_to article_path(@article.id)
58
+ end
59
+
60
+ def destroy
61
+ @article.destroy
62
+ redirect_to articles_path
63
+ end
64
+
65
+ private
66
+
67
+ def find_article
68
+ @article = Article.find(id: params[:id])
69
+ end
70
+ end
71
+
72
+ ```
73
+
29
74
  ### Relations
30
75
 
31
76
  If your remote API includes associated resources in the `_links` node for a given resource, you can use Braque's relations helpers to make navigating to those associated resources somewhat simpler.
@@ -114,51 +159,6 @@ This method supports passing params to the remote API as well.
114
159
  articles = magazine.articles(page: 2, size: 20)
115
160
  ```
116
161
 
117
- ### Controllers
118
-
119
- In a Rails app, once you've set up your model, you can use the following familiar syntax to query the remote API:
120
-
121
- ```ruby
122
- class ArticlesController < ApplicationController
123
- before_filter :find_article, except: [:index, :new]
124
-
125
- def index
126
- @articles = Article.list(page: params[:page], size: params[:size])
127
- end
128
-
129
- def new
130
- end
131
-
132
- def create
133
- @article = Article.create params[:article]
134
- redirect_to article_path(@article)
135
- end
136
-
137
- def show
138
- end
139
-
140
- def edit
141
- end
142
-
143
- def update
144
- @article = @article.save params[:article]
145
- redirect_to article_path(@article.id)
146
- end
147
-
148
- def destroy
149
- @article.destroy
150
- redirect_to articles_path
151
- end
152
-
153
- private
154
-
155
- def find_article
156
- @article = Article.find(id: params[:id])
157
- end
158
- end
159
-
160
- ```
161
-
162
162
  ### Subclassing
163
163
 
164
164
  Braque supports inheritance for shared setup across multiple models in your app that make calls to the same remote API.
data/lib/braque/model.rb CHANGED
@@ -74,7 +74,13 @@ module Braque
74
74
  include Braque::Collection
75
75
  include Braque::Relations
76
76
 
77
- [:api_root_url, :accept_header, :authorization_header, :http_authorization_header].each do |config_option|
77
+ [
78
+ :api_root_url,
79
+ :accept_header,
80
+ :authorization_header,
81
+ :http_authorization_header,
82
+ :remote_resource_name
83
+ ].each do |config_option|
78
84
  define_method config_option do |value|
79
85
  config[config_option] = value
80
86
  end
@@ -102,7 +108,7 @@ module Braque
102
108
  end
103
109
 
104
110
  def collection_method_name
105
- name.tableize
111
+ (config[:remote_resource_name].present? ? config[:remote_resource_name] : name).tableize
106
112
  end
107
113
 
108
114
  def instance_method_name
@@ -1,3 +1,3 @@
1
1
  module Braque
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Braque::Model, type: :model do
4
+ context 'with a custom defined resource name' do
5
+ before do
6
+ class OceanBreeze
7
+ include ::Braque::Model
8
+ api_root_url 'http://localhost:9292'
9
+ remote_resource_name 'Breeze'
10
+ attribute :id
11
+ attribute :title
12
+ end
13
+ end
14
+
15
+ let(:root_response) { JSON.parse(File.read 'spec/fixtures/root.json') }
16
+ let(:breeze_response) { JSON.parse(File.read 'spec/fixtures/resource.json') }
17
+
18
+ let(:root_request) do
19
+ WebMock.stub_request(:get, "#{OceanBreeze.config[:api_root_url]}/")
20
+ .to_return(status: 200, body: root_response)
21
+ end
22
+
23
+ let(:breeze_request) do
24
+ WebMock.stub_request(:get, "#{OceanBreeze.config[:api_root_url]}/breezes/1")
25
+ .to_return(status: 200, body: breeze_response)
26
+ end
27
+
28
+ let(:ocean_breeze) { OceanBreeze.new id: 1 }
29
+
30
+ before do
31
+ root_request
32
+ breeze_request
33
+ @ocean_breeze = OceanBreeze.find(id: 1)
34
+ end
35
+
36
+ it 'performs the API root request' do
37
+ expect(root_request).to have_been_requested
38
+ end
39
+
40
+ it 'performs the collection request' do
41
+ expect(breeze_request).to have_been_requested
42
+ end
43
+ end
44
+ 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.2.1
4
+ version: 0.2.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-09-14 00:00:00.000000000 Z
11
+ date: 2015-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hyperclient
@@ -70,6 +70,7 @@ files:
70
70
  - spec/braque/api_root_spec.rb
71
71
  - spec/braque/client_headers_spec.rb
72
72
  - spec/braque/collection_spec.rb
73
+ - spec/braque/custom_name_spec.rb
73
74
  - spec/braque/model_attributes_spec.rb
74
75
  - spec/braque/model_spec.rb
75
76
  - spec/braque/multiple_model_spec.rb