Class: Livefyre::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/livefyre/site.rb

Overview

Public: An object representing a Livefyre site belonging to a Livefyre domain

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, client = nil, options = {}) ⇒ Site

Public: Create a new Site



10
11
12
13
14
15
# File 'lib/livefyre/site.rb', line 10

def initialize(id, client = nil, options = {})
  @id = id
  @client = client || Livefyre.client
  @options = options
  @secret = options["api_secret"]
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/livefyre/site.rb', line 7

def client
  @client
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/livefyre/site.rb', line 7

def id
  @id
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/livefyre/site.rb', line 7

def options
  @options
end

#secretObject

Returns the value of attribute secret.



7
8
9
# File 'lib/livefyre/site.rb', line 7

def secret
  @secret
end

Class Method Details

.validate_signature(params, secret, time_window = 300) ⇒ Object

Public: Validate a signature as passed by the Livefyre postback service

params - Hash of request parameters secret - Site key to validate signature with time_window - Enforce that the sig_created is within time_window seconds of the current time.

Slush is given to  for system time drift. Pass nil or false to disable timestamp checking.

Returns [Bool] Raises [InvalidSignatureException] on failure



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

#add_admin(user) ⇒ Object

Public: Adds a user to the list of admins for this site

Returns [Bool] true on success Raises [APIException] when response is not valid



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

#add_owner(user) ⇒ Object

Public: Adds a user to the list of owners for this site

Returns [Bool] true on success Raises [APIException] when response is not valid



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

#adminsObject

Public: Retrieve a list of owners associated with this site

Returns [Array<Livefyre::User>] A list of users Raises: [APIException] when response is not valid



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

#comments(since = nil) ⇒ Object

Public: Fetches the latest comments from this site

since_id - [Integer] If provided, will return feed items after the given comment.

Returns: [Array<Comment>] List of comment



77
78
79
# File 'lib/livefyre/site.rb', line 77

def comments(since = nil)
  feed(since).select(&:comment?).map(&:comment)
end

#create_conversation(article_id, title, link, tags = nil) ⇒ Object

Public: Create a conversation collection on this site

Returns [Conversation]



195
196
197
# File 'lib/livefyre/site.rb', line 195

def create_conversation(article_id, title, link, tags = nil)
  Conversation.create(client, article_id, title, link, tags)
end

#feed(since_id = nil) ⇒ Object

Public: Fetches a feed of the site’s latest activity.

since_id - [Integer] If provided, will return feed items after the given feed item.

Returns [Array<Activity>] List of feed activities



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?
  timestamp = Time.now.utc.to_i
  sig = Base64.encode64 HMAC::SHA1.new(Base64.decode64 secret).update("sig_created=%s" % timestamp).digest
  url = "/%s/" % ["site", id, "sync", since_id].compact.join("/")
  response = client.get url, {:sig_created => timestamp, :sig => sig}
  if response.success?
    payload = JSON.parse(response.body).map {|item| Activity.new(client, item) }
  else
    raise APIException.new(response.body)
  end
end

#ownersObject

Public: Retrieve a list of owners associated with this site

Returns [Array<Livefyre::User>] A list of users Raises: APIException when response is not valid



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

#properties(reload = false) ⇒ Object

Public: Get a list of properties for this site

reload - Force a reload when set

Returns [Hash] Site properties Raises [APIException] when response is not valid



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 = options["api_secret"]
    @options
  else
    raise APIException.new(response.body)
  end
end

#reloadObject

Public: Reload this site’s properties from Livefyre

Returns self



84
85
86
87
# File 'lib/livefyre/site.rb', line 84

def reload
  properties(true)
  self
end

#remove_admin(user) ⇒ Object

Public: Removes a user from the list of admins for this site

Returns [Bool] true on success Raises [APIException] when response is not valid



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

#remove_owner(user) ⇒ Object

Public: Removes a user from the list of owners for this site

Returns [Bool] true on success Raises [APIException] when response is not valid



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

#search_conversations(query, options = {}) ⇒ Object

Public: Search conversations on this domain

query - string to query for options - [Hash] of options :fields - list of fields to search. Default [:article, :title, :body] :sort - Sort order for options. Valid values are [:relevance, :created, :updated, :hotness, :ncomments]. Default is :relevance :fields - List of fields to return in the result. Valid values are: article_id, site_id, domain_id, title, published, updated, author, url, ncomment, nuser, annotation, nlp, hotness, hottest_value, hottest_time, peak, peak_value, peak_time, comments:5, users:5, comment_state, hit_field, dispurl, relevancy :max - Maximum number of fields to return :since - [DateTime] Minimum date of results to return :until - [DateTime] Maximum date of results to return :page - Page of results to fetch. Default 1.

Returns [Array<Conversation>] An array of matching conversations Raises [APIException] when response is not valid



31
32
33
34
# File 'lib/livefyre/site.rb', line 31

def search_conversations(query, options = {})
  options[:sites] = [self]
  Domain.new(@client).search_conversations(query, options)
end

#set_postback_url(url) ⇒ Object

Public: Set the postback URL for actions on this site

See: https://github.com/Livefyre/livefyre-docs/wiki/Accessing-Site-Comment-Data

url - [String] URL to use as the postback URL for actions

Returns [Bool] true on success Raises: [APIException] when response is not valid



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

#to_sObject

Internal: Returns a cleaner string representation of this object

Returns [String] representation of this class



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=#{options.inspect}>"
end