Class: Livefyre::Site
- Inherits:
-
Object
- Object
- Livefyre::Site
- Defined in:
- lib/livefyre/site.rb
Overview
An object representing a Livefyre site belonging to a Livefyre domain
Instance Attribute Summary (collapse)
Class Method Summary (collapse)
-
+ (Bool) validate_signature(params, secret, time_window = 300)
Validate a signature as passed by the Livefyre postback service.
Instance Method Summary (collapse)
-
- (Bool) add_admin(user)
Adds a user to the list of admins for this site.
-
- (Bool) add_owner(user)
Adds a user to the list of owners for this site.
-
- (Array<Livefyre::User>) admins
Retrieve a list of owners associated with this site.
-
- (Array<Comment>) comments(since = nil)
Fetches the latest comments from this site.
-
- (Conversation) create_conversation(article_id, title, link, tags = nil)
Create a conversation collection on this site.
-
- (Array<Activity>) feed(since_id = nil)
Fetches a feed of the site's latest activity.
-
- (Site) initialize(id, client = nil, options = {})
constructor
Create a new Site.
-
- (Array<Livefyre::User>) owners
Retrieve a list of owners associated with this site.
-
- (Hash) properties(reload = false)
Get a list of properties for this site.
-
- (Object) reload
Reload this site's properties from Livefyre.
-
- (Bool) remove_admin(user)
Removes a user from the list of admins for this site.
-
- (Bool) remove_owner(user)
Removes a user from the list of owners for this site.
-
- (Array<Conversation>) search_conversations(query, options = {})
Search conversations on this domain.
-
- (Bool) set_postback_url(url)
Set the postback URL for actions on this site.
-
- (String) to_s
private
Returns a cleaner string representation of this object.
Constructor Details
- (Site) initialize(id, client = nil, options = {})
Create a new Site
10 11 12 13 14 15 |
# File 'lib/livefyre/site.rb', line 10 def initialize(id, client = nil, = {}) @id = id @client = client || Livefyre.client @options = @secret = ["api_secret"] end |
Instance Attribute Details
- (Object) client
7 8 9 |
# File 'lib/livefyre/site.rb', line 7 def client @client end |
- (Object) id
7 8 9 |
# File 'lib/livefyre/site.rb', line 7 def id @id end |
- (Object) options
7 8 9 |
# File 'lib/livefyre/site.rb', line 7 def @options end |
- (Object) secret
7 8 9 |
# File 'lib/livefyre/site.rb', line 7 def secret @secret end |
Class Method Details
+ (Bool) validate_signature(params, secret, time_window = 300)
Validate a signature as passed by the Livefyre postback service
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/livefyre/site.rb', line 215 def self.validate_signature(params, secret, time_window = 300) params = params.clone params.delete :controller params.delete :action sig = (params.delete(:sig) || "").strip raise InvalidSignatureException.new "Missing sig" if sig.nil? raise InvalidSignatureException.new "Missing site key" if secret.nil? hash_str = params.sort.map {|v| v.join("=") }.join("&") if time_window created_at = params[:sig_created] raise InvalidSignatureException.new "Missing sig_created" if created_at.nil? raise InvalidSignatureException.new "Invalid timestamp" if (Time.now.utc - Time.at(created_at.to_i).utc).abs > time_window end check = Base64.encode64 HMAC::SHA1.new(Base64.decode64 secret).update(hash_str).digest check = check.strip raise InvalidSignatureException.new "Invalid signature" if check != sig return sig == check end |
Instance Method Details
- (Bool) add_admin(user)
Adds a user to the list of admins for this site
168 169 170 171 172 173 174 175 176 |
# File 'lib/livefyre/site.rb', line 168 def add_admin(user) user = User.get_user(user, client) response = client.post "/site/#{id}/admins/?actor_token=#{CGI.escape client.system_token}", {:jid => user.jid} if response.success? true else raise APIException.new(response.body) end end |
- (Bool) add_owner(user)
Adds a user to the list of owners for this site
125 126 127 128 129 130 131 132 133 |
# File 'lib/livefyre/site.rb', line 125 def add_owner(user) uid = User.get_user_id(user) response = client.post "/site/#{id}/owners/?actor_token=#{CGI.escape client.system_token}", {:jid => client.jid(uid)} if response.success? true else raise APIException.new(response.body) end end |
- (Array<Livefyre::User>) admins
Retrieve a list of owners associated with this site
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/livefyre/site.rb', line 153 def admins response = client.get "/site/#{id}/admins/", {:actor_token => client.system_token} if response.success? JSON.parse(response.body).map do |u| client.user u.split("@", 2).first end else raise APIException.new(response.body) end end |
- (Array<Comment>) comments(since = nil)
Fetches the latest comments from this site
77 78 79 |
# File 'lib/livefyre/site.rb', line 77 def comments(since = nil) feed(since).select(&:comment?).map(&:comment) end |
- (Conversation) create_conversation(article_id, title, link, tags = nil)
Create a conversation collection on this site
195 196 197 |
# File 'lib/livefyre/site.rb', line 195 def create_conversation(article_id, title, link, = nil) Conversation.create(client, article_id, title, link, ) end |
- (Array<Activity>) feed(since_id = nil)
Fetches a feed of the site's latest activity.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/livefyre/site.rb', line 59 def feed(since_id = nil) reload if secret.nil? = Time.now.utc.to_i sig = Base64.encode64 HMAC::SHA1.new(Base64.decode64 secret).update("sig_created=%s" % ).digest url = "/%s/" % ["site", id, "sync", since_id].compact.join("/") response = client.get url, {:sig_created => , :sig => sig} if response.success? payload = JSON.parse(response.body).map {|item| Activity.new(client, item) } else raise APIException.new(response.body) end end |
- (Array<Livefyre::User>) owners
Retrieve a list of owners associated with this site
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/livefyre/site.rb', line 110 def owners response = client.get "/site/#{id}/owners/", {:actor_token => client.system_token} if response.success? JSON.parse(response.body).map do |u| client.user u.split("@", 2).first end else raise APIException.new(response.body) end end |
- (Hash) properties(reload = false)
Get a list of properties for this site
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/livefyre/site.rb', line 42 def properties(reload = false) return @options unless @options.nil? or @options.empty? or reload response = client.get "/site/#{id}/", {:actor_token => client.system_token} if response.success? @options = JSON.parse response.body @secret = ["api_secret"] @options else raise APIException.new(response.body) end end |
- (Object) reload
Reload this site's properties from Livefyre
84 85 86 87 |
# File 'lib/livefyre/site.rb', line 84 def reload properties(true) self end |
- (Bool) remove_admin(user)
Removes a user from the list of admins for this site
182 183 184 185 186 187 188 189 190 |
# File 'lib/livefyre/site.rb', line 182 def remove_admin(user) user = User.get_user(user, client) response = client.delete "/site/#{id}/admin/#{user.jid}/?actor_token=#{CGI.escape client.system_token}" if response.success? true else raise APIException.new(response.body) end end |
- (Bool) remove_owner(user)
Removes a user from the list of owners for this site
139 140 141 142 143 144 145 146 147 |
# File 'lib/livefyre/site.rb', line 139 def remove_owner(user) user = User.get_user(user, client) response = client.delete "/site/#{id}/owner/#{user.jid}/?actor_token=#{CGI.escape client.system_token}" if response.success? true else raise APIException.new(response.body) end end |
- (Array<Conversation>) search_conversations(query, options = {})
Search conversations on this domain
31 32 33 34 |
# File 'lib/livefyre/site.rb', line 31 def search_conversations(query, = {}) [:sites] = [self] Domain.new(@client).search_conversations(query, ) end |
- (Bool) set_postback_url(url)
Set the postback URL for actions on this site
See: https://github.com/Livefyre/livefyre-docs/wiki/Accessing-Site-Comment-Data
96 97 98 99 100 101 102 103 104 |
# File 'lib/livefyre/site.rb', line 96 def set_postback_url(url) response = client.post "/site/#{id}/", {:actor_token => client.system_token, :postback_url => url} if response.success? properties(true) rescue APIException nil true else raise APIException.new(response.body) end end |
- (String) to_s
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a cleaner string representation of this object
202 203 204 |
# File 'lib/livefyre/site.rb', line 202 def to_s "#<#{self.class.name}:0x#{object_id.to_s(16).rjust(14, "0")} id='#{id}' secret='#{secret}' options=#{.inspect}>" end |