her 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ language: ruby
3
3
  sudo: false
4
4
 
5
5
  rvm:
6
+ - 2.3.1
6
7
  - 2.2.2
7
8
  - 2.1.6
8
9
  - 2.0.0
@@ -18,9 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_development_dependency "rake", "~> 10.0"
21
- s.add_development_dependency "rspec", "~> 2.13"
22
- s.add_development_dependency "rspec-its", "~> 1.0"
23
- s.add_development_dependency "fivemat", "~> 1.2"
21
+ s.add_development_dependency "rspec", "~> 3.5"
24
22
  s.add_development_dependency "json", "~> 1.8"
25
23
 
26
24
  s.add_runtime_dependency "activemodel", ">= 3.0.0", "<= 6.0.0"
@@ -160,7 +160,10 @@ module Her
160
160
  # # Called via DELETE "/users/1"
161
161
  def destroy_existing(id, params={})
162
162
  request(params.merge(:_method => method_for(:destroy), :_path => build_request_path(params.merge(primary_key => id)))) do |parsed_data, response|
163
- new(parse(parsed_data[:data]).merge(:_destroyed => true))
163
+ data = parse(parsed_data[:data])
164
+ metadata = parsed_data[:metadata]
165
+ response_errors = parsed_data[:errors]
166
+ new(data.merge(:_destroyed => true, :metadata => metadata, :response_errors => response_errors))
164
167
  end
165
168
  end
166
169
 
@@ -1,3 +1,3 @@
1
1
  module Her
2
- VERSION = "0.8.2"
2
+ VERSION = "0.8.3"
3
3
  end
@@ -8,21 +8,24 @@ describe Her::API do
8
8
  describe "#setup" do
9
9
  context "when setting custom middleware" do
10
10
  before do
11
- class Foo; end;
12
- class Bar; end;
11
+ class Foo; end
12
+ class Bar; end
13
13
 
14
- subject.setup :url => "https://api.example.com" do |connection|
14
+ subject.setup url: "https://api.example.com" do |connection|
15
15
  connection.use Foo
16
16
  connection.use Bar
17
17
  end
18
18
  end
19
19
 
20
- specify { subject.connection.builder.handlers.should == [Foo, Bar] }
20
+ specify { expect(subject.connection.builder.handlers).to eq([Foo, Bar]) }
21
21
  end
22
22
 
23
23
  context "when setting custom options" do
24
- before { subject.setup :foo => { :bar => "baz" }, :url => "https://api.example.com" }
25
- its(:options) { should == { :foo => { :bar => "baz" }, :url => "https://api.example.com" } }
24
+ before { subject.setup foo: { bar: "baz" }, url: "https://api.example.com" }
25
+
26
+ describe "#options" do
27
+ it { expect(subject.options).to eq(foo: { bar: "baz" }, url: "https://api.example.com") }
28
+ end
26
29
  end
27
30
  end
28
31
 
@@ -30,83 +33,83 @@ describe Her::API do
30
33
  before do
31
34
  class SimpleParser < Faraday::Response::Middleware
32
35
  def on_complete(env)
33
- env[:body] = { :data => env[:body] }
36
+ env[:body] = { data: env[:body] }
34
37
  end
35
38
  end
36
39
  end
37
40
 
38
41
  context "making HTTP requests" do
39
- let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo")[:parsed_data] }
42
+ let(:parsed_data) { subject.request(_method: :get, _path: "/foo")[:parsed_data] }
40
43
  before do
41
- subject.setup :url => "https://api.example.com" do |builder|
44
+ subject.setup url: "https://api.example.com" do |builder|
42
45
  builder.use SimpleParser
43
- builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is."] } }
46
+ builder.adapter(:test) { |stub| stub.get("/foo") { [200, {}, "Foo, it is."] } }
44
47
  end
45
48
  end
46
49
 
47
- specify { parsed_data[:data].should == "Foo, it is." }
50
+ specify { expect(parsed_data[:data]).to eq("Foo, it is.") }
48
51
  end
49
52
 
50
53
  context "making HTTP requests while specifying custom HTTP headers" do
51
- let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 })[:parsed_data] }
54
+ let(:parsed_data) { subject.request(_method: :get, _path: "/foo", _headers: { "X-Page" => 2 })[:parsed_data] }
52
55
 
