harpy 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -122,6 +122,13 @@ module Harpy
122
122
  end
123
123
  end
124
124
 
125
+ def destroy
126
+ raise Harpy::UrlRequired unless url
127
+ _run_destroy_callbacks do
128
+ process_response Harpy.client.delete(url), :destroy
129
+ end
130
+ end
131
+
125
132
  def link(rel)
126
133
  link = (@attrs["link"]||[]).detect{|l| l["rel"]==rel.to_s}
127
134
  link["href"] if link
@@ -184,7 +191,7 @@ module Harpy
184
191
  Harpy.client.invalid_code response
185
192
  end
186
193
  end
187
-
194
+
188
195
  def method_missing(method, *args)
189
196
  if persisted? && !@attrs.has_key?(method.to_s)
190
197
  super
@@ -1,3 +1,3 @@
1
1
  module Harpy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -27,6 +27,7 @@ module Harpy
27
27
  before_save :callback_before_save
28
28
  before_create :callback_before_create
29
29
  before_update :callback_before_update
30
+ before_destroy :callback_before_destroy
30
31
  attr_reader :callbacks
31
32
  def initialize(*args)
32
33
  @callbacks = []
@@ -44,6 +45,9 @@ module Harpy
44
45
  def callback_before_update
45
46
  @callbacks << :update
46
47
  end
48
+ def callback_before_destroy
49
+ @callbacks << :destroy
50
+ end
47
51
  end
48
52
  end
49
53
  end
@@ -545,4 +549,78 @@ describe "class including Harpy::Resource" do
545
549
  end
546
550
  end
547
551
  end
552
+ describe "#destroy" do
553
+ subject{ Harpy::Spec::User.new "company_name" => "Stark Enterprises" }
554
+ context "when link to self is missing" do
555
+ it "raises Harpy::UrlRequired" do
556
+ lambda{ subject.destroy }.should raise_error Harpy::UrlRequired
557
+ subject.callbacks.should =~ []
558
+ end
559
+ end
560
+ context "when link to self is present" do
561
+ let(:url) { "http://localhost/user/1" }
562
+ subject do
563
+ Harpy::Spec::User.new({
564
+ "urn" => "urn:harpy:user:1",
565
+ "company_name" => "Stark Enterprises",
566
+ "link" => [{"rel" => "self", "href" => url}],
567
+ })
568
+ end
569
+ [200, 201, 302].each do |response_code|
570
+ it "is true and merges response attributes on #{response_code}" do
571
+ response = Typhoeus::Response.new :code => response_code, :body => <<-eos
572
+ {
573
+ "firstname": "Anthony",
574
+ "urn": "urn:harpy:user:1",
575
+ "link": [
576
+ {"rel": "self", "href": "#{url}/1"}
577
+ ]
578
+ }
579
+ eos
580
+ Harpy.client.should_receive(:delete).with(url).and_return response
581
+ subject.destroy.should be_true
582
+ subject.callbacks.should =~ [:destroy]
583
+ subject.firstname.should == "Anthony"
584
+ subject.company_name.should == "Stark Enterprises"
585
+ end
586
+ end
587
+ it "is true but doesn't touch attributes on 204" do
588
+ response = Typhoeus::Response.new :code => 204
589
+ Harpy.client.should_receive(:delete).with(url).and_return response
590
+ subject.destroy.should be_true
591
+ subject.callbacks.should =~ [:destroy]
592
+ subject.company_name.should == "Stark Enterprises"
593
+ end
594
+ it "raises Harpy::Unauthorized on 401" do
595
+ response = Typhoeus::Response.new :code => 401
596
+ Harpy.client.should_receive(:delete).with(url).and_return response
597
+ lambda { subject.destroy }.should raise_error Harpy::Unauthorized, "Server returned a 401 response code"
598
+ subject.callbacks.should =~ [:destroy]
599
+ end
600
+ it "is false and fills in errors on 422" do
601
+ response = Typhoeus::Response.new :code => 422, :body => <<-eos
602
+ {
603
+ "firstname": "Anthony",
604
+ "errors": {
605
+ "lastname": ["can't be blank", "must be unique"]
606
+ }
607
+ }
608
+ eos
609
+ Harpy.client.should_receive(:delete).with(url).and_return response
610
+ subject.destroy.should be_false
611
+ subject.callbacks.should =~ [:destroy]
612
+ subject.should have(1).error
613
+ subject.errors[:lastname].should =~ ["can't be blank", "must be unique"]
614
+ lambda { subject.firstname }.should raise_error NoMethodError
615
+ subject.company_name.should == "Stark Enterprises"
616
+ end
617
+ it "delegates other response codes to client" do
618
+ response = Typhoeus::Response.new :code => 500
619
+ Harpy.client.should_receive(:delete).with(url).and_return response
620
+ Harpy.client.should_receive(:invalid_code).with(response)
621
+ subject.destroy
622
+ subject.callbacks.should =~ [:destroy]
623
+ end
624
+ end
625
+ end
548
626
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harpy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,12 +10,12 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-06-16 00:00:00.000000000 +02:00
13
+ date: 2011-06-28 00:00:00.000000000 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: typhoeus
18
- requirement: &2165828200 !ruby/object:Gem::Requirement
18
+ requirement: &2168462220 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: 0.2.4
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2165828200
26
+ version_requirements: *2168462220
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
- requirement: &2165827680 !ruby/object:Gem::Requirement
29
+ requirement: &2168461700 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ! '>='
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: 3.0.0
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *2165827680
37
+ version_requirements: *2168461700
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: activemodel
40
- requirement: &2165827200 !ruby/object:Gem::Requirement
40
+ requirement: &2168461220 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ! '>='
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: 3.0.0
46
46
  type: :runtime
