livefyre 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +147 -0
- data/Rakefile +10 -0
- data/app/assets/javascripts/livefyre.js.coffee +72 -0
- data/app/assets/javascripts/livefyre.js.js +117 -0
- data/lib/livefyre/client.rb +117 -0
- data/lib/livefyre/controller_extensions.rb +58 -0
- data/lib/livefyre/domain.rb +186 -0
- data/lib/livefyre/engine.rb +6 -0
- data/lib/livefyre/helpers.rb +56 -0
- data/lib/livefyre/model_extensions.rb +76 -0
- data/lib/livefyre/site.rb +138 -0
- data/lib/livefyre/user.rb +110 -0
- data/lib/livefyre/version.rb +3 -0
- data/lib/livefyre.rb +50 -0
- data/livefyre.gemspec +26 -0
- data/railties/railtie.rb +9 -0
- data/spec/livefyre/client_spec.rb +104 -0
- data/spec/livefyre/domain_spec.rb +294 -0
- data/spec/livefyre/livefyre_spec.rb +30 -0
- data/spec/livefyre/model_spec.rb +92 -0
- data/spec/livefyre/site_spec.rb +211 -0
- data/spec/livefyre/user_spec.rb +122 -0
- data/spec/spec_helper.rb +26 -0
- metadata +196 -0
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Livefyre::Client do
|
4
|
+
it "should not initialize without a host" do
|
5
|
+
expect { Livefyre::Client.new(:key => "x", :system_token => "x") }.to raise_error Exception
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should not initialize without a key" do
|
9
|
+
expect { Livefyre::Client.new(:host => "x", :system_token => "x") }.to raise_error Exception
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not initialize without a system token" do
|
13
|
+
expect { Livefyre::Client.new(:key => "x", :host => "x") }.to raise_error Exception
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should initialize with a key, host, and system token" do
|
17
|
+
Livefyre::Client.new(:key => "x", :network => "x", :system_token => "x").should be_a Livefyre::Client
|
18
|
+
end
|
19
|
+
|
20
|
+
context "an instance" do
|
21
|
+
subject { Livefyre::Client.new(:key => "x", :host => "x", :system_token => "x") }
|
22
|
+
|
23
|
+
describe "#sign" do
|
24
|
+
before { @token = subject.sign({:foo => "bar"}) }
|
25
|
+
|
26
|
+
it "should return a string token" do
|
27
|
+
@token.should be_a String
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should validate" do
|
31
|
+
subject.validate(@token).should be_a Hash
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should decode" do
|
35
|
+
subject.validate(@token)["foo"].should == "bar"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#user" do
|
40
|
+
before { @user = subject.user(1234, "foobar") }
|
41
|
+
it "should return a user" do
|
42
|
+
@user.should be_a Livefyre::User
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should have its ID set" do
|
46
|
+
@user.id.should == 1234
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should have a reference to this client" do
|
50
|
+
@user.client.should == subject
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#set_user_role" do
|
55
|
+
context "with a valid role and scope" do
|
56
|
+
let(:client) { double "client" }
|
57
|
+
|
58
|
+
before do
|
59
|
+
subject.stub(:http_client).and_return(client)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should post an affiliation update for a domain" do
|
63
|
+
client.should_receive(:post).with("/api/v1.1/private/management/user/123@x/role/", {:affiliation=>"admin", :lftoken=>"x", :domain_wide=>1}).and_return( double(:success? => true) )
|
64
|
+
subject.set_user_role(123, "admin", "domain").should == true
|
65
|
+
end
|
66
|
+
|
67
|
+
context "when updating a site affiliation" do
|
68
|
+
it "should fail if no scope ID is passed" do
|
69
|
+
client.should_not_receive(:post)
|
70
|
+
expect { subject.set_user_role(123, "admin", "site") }.to raise_error Exception
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should post an affiliation update for a site" do
|
74
|
+
client.should_receive(:post).with("/api/v1.1/private/management/user/123@x/role/", {:affiliation=>"admin", :lftoken=>"x", :site_id=>123}).and_return( double(:success? => true) )
|
75
|
+
subject.set_user_role(123, "admin", "site", 123).should == true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when updating a conversation affiliation" do
|
80
|
+
it "should fail if no scope ID is passed" do
|
81
|
+
client.should_not_receive(:post)
|
82
|
+
expect { subject.set_user_role(123, "admin", "conv") }.to raise_error Exception
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should post an affiliation update for a site" do
|
86
|
+
client.should_receive(:post).with("/api/v1.1/private/management/user/123@x/role/", {:affiliation=>"admin", :lftoken=>"x", :conv_id=>123}).and_return( double(:success? => true) )
|
87
|
+
subject.set_user_role(123, "admin", "conv", 123).should == true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when it fails" do
|
92
|
+
it "should raise an exception" do
|
93
|
+
client.should_receive(:post).and_return( double(:success? => false, :body => "Failure due to zombie outbreak") )
|
94
|
+
expect { subject.set_user_role(123, "admin", "domain") }.to raise_error(Livefyre::APIException)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should have a valid string representation" do
|
101
|
+
subject.to_s.should match(/Livefyre::Client/)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,294 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Livefyre::Domain do
|
4
|
+
context "an instance" do
|
5
|
+
subject { Livefyre::Domain.new }
|
6
|
+
|
7
|
+
describe "#sites" do
|
8
|
+
context "on success" do
|
9
|
+
before do
|
10
|
+
client = double( :get => double(:success? => true, :body => [{:id => "foo.com"}].to_json), :system_token => "x" )
|
11
|
+
subject.stub(:client).and_return(client)
|
12
|
+
end
|
13
|
+
|
14
|
+
its(:sites) { should be_a Array }
|
15
|
+
|
16
|
+
its("sites.first") { should be_a Livefyre::Site }
|
17
|
+
its("sites.first.id") { should == "foo.com" }
|
18
|
+
end
|
19
|
+
|
20
|
+
context "on failure" do
|
21
|
+
before do
|
22
|
+
client = double( :get => double(:success? => false, :body => ""), :system_token => "x" )
|
23
|
+
subject.stub(:client).and_return(client)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should raise an exception" do
|
27
|
+
expect { subject.sites }.to raise_error(Livefyre::APIException)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#users" do
|
33
|
+
context "on success" do
|
34
|
+
before do
|
35
|
+
client = double( :get => double(:success? => true, :body => [{:id => "foo"}].to_json), :system_token => "x" )
|
36
|
+
subject.stub(:client).and_return(client)
|
37
|
+
end
|
38
|
+
|
39
|
+
its(:users) { should be_a Array }
|
40
|
+
|
41
|
+
its("users.first") { should be_a Livefyre::User }
|
42
|
+
its("users.first.id") { should == "foo" }
|
43
|
+
end
|
44
|
+
|
45
|
+
context "on failure" do
|
46
|
+
before do
|
47
|
+
client = double( :get => double(:success? => false, :body => ""), :system_token => "x" )
|
48
|
+
subject.stub(:client).and_return(client)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should raise an exception" do
|
52
|
+
expect { subject.users }.to raise_error(Livefyre::APIException)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#add_user" do
|
58
|
+
context "on success" do
|
59
|
+
before do
|
60
|
+
client = double( :post => double(:success? => true), :system_token => "x", :host => "some_host", :key => "some_key" )
|
61
|
+
subject.stub(:client).and_return(client)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return true" do
|
65
|
+
subject.add_user({"id" => "valid ID"}).should be true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "on failure" do
|
70
|
+
before do
|
71
|
+
client = double( :post => double(:success? => false, :body => ""), :system_token => "x" )
|
72
|
+
subject.stub(:client).and_return(client)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should raise an exception" do
|
76
|
+
expect { subject.add_user({"id" => "valid ID"}) }.to raise_error(Livefyre::APIException)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should raise an exception when passed an invalid ID" do
|
80
|
+
expect { subject.add_user({"bad_id" => "invalid ID"}) }.to raise_error("Invalid ID")
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#create_site" do
|
86
|
+
context "on success" do
|
87
|
+
before do
|
88
|
+
client = double( :post => double(:success? => true, :body => {"id" => "foo"}.to_json), :system_token => "x", :host => "some_host", :key => "some_key" )
|
89
|
+
subject.stub(:client).and_return(client)
|
90
|
+
@site = subject.create_site("some URL")
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should return a Site" do
|
94
|
+
@site.should be_a Livefyre::Site
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should be prepopulated with the values we just passed" do
|
98
|
+
@site.id.should == "foo"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "on failure" do
|
103
|
+
before do
|
104
|
+
client = double( :post => double(:success? => false, :body => ""), :system_token => "x" )
|
105
|
+
subject.stub(:client).and_return(client)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should raise an exception" do
|
109
|
+
expect { subject.create_site("some_url") }.to raise_error(Livefyre::APIException)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "#owners" do
|
115
|
+
context "on success" do
|
116
|
+
before do
|
117
|
+
client = double( :get => double(:success? => true, :body => ["foo@bar"].to_json), :system_token => "x" )
|
118
|
+
client.should_receive(:user).with("foo").and_return( Livefyre.client.user("foo") )
|
119
|
+
subject.stub(:client).and_return(client)
|
120
|
+
end
|
121
|
+
|
122
|
+
its(:owners) { should be_a Array }
|
123
|
+
|
124
|
+
its("owners.first") { should be_a Livefyre::User }
|
125
|
+
its("owners.first.id") { should == "foo" }
|
126
|
+
end
|
127
|
+
|
128
|
+
context "on failure" do
|
129
|
+
before do
|
130
|
+
client = double( :get => double(:success? => false, :body => ""), :system_token => "x" )
|
131
|
+
subject.stub(:client).and_return(client)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should raise an exception" do
|
135
|
+
expect { subject.owners }.to raise_error(Livefyre::APIException)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#add_owner" do
|
141
|
+
context "on success" do
|
142
|
+
before do
|
143
|
+
client = double( :put => double(:success? => true), :system_token => "x", :host => "some_host", :key => "some_key" )
|
144
|
+
subject.stub(:client).and_return(client)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should return true" do
|
148
|
+
subject.add_owner("some ID").should be true
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context "on failure" do
|
153
|
+
before do
|
154
|
+
client = double( :put => double(:success? => false, :body => ""), :system_token => "x", :host => "some_host", :key => "some_key" )
|
155
|
+
subject.stub(:client).and_return(client)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should raise an exception" do
|
159
|
+
expect { subject.add_owner("some user ID") }.to raise_error(Livefyre::APIException)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe "#remove_owner" do
|
165
|
+
context "on success" do
|
166
|
+
before do
|
167
|
+
client = double( :delete => double(:success? => true), :system_token => "x", :host => "some_host", :key => "some_key" )
|
168
|
+
subject.stub(:client).and_return(client)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should return true" do
|
172
|
+
subject.remove_owner("some ID").should be true
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "on failure" do
|
177
|
+
before do
|
178
|
+
client = double( :delete => double(:success? => false, :body => ""), :system_token => "x", :host => "some_host", :key => "some_key" )
|
179
|
+
subject.stub(:client).and_return(client)
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should raise an exception" do
|
183
|
+
expect { subject.remove_owner("some user ID") }.to raise_error(Livefyre::APIException)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
describe "#admins" do
|
190
|
+
context "on success" do
|
191
|
+
before do
|
192
|
+
client = double( :get => double(:success? => true, :body => ["foo@bar"].to_json), :system_token => "x" )
|
193
|
+
client.should_receive(:user).with("foo").and_return( Livefyre.client.user("foo") )
|
194
|
+
subject.stub(:client).and_return(client)
|
195
|
+
end
|
196
|
+
|
197
|
+
its(:admins) { should be_a Array }
|
198
|
+
|
199
|
+
its("admins.first") { should be_a Livefyre::User }
|
200
|
+
its("admins.first.id") { should == "foo" }
|
201
|
+
end
|
202
|
+
|
203
|
+
context "on failure" do
|
204
|
+
before do
|
205
|
+
client = double( :get => double(:success? => false, :body => ""), :system_token => "x" )
|
206
|
+
subject.stub(:client).and_return(client)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should raise an exception" do
|
210
|
+
expect { subject.admins }.to raise_error(Livefyre::APIException)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
describe "#add_admin" do
|
216
|
+
context "on success" do
|
217
|
+
before do
|
218
|
+
client = double( :post => double(:success? => true), :system_token => "x", :host => "some_host", :key => "some_key" )
|
219
|
+
subject.stub(:client).and_return(client)
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should return true" do
|
223
|
+
subject.add_admin("some ID").should be true
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
context "on failure" do
|
228
|
+
before do
|
229
|
+
client = double( :post => double(:success? => false, :body => ""), :system_token => "x", :host => "some_host", :key => "some_key" )
|
230
|
+
subject.stub(:client).and_return(client)
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should raise an exception" do
|
234
|
+
expect { subject.add_admin("some user ID") }.to raise_error(Livefyre::APIException)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
describe "#remove_admin" do
|
240
|
+
context "on success" do
|
241
|
+
before do
|
242
|
+
client = double( :delete => double(:success? => true), :system_token => "x", :host => "some_host", :key => "some_key" )
|
243
|
+
subject.stub(:client).and_return(client)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should return true" do
|
247
|
+
subject.remove_admin("some ID").should be true
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
context "on failure" do
|
252
|
+
before do
|
253
|
+
client = double( :delete => double(:success? => false, :body => ""), :system_token => "x", :host => "some_host", :key => "some_key" )
|
254
|
+
subject.stub(:client).and_return(client)
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should raise an exception" do
|
258
|
+
expect { subject.remove_admin("some user ID") }.to raise_error(Livefyre::APIException)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe "#set_pull_url" do
|
264
|
+
context "when it succeeds" do
|
265
|
+
before do
|
266
|
+
response = double(:success? => true)
|
267
|
+
client = double(:post => response, :system_token => "x")
|
268
|
+
subject.stub(:client).and_return(client)
|
269
|
+
@response = subject.set_pull_url 'http://foo.bar/{id}/'
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should return true" do
|
273
|
+
@response.should == true
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
context "when it fails" do
|
278
|
+
before do
|
279
|
+
response = double(:success? => false, :body => "failure")
|
280
|
+
client = double(:post => response, :system_token => "x")
|
281
|
+
subject.stub(:client).and_return(client)
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should raise an exception" do
|
285
|
+
expect { subject.set_pull_url 'http://foo.bar/{id}/' }.to raise_error(Livefyre::APIException)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should have a valid string representation" do
|
291
|
+
subject.to_s.should match(/Livefyre::Domain.*host='#{subject.client.host}'/)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Livefyre do
|
4
|
+
context "the default client" do
|
5
|
+
it "should raise an exception if the configuration is not set" do
|
6
|
+
c = Livefyre.config
|
7
|
+
Livefyre.config = nil
|
8
|
+
lambda { Livefyre.client }.should raise_exception(Exception)
|
9
|
+
Livefyre.config = c
|
10
|
+
end
|
11
|
+
|
12
|
+
context "when a configuration is set" do
|
13
|
+
before {
|
14
|
+
Livefyre.config = {:host => "foo.bar", :key => "foo", :system_token => "123"}
|
15
|
+
}
|
16
|
+
|
17
|
+
it "should get a Livefyre::Client" do
|
18
|
+
Livefyre.client.should be_a(Livefyre::Client)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should get the same client instance across multiple calls' do
|
22
|
+
Livefyre.client.should eql(Livefyre.client)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should fetch the config' do
|
26
|
+
Livefyre.config.should == {:host => "foo.bar", :key => "foo", :system_token => "123"}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? Livefyre::Model
|
4
|
+
class BaseUser
|
5
|
+
include Livefyre::Model
|
6
|
+
attr_accessor :id
|
7
|
+
attr_accessor :email
|
8
|
+
|
9
|
+
def email=(email)
|
10
|
+
@email = email
|
11
|
+
@email_changed = true
|
12
|
+
end
|
13
|
+
|
14
|
+
def email_changed?
|
15
|
+
@email_changed
|
16
|
+
end
|
17
|
+
|
18
|
+
def save
|
19
|
+
self.class.instance_variable_get("@callbacks").each do |callback|
|
20
|
+
send callback
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.after_save(callback)
|
25
|
+
@callbacks ||= []
|
26
|
+
@callbacks.push callback
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class UserWithDefer < BaseUser
|
31
|
+
livefyre_user :update_on => [:email], :defer => true
|
32
|
+
end
|
33
|
+
|
34
|
+
class UserWithoutDefer < BaseUser
|
35
|
+
livefyre_user :update_on => [:email]
|
36
|
+
end
|
37
|
+
|
38
|
+
class UserWithCustomId < BaseUser
|
39
|
+
livefyre_user :id => :custom_id, :update_on => [:email]
|
40
|
+
|
41
|
+
def custom_id
|
42
|
+
"foobar"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe Livefyre::Model do
|
47
|
+
context "when saving" do
|
48
|
+
it "should not do anything if the watched fields haven't changed" do
|
49
|
+
u = UserWithDefer.new
|
50
|
+
u.should_not_receive(:refresh_livefyre)
|
51
|
+
u.save
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should call refresh when fields have changed" do
|
55
|
+
u = UserWithDefer.new
|
56
|
+
u.email = "new@email.com"
|
57
|
+
u.should_receive(:refresh_livefyre)
|
58
|
+
u.save
|
59
|
+
end
|
60
|
+
|
61
|
+
context "an instance that is set to defer" do
|
62
|
+
let(:user) { UserWithDefer.new }
|
63
|
+
|
64
|
+
it "should invoke Resque when refresh_livefyre is called" do
|
65
|
+
Resque.should_receive(:enqueue).with(Livefyre::Model::RequestPull, user.id)
|
66
|
+
user.email = "new@email.com"
|
67
|
+
user.save
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "an instance that is not set to defer" do
|
72
|
+
let(:user) { UserWithoutDefer.new }
|
73
|
+
|
74
|
+
it "should invoke Livefyre::Model::RequestPull when refresh_livefyre is called" do
|
75
|
+
Livefyre::Model::RequestPull.should_receive(:perform).with(user.id)
|
76
|
+
user.email = "new@email.com"
|
77
|
+
user.save
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "an instance with a custom ID" do
|
82
|
+
let(:user) { UserWithCustomId.new }
|
83
|
+
|
84
|
+
it "should invoke Livefyre::Model::RequestPull when refresh_livefyre is called" do
|
85
|
+
Livefyre::Model::RequestPull.should_receive(:perform) #.with(user.custom_id)
|
86
|
+
user.email = "new@email.com"
|
87
|
+
user.save
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|