53
56
  before do
54
- subject.setup :url => "https://api.example.com" do |builder|
57
+ subject.setup url: "https://api.example.com" do |builder|
55
58
  builder.use SimpleParser
56
- builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is page #{env[:request_headers]["X-Page"]}."] } }
59
+ builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is page #{env[:request_headers]['X-Page']}."] } }
57
60
  end
58
61
  end
59
62
 
60
- specify { parsed_data[:data].should == "Foo, it is page 2." }
63
+ specify { expect(parsed_data[:data]).to eq("Foo, it is page 2.") }
61
64
  end
62
65
 
63
66
  context "parsing a request with the default parser" do
64
- let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1")[:parsed_data] }
67
+ let(:parsed_data) { subject.request(_method: :get, _path: "users/1")[:parsed_data] }
65
68
  before do
66
- subject.setup :url => "https://api.example.com" do |builder|
69
+ subject.setup url: "https://api.example.com" do |builder|
67
70
  builder.use Her::Middleware::FirstLevelParseJSON
68
71
  builder.adapter :test do |stub|
69
- stub.get("/users/1") { |env| [200, {}, MultiJson.dump({ :id => 1, :name => "George Michael Bluth", :errors => ["This is a single error"], :metadata => { :page => 1, :per_page => 10 } })] }
72
+ stub.get("/users/1") { [200, {}, MultiJson.dump(id: 1, name: "George Michael Bluth", errors: ["This is a single error"], metadata: { page: 1, per_page: 10 })] }
70
73
  end
71
74
  end
72
75
  end
73
76
 
74
77
  specify do
75
- parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
76
- parsed_data[:errors].should == ["This is a single error"]
77
- parsed_data[:metadata].should == { :page => 1, :per_page => 10 }
78
+ expect(parsed_data[:data]).to eq(id: 1, name: "George Michael Bluth")
79
+ expect(parsed_data[:errors]).to eq(["This is a single error"])
80
+ expect(parsed_data[:metadata]).to eq(page: 1, per_page: 10)
78
81
  end
79
82
  end
80
83
 
81
84
  context "parsing a request with a custom parser" do
82
- let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1")[:parsed_data] }
85
+ let(:parsed_data) { subject.request(_method: :get, _path: "users/1")[:parsed_data] }
83
86
  before do
84
87
  class CustomParser < Faraday::Response::Middleware
85
88
  def on_complete(env)
86
- json = MultiJson.load(env[:body], :symbolize_keys => true)
89
+ json = MultiJson.load(env[:body], symbolize_keys: true)
87
90
  errors = json.delete(:errors) || []
88
91
  metadata = json.delete(:metadata) || {}
89
92
  env[:body] = {
90
- :data => json,
91
- :errors => errors,
92
- :metadata => metadata,
93
+ data: json,
94
+ errors: errors,
95
+ metadata: metadata
93
96
  }
94
97
  end
95
98
  end
96
99
 
97
- subject.setup :url => "https://api.example.com" do |builder|
100
+ subject.setup url: "https://api.example.com" do |builder|
98
101
  builder.use CustomParser
99
102
  builder.use Faraday::Request::UrlEncoded
100
103
  builder.adapter :test do |stub|
101
- stub.get("/users/1") { |env| [200, {}, MultiJson.dump(:id => 1, :name => "George Michael Bluth")] }
104
+ stub.get("/users/1") { [200, {}, MultiJson.dump(id: 1, name: "George Michael Bluth")] }
102
105
  end
103
106
  end
104
107
  end
105
108
 
106
109
  specify do
107
- parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
108
- parsed_data[:errors].should == []
109
- parsed_data[:metadata].should == {}
110
+ expect(parsed_data[:data]).to eq(id: 1, name: "George Michael Bluth")
111
+ expect(parsed_data[:errors]).to eq([])
112
+ expect(parsed_data[:metadata]).to eq({})
110
113
  end
111
114
  end
112
115
  end
@@ -1,26 +1,41 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Her::Collection do
4
-
5
4
  let(:items) { [1, 2, 3, 4] }