47
47
  prerelease: false
48
- version_requirements: *2165827200
48
+ version_requirements: *2168461220
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: hash-deep-merge
51
- requirement: &2165826720 !ruby/object:Gem::Requirement
51
+ requirement: &2168460740 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ~>
@@ -56,10 +56,10 @@ dependencies:
56
56
  version: 0.1.1
57
57
  type: :runtime
58
58
  prerelease: false
59
- version_requirements: *2165826720
59
+ version_requirements: *2168460740
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: yajl-ruby
62
- requirement: &2165826240 !ruby/object:Gem::Requirement
62
+ requirement: &2168460260 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
65
65
  - - ~>
@@ -67,10 +67,10 @@ dependencies:
67
67
  version: 0.8.2
68
68
  type: :runtime
69
69
  prerelease: false
70
- version_requirements: *2165826240
70
+ version_requirements: *2168460260
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rake
73
- requirement: &2165825760 !ruby/object:Gem::Requirement
73
+ requirement: &2168459780 !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
76
76
  - - ~>
@@ -78,10 +78,10 @@ dependencies:
78
78
  version: 0.8.7
79
79
  type: :development
80
80
  prerelease: false
81
- version_requirements: *2165825760
81
+ version_requirements: *2168459780
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rspec
84
- requirement: &2156256500 !ruby/object:Gem::Requirement
84
+ requirement: &2168459300 !ruby/object:Gem::Requirement
85
85
  none: false
86
86
  requirements:
87
87
  - - ~>
@@ -89,10 +89,10 @@ dependencies:
89
89
  version: 2.6.0
90
90
  type: :development
91
91
  prerelease: false
92
- version_requirements: *2156256500
92
+ version_requirements: *2168459300
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: rocco
95
- requirement: &2156256020 !ruby/object:Gem::Requirement
95
+ requirement: &2168458820 !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements:
98
98
  - - ~>
@@ -100,7 +100,7 @@ dependencies:
100
100
  version: '0.7'
101
101
  type: :development
102
102
  prerelease: false
103
- version_requirements: *2156256020
103
+ version_requirements: *2168458820
104
104
  description: Client for REST API with HATEOAS
105
105
  email:
106
106
  - joseph.halter@thetalentbox.com