Class: Livefyre::Site

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

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, options = {})
  @id = id
  @client = client || Livefyre.client
  @options = options
  @secret = options["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
  @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

Parameters:

  • params

    Hash of request parameters

  • secret

    Site key to validate signature with

  • time_window (defaults to: 300)

    Enforce that the sig_created is within time_window seconds of the current time. Slush is given to account for system time drift. Pass nil or false to disable timestamp checking.

Returns:

  • (Bool)

    Returns

Raises:



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

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



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

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



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

Returns:



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

Parameters:

  • since_id (Integer)

    If provided, will return feed items after the given comment.

Returns:

  • (Array<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

- (Conversation) create_conversation(article_id, title, link, tags = nil)

Create a conversation collection on this site

Returns:



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

- (Array<Activity>) feed(since_id = nil)

Fetches a feed of the site's latest activity.

Parameters:

  • since_id (Integer) (defaults to: nil)

    If provided, will return feed items after the given feed item.

Returns:

  • (Array<Activity>)

    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

- (Array<Livefyre::User>) owners

Retrieve a list of owners associated with this site

Returns:

Raises:

  • 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

- (Hash) properties(reload = false)

Get a list of properties for this site

Parameters:

  • reload (defaults to: false)

    Force a reload when set

Returns:

  • (Hash)

    Returns Hash Site properties

Raises:



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

- (Object) reload

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

- (Bool) remove_admin(user)

Removes a user from the list of admins for this site

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



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

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



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

Parameters:

  • query

    string to query for

  • options (Hash) (defaults to: {})

    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>)

    Returns Array<Conversation> An array of matching conversations

Raises:



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

- (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

Parameters:

  • url (String)

    URL to use as the postback URL for actions

Returns:

  • (Bool)

    Returns Bool true on success



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

Returns:

  • (String)

    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