6
- let(:metadata) { { :name => 'Testname' } }
7
- let(:errors) { { :name => ['not_present'] } }
5
+ let(:metadata) { { name: "Testname" } }
6
+ let(:errors) { { name: ["not_present"] } }
8
7
 
9
8
  describe "#new" do
10
9
  context "without parameters" do
11
10
  subject { Her::Collection.new }
12
11
 
13
- it { should eq([]) }
14
- its(:metadata) { should eq({}) }
15
- its(:errors) { should eq({}) }
12
+ it { is_expected.to eq([]) }
13
+
14
+ describe "#metadata" do
15
+ subject { super().metadata }
16
+ it { is_expected.to eq({}) }
17
+ end
18
+
19
+ describe "#errors" do
20
+ subject { super().errors }
21
+ it { is_expected.to eq({}) }
22
+ end
16
23
  end
17
24
 
18
25
  context "with parameters" do
19
26
  subject { Her::Collection.new(items, metadata, errors) }
20
27
 
21
- it { should eq([1,2,3,4]) }
22
- its(:metadata) { should eq({ :name => 'Testname' }) }
23
- its(:errors) { should eq({ :name => ['not_present'] }) }
28
+ it { is_expected.to eq([1, 2, 3, 4]) }
29
+
30
+ describe "#metadata" do
31
+ subject { super().metadata }
32
+ it { is_expected.to eq(name: "Testname") }
33
+ end
34
+
35
+ describe "#errors" do
36
+ subject { super().errors }
37
+ it { is_expected.to eq(name: ["not_present"]) }
38
+ end
24
39
  end
25
40
  end
26
41
  end
@@ -1,163 +1,166 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Her::JsonApi::Model do
4
4
  before do
5
- Her::API.setup :url => "https://api.example.com" do |connection|
5
+ Her::API.setup url: "https://api.example.com" do |connection|
6
6
  connection.use Her::Middleware::JsonApiParser
7
7
  connection.adapter :test do |stub|
8
- stub.get("/users/1") do |env|
9
- [
8
+ stub.get("/users/1") do
9
+ [
10
10
  200,
11
11
  {},
12
12
  {
13
13
  data: {
14
14
  id: 1,
15
- type: 'users',
15
+ type: "users",
16
16
  attributes: {
17
- name: "Roger Federer",
18
- },
17
+ name: "Roger Federer"
18
+ }
19
19
  }
20
-
20
+
21
21
  }.to_json
22
- ]
22
+ ]
23
23
  end
24
24
 
25
- stub.get("/users") do |env|
26
- [
25
+ stub.get("/users") do
26
+ [
27
27
  200,
28
28
  {},
29
29
  {
30
30
  data: [
31
31
  {
32
32
  id: 1,
33
- type: 'users',
33
+ type: "users",
34
34
  attributes: {
35
- name: "Roger Federer",
36
- },
37
- },
35
+ name: "Roger Federer"
36
+ }
37
+ },
38
38
  {
39
39
  id: 2,
40
- type: 'users',
40
+ type: "users",
41
41
  attributes: {
42
- name: "Kei Nishikori",
43
- },
42
+ name: "Kei Nishikori"
43
+ }
44
44
  }
45
45
  ]
46
46
  }.to_json
47
- ]
47
+ ]
48
48
  end
49
49
 
50
- stub.post("/users", data: {
51
- type: 'users',
50
+ stub.post("/users", data:
51
+ {
52
+ type: "users",
52
53
  attributes: {
53
- name: "Jeremy Lin",
54
- },
55
- }) do |env|
56
- [
54
+ name: "Jeremy Lin"
55
+ }
56
+ }) do
57
+ [
57
58
  201,
58
59
  {},
59
60
  {
60
61
  data: {
61
62
  id: 3,
62
- type: 'users',
63
+ type: "users",
63
64
  attributes: {
64
- name: 'Jeremy Lin',
65
- },
65
+ name: "Jeremy Lin"
66
+ }
66
67
  }
67
-
68
+
68
69
  }.to_json
69
- ]
70
+ ]
70
71
  end
71
72
 
