socialcastr 0.2.9 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/socialcastr/base.rb +44 -43
- data/lib/socialcastr/comment.rb +4 -4
- data/lib/socialcastr/message.rb +9 -8
- data/lib/socialcastr/version.rb +1 -1
- data/spec/message_spec.rb +18 -39
- metadata +12 -12
data/lib/socialcastr/base.rb
CHANGED
@@ -2,11 +2,13 @@ require 'nokogiri'
|
|
2
2
|
require 'socialcastr/sax/active_resource'
|
3
3
|
module Socialcastr
|
4
4
|
class Base
|
5
|
-
|
5
|
+
attr_accessor :prefix_options
|
6
|
+
def initialize(arguments={}, prefix_options={})
|
6
7
|
@data = {}
|
7
|
-
arguments.
|
8
|
+
arguments.each_pair do |k,v|
|
8
9
|
@data[k.to_s] = v
|
9
10
|
end
|
11
|
+
@prefix_options = prefix_options
|
10
12
|
end
|
11
13
|
|
12
14
|
def save
|
@@ -14,25 +16,32 @@ module Socialcastr
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def create
|
17
|
-
|
18
|
-
|
19
|
-
|
19
|
+
api.post(collection_path, to_params).tap do |xml|
|
20
|
+
copy_attributes_from_object(self.class.parse(xml))
|
21
|
+
end
|
20
22
|
end
|
21
23
|
|
22
24
|
def update
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
api.put(element_path, to_params).tap do |xml|
|
26
|
+
copy_attributes_from_object(self.class.parse(xml))
|
27
|
+
end
|
26
28
|
end
|
27
29
|
|
28
30
|
def refresh
|
29
|
-
|
30
|
-
|
31
|
-
|
31
|
+
api.get(element_path).tap do |xml|
|
32
|
+
copy_attributes_from_object(self.class.parse(xml))
|
33
|
+
end
|
34
|
+
return self
|
35
|
+
end
|
36
|
+
|
37
|
+
def destroy
|
38
|
+
api.delete(element_path)
|
39
|
+
return self
|
32
40
|
end
|
33
41
|
|
34
42
|
def copy_attributes_from_object(object=nil)
|
35
43
|
@data = object.instance_variable_get("@data")
|
44
|
+
return self
|
36
45
|
end
|
37
46
|
|
38
47
|
def new?
|
@@ -43,12 +52,12 @@ module Socialcastr
|
|
43
52
|
self.class.api
|
44
53
|
end
|
45
54
|
|
46
|
-
def element_path
|
47
|
-
self.class.element_path(self.id, prefix_options)
|
55
|
+
def element_path
|
56
|
+
self.class.element_path(self.id, @prefix_options)
|
48
57
|
end
|
49
58
|
|
50
|
-
def collection_path
|
51
|
-
self.class.collection_path(
|
59
|
+
def collection_path
|
60
|
+
self.class.collection_path(@prefix_options)
|
52
61
|
end
|
53
62
|
|
54
63
|
def to_params
|
@@ -59,6 +68,10 @@ module Socialcastr
|
|
59
68
|
params
|
60
69
|
end
|
61
70
|
|
71
|
+
def to_prefix_options
|
72
|
+
@prefix_options.merge "#{self.class.model_name.downcase}_id" => id
|
73
|
+
end
|
74
|
+
|
62
75
|
def param_name(variable_name)
|
63
76
|
"#{self.class.model_name.downcase}[#{variable_name}]"
|
64
77
|
end
|
@@ -77,41 +90,29 @@ module Socialcastr
|
|
77
90
|
else
|
78
91
|
obj = @data[method_name(method)]
|
79
92
|
unless new?
|
80
|
-
|
81
|
-
obj.add_container_id(self)
|
82
|
-
end
|
83
|
-
if obj.class == Array
|
84
|
-
obj.each do |o|
|
85
|
-
o.add_container_id(self)
|
86
|
-
end
|
87
|
-
end
|
93
|
+
self.class.set_prefix_options(obj, to_prefix_options)
|
88
94
|
end
|
89
95
|
return obj
|
90
96
|
end
|
91
97
|
end
|
92
98
|
|
93
|
-
def add_container_id(container)
|
94
|
-
self.send("#{container.class.model_name.downcase}_id=", container.id)
|
95
|
-
end
|
96
|
-
|
97
99
|
class << self
|
98
|
-
def parse(xml="")
|
99
|
-
|
100
|
-
Nokogiri::XML::SAX::Parser.new(
|
101
|
-
|
102
|
-
when Hash
|
103
|
-
return from_hash(source.data)
|
104
|
-
else
|
105
|
-
return source.data
|
106
|
-
end
|
100
|
+
def parse(xml="", prefix_options={})
|
101
|
+
resource = SAX::ActiveResource.new
|
102
|
+
Nokogiri::XML::SAX::Parser.new(resource).parse(xml)
|
103
|
+
return set_prefix_options(resource.data, prefix_options)
|
107
104
|
end
|
108
105
|
|
109
|
-
def
|
110
|
-
obj
|
111
|
-
|
112
|
-
obj
|
106
|
+
def set_prefix_options(obj, prefix_options={})
|
107
|
+
if obj.class.ancestors.include? Socialcastr::Base
|
108
|
+
obj.prefix_options = prefix_options
|
109
|
+
return obj
|
110
|
+
end
|
111
|
+
if obj.class == Array
|
112
|
+
obj.each do |o|
|
113
|
+
set_prefix_options(o, prefix_options)
|
114
|
+
end
|
113
115
|
end
|
114
|
-
return obj
|
115
116
|
end
|
116
117
|
|
117
118
|
def from_hash(h)
|
@@ -137,13 +138,13 @@ module Socialcastr
|
|
137
138
|
def find_single(id, options)
|
138
139
|
(prefix_options, query_options) = parse_options(options)
|
139
140
|
path = element_path(id, prefix_options)
|
140
|
-
|
141
|
+
parse(api.get(path, query_options), prefix_options)
|
141
142
|
end
|
142
143
|
|
143
144
|
def find_every(options)
|
144
145
|
(prefix_options, query_options) = parse_options(options)
|
145
146
|
path = collection_path(prefix_options)
|
146
|
-
|
147
|
+
parse(api.get(path, query_options), prefix_options)
|
147
148
|
end
|
148
149
|
|
149
150
|
def all(*arguments)
|
data/lib/socialcastr/comment.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
module Socialcastr
|
2
2
|
class Comment < Base
|
3
3
|
def like!
|
4
|
-
|
4
|
+
Socialcastr::Like.new({}, to_prefix_options).save
|
5
5
|
refresh
|
6
6
|
return self
|
7
7
|
end
|
8
8
|
|
9
9
|
def unlike!
|
10
|
-
|
10
|
+
like.destroy
|
11
11
|
refresh
|
12
12
|
return self
|
13
13
|
end
|
@@ -20,8 +20,8 @@ module Socialcastr
|
|
20
20
|
self.user.id != api_id
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
self.likes.select { |like| like.unlikable_by?(api.profile.id) }.first
|
23
|
+
def like
|
24
|
+
self.likes.select { |like| like.unlikable_by?(api.profile.id) }.first
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/socialcastr/message.rb
CHANGED
@@ -4,7 +4,8 @@ module Socialcastr
|
|
4
4
|
|
5
5
|
def flag!
|
6
6
|
return true if flagged?
|
7
|
-
|
7
|
+
Socialcastr::Flag.new({}, :message_id => id).save
|
8
|
+
refresh
|
8
9
|
end
|
9
10
|
|
10
11
|
def flagged?
|
@@ -13,24 +14,24 @@ module Socialcastr
|
|
13
14
|
|
14
15
|
def unflag!
|
15
16
|
return unless flagged?
|
16
|
-
|
17
|
-
|
17
|
+
self.flag.destroy
|
18
|
+
refresh
|
18
19
|
end
|
19
20
|
|
20
21
|
def like!
|
21
|
-
|
22
|
-
|
22
|
+
Socialcastr::Like.new({}, :message_id => id).save
|
23
|
+
refresh
|
23
24
|
end
|
24
25
|
|
25
26
|
def unlike!
|
26
27
|
self.likes.reject! do |l|
|
27
|
-
l.unlikable &&
|
28
|
+
l.unlikable && l.destroy
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
def comment!(arguments={})
|
32
|
-
|
33
|
-
|
33
|
+
Socialcastr::Comment.new(arguments, :message_id => id).save
|
34
|
+
refresh
|
34
35
|
end
|
35
36
|
|
36
37
|
|
data/lib/socialcastr/version.rb
CHANGED
data/spec/message_spec.rb
CHANGED
@@ -67,24 +67,20 @@ describe Socialcastr::Message do
|
|
67
67
|
|
68
68
|
context 'flagging a message' do
|
69
69
|
before :each do
|
70
|
-
fake_socialcast_api_for(:message)
|
71
|
-
|
72
|
-
|
73
|
-
@api = mock(:api)
|
74
|
-
response = "<flag><id type='integer'>3333</id></flag>"
|
75
|
-
Socialcastr::Message.stub!(:api).and_return(@api)
|
76
|
-
@api.stub!(:post).and_return(response)
|
70
|
+
fake_socialcast_api_for(:message)
|
71
|
+
@message = Socialcastr::Message.find(425)
|
72
|
+
@flag = mock('flag', :save => true, :id => 1)
|
77
73
|
end
|
78
74
|
|
79
75
|
it "should assign an id to the Flag" do
|
76
|
+
Socialcastr::Flag.should_receive(:new).and_return(@flag)
|
80
77
|
@message.flag!
|
81
|
-
@message.flag.id.should == 3333
|
82
78
|
end
|
83
79
|
|
84
80
|
context "unflagging the message" do
|
85
81
|
before :each do
|
86
|
-
@message.flag
|
87
|
-
@
|
82
|
+
@message.flag = @flag
|
83
|
+
@flag.should_receive(:destroy)
|
88
84
|
end
|
89
85
|
|
90
86
|
it "should not be flagged afterwards" do
|
@@ -101,41 +97,25 @@ describe Socialcastr::Message do
|
|
101
97
|
|
102
98
|
context 'liking a message' do
|
103
99
|
before :each do
|
104
|
-
fake_socialcast_api_for :message
|
105
|
-
|
106
|
-
end
|
100
|
+
fake_socialcast_api_for :message
|
101
|
+
@message = Socialcastr::Message.find(425)
|
107
102
|
|
108
|
-
@
|
109
|
-
response = "<like><id type='integer'>2222</id><unlikable>true</unlikable></like>"
|
110
|
-
Socialcastr::Message.stub!(:api).and_return(@api)
|
111
|
-
@api.stub!(:post).and_return(response)
|
103
|
+
@like = mock('like', :save => true)
|
112
104
|
end
|
113
105
|
|
114
106
|
it 'should create a new like' do
|
115
|
-
|
116
|
-
@message.like!
|
117
|
-
@message.likes.count.should == old_count + 1
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'should assign an id the the new like' do
|
107
|
+
Socialcastr::Like.should_receive(:new).and_return(@like)
|
121
108
|
@message.like!
|
122
|
-
@message.likes.last.id.should == 2222
|
123
109
|
end
|
124
110
|
end
|
125
111
|
|
126
112
|
context "unliking a message" do
|
127
113
|
before :each do
|
128
|
-
fake_socialcast_api_for :message
|
129
|
-
|
130
|
-
end
|
114
|
+
fake_socialcast_api_for :message
|
115
|
+
@message = Socialcastr::Message.find(425)
|
131
116
|
|
132
|
-
@
|
133
|
-
|
134
|
-
Socialcastr::Message.stub!(:api).and_return(@api)
|
135
|
-
@api.stub!(:post).and_return(response)
|
136
|
-
@api.stub!(:delete).and_return("")
|
137
|
-
@message.like!
|
138
|
-
@message.likes.count.should == 1
|
117
|
+
@like = mock('like', :unlikable => true, :save => true, :destroy => true)
|
118
|
+
@message.stub!(:likes).and_return([@like])
|
139
119
|
end
|
140
120
|
|
141
121
|
it "should remove a like" do
|
@@ -158,16 +138,15 @@ describe Socialcastr::Message do
|
|
158
138
|
|
159
139
|
context "commenting a message" do
|
160
140
|
before :each do
|
161
|
-
fake_socialcast_api_for :message
|
162
|
-
|
163
|
-
end
|
141
|
+
fake_socialcast_api_for :message
|
142
|
+
@message = Socialcastr::Message.find(425)
|
164
143
|
end
|
165
144
|
|
166
145
|
it "should post to messages/425/comments.xml" do
|
167
146
|
@api = mock(:api)
|
168
147
|
response = "<comment></comment>"
|
169
|
-
Socialcastr::
|
170
|
-
@api.should_receive(:post).with("
|
148
|
+
Socialcastr::Comment.stub!(:api).and_return(@api)
|
149
|
+
@api.should_receive(:post).with("messages/425/comments", {"comment[text]" => "hallo world"}).and_return(response)
|
171
150
|
@message.comment! :text => "hallo world"
|
172
151
|
end
|
173
152
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socialcastr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-07-
|
12
|
+
date: 2011-07-24 00:00:00.000000000 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
17
|
-
requirement: &
|
17
|
+
requirement: &2153369440 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0.8'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2153369440
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rspec
|
28
|
-
requirement: &
|
28
|
+
requirement: &2153368940 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '2.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2153368940
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: yard
|
39
|
-
requirement: &
|
39
|
+
requirement: &2153368560 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2153368560
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: artifice
|
50
|
-
requirement: &
|
50
|
+
requirement: &2153368100 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *2153368100
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: nokogiri
|
61
|
-
requirement: &
|
61
|
+
requirement: &2153367600 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
version: 1.4.4
|
67
67
|
type: :runtime
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *2153367600
|
70
70
|
description: A Ruby wrapper for the Socialcast REST API
|
71
71
|
email:
|
72
72
|
- bru@codewitch.org
|