livefyre 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/livefyre/user.rb DELETED
@@ -1,162 +0,0 @@
1
- module Livefyre
2
- # Public: Interface for dealing with Livefyre users by User ID.
3
- class User
4
- attr_accessor :id, :display_name
5
-
6
- # Public: Create a new Livefyre User proxy.
7
- #
8
- # id - [String] ID of the user to proxy
9
- # client - [Livefyre::Client] an instance of Livefyre::Client. If nil, the default client is used.
10
- # display_name - [String] The display name for this user (optional)
11
- def initialize(id, client = nil, display_name = nil, args = {})
12
- @id = id
13
- @client = client || Livefyre.client
14
- @display_name = display_name
15
- @options = args
16
- end
17
-
18
- # Public: Retrieve user information and recent comments for this user from Livefyre
19
- #
20
- # Returns [Hash] of profile data
21
- # Raises [JSON::ParserError] if the returned data cannot be parsed
22
- # Raises [APIException] if the API does not return a valid response
23
- def profile
24
- response = @client.get "/profile/#{id}/", {:actor_token => token}
25
- if response.success?
26
- begin
27
- JSON.parse(response.body)["data"]
28
- rescue JSON::ParserError => e
29
- raise APIException.new("Parse error: #{e.message}")
30
- end
31
- else
32
- raise APIException.new(result.body)
33
- end
34
- end
35
-
36
- # Public: Setter for the client to associate with this user
37
- def client=(client)
38
- @client = client
39
- end
40
-
41
- # Internal - Fetch an internal Jabber-style ID for this user
42
- #
43
- # Returns [String] representation of this user
44
- def jid
45
- "#{id}@#{@client.host}"
46
- end
47
-
48
- # Public: Creates a signed JWT token for this user
49
- #
50
- # max_age - [Integer] Expiry time for this token in seconds (default: 86400)
51
- #
52
- # Returns [String] token
53
- def token(max_age = 86400)
54
- data = {
55
- :domain => @client.host,
56
- :user_id => id,
57
- :expires => Time.now.to_i + max_age
58
- }.tap do |opts|
59
- opts[:display_name] = @display_name unless @display_name.nil?
60
- end
61
- JWT.encode(data, @client.key)
62
- end
63
-
64
- # Public: Update this user's profile on Livefyre
65
- #
66
- # data - [Hash] A hash of user data as defined by the Livefyre user profile schema
67
- #
68
- # Returns [Bool] true on success
69
- # Raises [APIException] if the request failed
70
- def push(data)
71
- result = @client.post "/profiles/?actor_token=#{CGI.escape @client.system_token}&id=#{id}", {:data => data.to_json}
72
- if result.success?
73
- true
74
- else
75
- raise APIException.new(result.body)
76
- end
77
- end
78
-
79
- # Public: Invoke Livefyre ping-to-pull to refresh this user's data
80
- #
81
- # Returns [Bool] true on success
82
- # Raises [APIException] if the request failed
83
- def refresh
84
- result = @client.post "/api/v3_0/user/#{id}/refresh", {:lftoken => @client.system_token}
85
- if result.success?
86
- true
87
- else
88
- raise APIException.new(result.body)
89
- end
90
- end
91
-
92
- # Public: Follow the given conversation
93
- #
94
- # conversation - [Conversation] to follow
95
- # Returns [Boolean] true on success
96
- # Raises [APIException] on failure
97
- def follow_conversation(conversation)
98
- conversation.follow_as self
99
- end
100
-
101
- # Public: Unfollow the given conversation
102
- #
103
- # conversation - [Conversation] to unfollow
104
- # Returns [Boolean] true on success
105
- # Raises [APIException] on failure
106
- def unfollow_conversation(conversation)
107
- conversation.unfollow_as self
108
- end
109
-
110
- # Public: Coerce a string or [User] into a user ID
111
- #
112
- # userish - [String/User/Int]A [User] or user ID
113
- #
114
- # Returns [String] User ID
115
- # Raises Exception when value can't be coerced
116
- def self.get_user_id(userish)
117
- case userish
118
- when String
119
- userish.split("@", 2).first
120
- when Fixnum
121
- userish
122
- when User
123
- userish.id
124
- else
125
- raise "Invalid user ID"
126
- end
127
- end
128
-
129
- # Public: Convenience method to refresh a user by ID
130
- #
131
- # id - A Livefyre user ID to refresh
132
- #
133
- # Returns [Bool] true on success
134
- # Raises [APIException] if the request failed
135
- def self.refresh(id)
136
- new(id).refresh
137
- end
138
-
139
- # Public: Fetch a Livefyre::User from a user record or ID
140
- #
141
- # userish - [String/User/Int] A User or user ID
142
- # client - [Livefyre::Client] Client to bind to the User record
143
- #
144
- # Returns [User]
145
- def self.get_user(userish, client)
146
- case userish
147
- when User
148
- userish.client = client
149
- userish
150
- else
151
- new get_user_id(userish), client
152
- end
153
- end
154
-
155
- # Internal: Returns a cleaner string representation of this object
156
- #
157
- # Returns [String] representation of this class
158
- def to_s
159
- "#<#{self.class.name}:0x#{object_id.to_s(16).rjust(14, "0")} id='#{id}' display_name='#{display_name}'>"
160
- end
161
- end
162
- end
data/railties/railtie.rb DELETED
@@ -1,9 +0,0 @@
1
- module Livefyre
2
- class Railtie < Rails::Railtie
3
- initializer "livefyre.initializer" do
4
- ActionController::Base.send :include, Livefyre::Controller
5
- ActionView::Base.send :include, Livefyre::Helpers
6
- ActiveRecord::Base.send :include, Livefyre::Model if defined?(ActiveRecord)
7
- end
8
- end
9
- end
@@ -1,104 +0,0 @@
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.instance_variable_get("@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
@@ -1,294 +0,0 @@
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( :post => 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( :post => 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
@@ -1,30 +0,0 @@
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
@@ -1,96 +0,0 @@
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 UserWithBlock < BaseUser
31
- attr_accessor :foo
32
- livefyre_user :update_on => [:email] do |o, id|
33
- o.foo = 1
34
- end
35
- end
36
-
37
- class UserWithoutBlock < BaseUser
38
- livefyre_user :update_on => [:email]
39
- end
40
-
41
- class UserWithCustomId < BaseUser
42
- livefyre_user :id => :custom_id, :update_on => [:email]
43
-
44
- def custom_id
45
- "foobar"
46
- end
47
- end
48
-
49
- describe Livefyre::Model do
50
- context "when saving" do
51
- it "should not do anything if the watched fields haven't changed" do
52
- u = UserWithoutBlock.new
53
- u.should_not_receive(:refresh_livefyre)
54
- u.save
55
- end
56
-
57
- it "should call refresh when fields have changed" do
58
- u = UserWithoutBlock.new
59
- u.email = "new@email.com"
60
- u.should_receive(:refresh_livefyre)
61
- u.save
62
- end
63
-
64
- context "an instance that is set to call a block" do
65
- let(:user) { UserWithBlock.new }
66
-
67
- it "should invoke the passed block when refresh_livefyre is called" do
68
- Livefyre::User.should_not_receive(:refresh)
69
- user.email = "new@email.com"
70
- user.save
71
- user.foo.should == 1
72
- end
73
- end
74
-
75
- context "an instance that is not set to defer" do
76
- let(:user) { UserWithoutBlock.new }
77
-
78
- it "should invoke Livefyre::Model::RequestPull when refresh_livefyre is called" do
79
- Livefyre::User.should_receive(:refresh)
80
- user.email = "new@email.com"
81
- user.save
82
- end
83
- end
84
-
85
- context "an instance with a custom ID" do
86
- let(:user) { UserWithCustomId.new }
87
-
88
- it "should invoke Livefyre::Model::RequestPull when refresh_livefyre is called" do
89
- Livefyre::User.should_receive(:refresh)
90
- user.email = "new@email.com"
91
- user.save
92
- end
93
- end
94
- end
95
- end
96
- end