72
- stub.patch("/users/1", data: {
73
- type: 'users',
73
+ stub.patch("/users/1", data:
74
+ {
75
+ type: "users",
74
76
  id: 1,
75
77
  attributes: {
76
- name: "Fed GOAT",
77
- },
78
- }) do |env|
79
- [
78
+ name: "Fed GOAT"
79
+ }
80
+ }) do
81
+ [
80
82
  200,
81
83
  {},
82
84
  {
83
85
  data: {
84
86
  id: 1,
85
- type: 'users',
87
+ type: "users",
86
88
  attributes: {
87
- name: 'Fed GOAT',
88
- },
89
+ name: "Fed GOAT"
90
+ }
89
91
  }
90
-
92
+
91
93
  }.to_json
92
- ]
94
+ ]
93
95
  end
94
96
 
95
- stub.delete("/users/1") { |env|
96
- [ 204, {}, {}, ]
97
- }
97
+ stub.delete("/users/1") do
98
+ [204, {}, {}]
99
+ end
98
100
  end
99
-
100
101
  end
101
102
 
102
103
  spawn_model("Foo::User", type: Her::JsonApi::Model)
103
104
  end
104
105
 
105
- it 'allows configuration of type' do
106
+ it "allows configuration of type" do
106
107
  spawn_model("Foo::Bar", type: Her::JsonApi::Model) do
107
108
  type :foobars
108
109
  end
109
110
 
110
- expect(Foo::Bar.instance_variable_get('@type')).to eql('foobars')
111
+ expect(Foo::Bar.instance_variable_get("@type")).to eql("foobars")
111
112
  end
112
113
 
113
- it 'finds models by id' do
114
+ it "finds models by id" do
114
115
  user = Foo::User.find(1)
115
116
  expect(user.attributes).to eql(
116
- 'id' => 1,
117
- 'name' => 'Roger Federer',
117
+ "id" => 1,
118
+ "name" => "Roger Federer"
118
119
  )
119
120
  end
120
121
 
121
- it 'finds a collection of models' do
122
+ it "finds a collection of models" do
122
123
  users = Foo::User.all
123
- expect(users.map(&:attributes)).to match_array([
124
- {
125
- 'id' => 1,
126
- 'name' => 'Roger Federer',
127
- },
128
- {
129
- 'id' => 2,
130
- 'name' => 'Kei Nishikori',
131
- }
132
- ])
124
+ expect(users.map(&:attributes)).to match_array(
125
+ [
126
+ {
127
+ "id" => 1,
128
+ "name" => "Roger Federer"
129
+ },
130
+ {
131
+ "id" => 2,
132
+ "name" => "Kei Nishikori"
133
+ }
134
+ ]
135
+ )
133
136
  end
134
137
 
135
- it 'creates a Foo::User' do
136
- user = Foo::User.new(name: 'Jeremy Lin')
138
+ it "creates a Foo::User" do
139
+ user = Foo::User.new(name: "Jeremy Lin")
137
140
  user.save
138
141
  expect(user.attributes).to eql(
139
- 'id' => 3,
140
- 'name' => 'Jeremy Lin',
142
+ "id" => 3,
143
+ "name" => "Jeremy Lin"
141
144
  )
142
145
  end
143
146
 
144
- it 'updates a Foo::User' do
147
+ it "updates a Foo::User" do
145
148
  user = Foo::User.find(1)
146
- user.name = 'Fed GOAT'
149
+ user.name = "Fed GOAT"
147
150
  user.save
148
151
  expect(user.attributes).to eql(
149
- 'id' => 1,
150
- 'name' => 'Fed GOAT',
152
+ "id" => 1,
153
+ "name" => "Fed GOAT"
151
154
  )
152
155
  end
153
156
 
154
- it 'destroys a Foo::User' do
157
+ it "destroys a Foo::User" do
155
158
  user = Foo::User.find(1)
156
159
  expect(user.destroy).to be_destroyed
157
160
  end
158
161
 
159
- context 'undefined methods' do
160
- it 'removes methods that are not compatible with json api' do
162
+ context "undefined methods" do
163
+ it "removes methods that are not compatible with json api" do
161
164
  [:parse_root_in_json, :include_root_in_json, :root_element, :primary_key].each do |method|
162
165
  expect { Foo::User.new.send(method, :foo) }.to raise_error NoMethodError, "Her::JsonApi::Model does not support the #{method} configuration option"
163
166
  end