jsonapi-consumer 0.1.0.pre.3 → 0.1.0

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: 1c1023158bad9c3f878f6c70c43de0030f33548f
4
- data.tar.gz: e00a070c00e6dc4f013b7acb976fa38a3dab2b4d
3
+ metadata.gz: 2ebd6ed496bf4e33e58103e89a8ea1f8ec65e49c
4
+ data.tar.gz: 4de1a846349a7e3606f45573fa70dd8c2b60bbdd
5
5
  SHA512:
6
- metadata.gz: 576270b931a52c0b2813bd09a86a0352cd4b1ca9961ec4ed1cd923fc8f6a09076d03acd6879758c9c7c56f71851fba863cf53effd68b22c9a160adceafad2302
7
- data.tar.gz: b8f8db8bb78d3dfd10c7b56206cf2419962a36a3c15c7f0f9ff0f9bb246bab4ecc8f9961a0c308de4ebb23f89669409c1c0b363999a8a0c04f063470b6bd9fbf
6
+ metadata.gz: f90b5cf975914fdc9dced9176b48e8705f98c3ee8b423c653629aa76513e11b3615ed7e771f4bd54c58416198ad5073198b955a9bd8e379672c5fde8b5fa9ec6
7
+ data.tar.gz: 25da9ee3afd9b759a001834fb4989aeb73c1e6a8c0cfc59cecd51bd09fa1a17ac33350618db264089108a9fa7cacd68aef81f2a6f5c79fa0e602aab0cd5bb32b
data/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ 0.1.0 / 2014-12-03
3
+ ==================
4
+
5
+ * `middleware` and `ssl` attributes are inheritable
6
+ * Support custom faraday connection middleware with `#middleware` block
7
+ * update README with usage
8
+
2
9
  0.1.0.pre.3 / 2014-11-11
3
10
  ==================
4
11
 
data/README.md CHANGED
@@ -22,7 +22,67 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ It's suggested to create a base resource for the whole API that you can re-use.
26
+
27
+ ```ruby
28
+ class Base
29
+ include JSONAPI::Consumer::Resource
30
+
31
+ self.host = 'http://localhost:3000/api/'
32
+ end
33
+ ```
34
+
35
+ Then inherit from that Base class for each resource defined in your API.
36
+
37
+ ```ruby
38
+ module Blog
39
+
40
+ class Author < Base
41
+ has_many :posts, class_name: 'Blog::Post'
42
+ end
43
+
44
+ class Post < Base
45
+ has_one :user, class_name: 'Blog::User'
46
+ has_many :comments, class_name: 'Blog::Comment'
47
+ end
48
+
49
+ class User < Base
50
+
51
+ end
52
+
53
+ class Comment < Base
54
+
55
+ end
56
+
57
+ end
58
+ ```
59
+
60
+ #### Additional Features
61
+
62
+ ##### Dynamic Objects
63
+
64
+ By default calling `.new` or `.build` on a resource will give you an empty
65
+ object with no attributes defined. This is less than ideal when building forms
66
+ with something like Rails' FormBuilder.
67
+
68
+ We suggest setting up your model to do a `GET /{resource_name}/new` if your
69
+ server supports it and using `.build` instead of `.new`. This will populate the
70
+ object with defaults set by the server response.
71
+
72
+ ```ruby
73
+ class User
74
+ include JSONAPI::Consumer::Resource
75
+
76
+ self.request_new_object_on_build = true
77
+
78
+ # .build will now call GET /users/new
79
+ end
80
+ ```
81
+
82
+ #### Testing
83
+
84
+ We suggest [Webmock](https://github.com/bblimke/webmock) at this stage in
85
+ development. We plan to add test helpers before the first major release.
26
86
 
27
87
  ## Contributing
28
88
 
@@ -6,7 +6,7 @@ module JSONAPI::Consumer
6
6
  extend ActiveModel::Naming
7
7
 
8
8
  attr_reader :errors
9
- class_attribute :host
9
+ class_attribute :host, :connection, :ssl
10
10
  end
11
11
 
12
12
  include ObjectBuildConcern
@@ -17,6 +17,10 @@ module JSONAPI::Consumer
17
17
  include ConnectionConcern
18
18
 
19
19
  module ClassMethods
20
+ def middleware(&block)
21
+ _connection(true, &block)
22
+ end
23
+
20
24
  def json_key
21
25
  self.name.demodulize.pluralize.underscore
22
26
  end
@@ -23,18 +23,22 @@ module JSONAPI::Consumer
23
23
  end
24
24
 
25
25
  # :nodoc:
26
- def _connection
27
- @connection ||= begin
26
+ def _connection(reload=false)
27
+ self.connection = nil if !!reload
28
+ self.connection ||= begin
28
29
  Faraday.new(url: self.host, ssl: self.ssl) do |conn|
29
30
  conn.request :json
30
31
  conn.request :request_headers, accept: "application/json"
31
32
 
33
+ yield(conn) if block_given?
34
+
32
35
  conn.use Middleware::RequestTimeout
33
36
  conn.use Middleware::ParseJson
34
37
 
35
38
  conn.use Middleware::RaiseError
36
39
  conn.adapter Faraday.default_adapter
37
40
  end
41
+
38
42
  end
39
43
  end
40
44
  end
@@ -1,5 +1,5 @@
1
1
  module Jsonapi
2
2
  module Consumer
3
- VERSION = "0.1.0.pre.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -35,3 +35,11 @@ module Blog
35
35
  end
36
36
  end
37
37
  # END - Blog example
38
+
39
+
40
+ class NotCalled < Faraday::Response::Middleware
41
+ class ::NotCalledError < StandardError; end
42
+ def parse(body)
43
+ raise NotCalledError, "this should not be called"
44
+ end
45
+ end
@@ -10,6 +10,27 @@ RSpec.describe 'Connection' do
10
10
 
11
11
  let(:obj) { test_class.new(name: 'jsonapi.example') }
12
12
 
13
+ describe 'custom connection middleware' do
14
+
15
+ it 'handles custom middleware' do
16
+ stub_request(:get, "http://localhost:3000/api/basic_resources")
17
+ .with(headers: {accept: 'application/json'})
18
+ .to_return(headers: {content_type: "application/json"}, body: {
19
+ basic_resources: [
20
+ {id: '1'}
21
+ ]
22
+ }.to_json)
23
+
24
+ expect { BasicResource.all }.to_not raise_error
25
+
26
+ BasicResource.middleware do |conn|
27
+ conn.use NotCalled
28
+ end
29
+
30
+ expect { BasicResource.all }.to raise_error(NotCalledError)
31
+ end
32
+ end
33
+
13
34
  describe '.all' do
14
35
  it 'returns all results as objects' do
15
36
  stub_request(:get, "http://localhost:3000/api/records")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Smestad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-11 00:00:00.000000000 Z
11
+ date: 2014-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -217,9 +217,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
217
217
  version: '0'
218
218
  required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  requirements:
220
- - - ">"
220
+ - - ">="
221
221
  - !ruby/object:Gem::Version
222
- version: 1.3.1
222
+ version: '0'
223
223
  requirements: []
224
224
  rubyforge_project:
225
225
  rubygems_version: 2.2.2