Class: Livefyre::Domain

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

Overview

Public: Proxy for a Livefyre domain resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ Domain

Returns a new instance of Domain.



6
7
8
# File 'lib/livefyre/domain.rb', line 6

def initialize(client = nil)
  @client = client || Livefyre.client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/livefyre/domain.rb', line 4

def client
  @client
end

Instance Method Details

#add_admin(user) ⇒ Object

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

user - [String, User, Integer] User or user ID to add as an admin

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



172
173
174
175
176
177
178
179
180
# File 'lib/livefyre/domain.rb', line 172

def add_admin(user)
  user = User.get_user(user, client)
  response = client.post "/admins/?actor_token=#{CGI.escape user.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 domain

user - [String, User, Integer] User or user ID to add as an owner

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



125
126
127
128
129
130
131
132
133
# File 'lib/livefyre/domain.rb', line 125

def add_owner(user)
  user = User.get_user(user, client)
  response = client.post "/owners/?actor_token=#{CGI.escape client.system_token}", {:jid => user.jid}
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

#add_user(profile) ⇒ Object

Public: Push a user profile to this domain

profile - [Hash] Hash of user data to publish per the Livefyre profile schema

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



80
81
82
83
84
85
86
87
88
# File 'lib/livefyre/domain.rb', line 80

def add_user(profile)
  raise "Invalid ID" if profile["id"].nil?
  response = client.post "/profiles/?actor_token=#{CGI.escape client.system_token}&id=#{CGI.escape profile["id"]}", profile
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

#adminsObject

Public: Retrieve a list of owners associated with this domain

Returns [Array<User>] An array of users Raises [APIException] when response is not valid



155
156
157
158
159
160
161
162
163
164
# File 'lib/livefyre/domain.rb', line 155

def admins
  response = client.get "/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

#create_site(url) ⇒ Object

Public: Create a new site on this domain

Returns [Site] A new site. Raises [APIException] when response is not valid



94
95
96
97
98
99
100
101
102
# File 'lib/livefyre/domain.rb', line 94

def create_site(url)
  response = client.post "/sites/?actor_token=#{CGI.escape client.system_token}&url=#{CGI.escape url}"
  if response.success?
    opts = JSON.parse response.body
    Site.new(opts["id"], client, opts)
  else
    raise APIException.new(response.body)
  end
end

#ownersObject

Public: Retrieve a list of owners associated with this domain

Returns [Array<User>] An array of users Raises [APIException] when response is not valid



108
109
110
111
112
113
114
115
116
117
# File 'lib/livefyre/domain.rb', line 108

def owners
  response = client.get "/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

#remove_admin(user) ⇒ Object

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

user - [String, User, Integer] User or user ID to remove as an admin

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



188
189
190
191
192
193
194
195
196
# File 'lib/livefyre/domain.rb', line 188

def remove_admin(user)
  user = User.get_user(user, client)
  response = client.delete "/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 domain

user - [String, User, Integer] User or user ID to remove as an owner

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



141
142
143
144
145
146
147
148
149
# File 'lib/livefyre/domain.rb', line 141

def remove_owner(user)
  user = User.get_user(user, client)
  response = client.delete "/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 :sites - Array of Sites or site IDs to limit results to. Maximum 5 sites. :page - Page of results to fetch. Default 1.

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/livefyre/domain.rb', line 40

def search_conversations(query, options = {})
  query = {}
  query[:return_fields] = options[:fields] if options[:fields]
  query[:fields]   = options[:fields] || [:article, :title, :body]
  query[:order]    = options[:sort] || "relevance"
  query[:max]      = options[:max].to_i if options[:max]
  query[:since]    = options[:since].utc.iso8601 if options[:since]
  query[:until]    = options[:until].utc.iso8601 if options[:until]
  query[:sites]    = options[:sites].map {|s| s.is_a?(Site) ? s.id : s.to_i }
  query[:cursor]   = (query[:max] || 10).to_i * options[:page].to_i if options[:page]
  query[:apitoken] = client.system_token ## TODO: I expect this is wrong; this param seems to expect the old-style API token.
  response = client.search.get "/api/v1.1/public/search/convs/", query
  if response.success?
    JSON.parse(response.body)
  else
    raise APIException.new(response.body)
  end
end

#set_pull_url(url) ⇒ Object

Public: Sets the profile pull URL for the entire network.

url - A URL template that includes the string “{id}” in it somewhere

Returns [Bool] true on success Raises APIException if the request failed



204
205
206
207
208
209
210
211
# File 'lib/livefyre/domain.rb', line 204

def set_pull_url(url)
  result = client.post "/", {:pull_profile_url => url, :actor_token => client.system_token}
  if result.success?
    return true
  else
    raise APIException.new(result.body)
  end
end

#sitesObject

Public: Get a list of sites for this domain

Returns [Array<Site>] An array of sites Raises [APIException] when response is not valid



14
15
16
17
18
19
20
21
22
23
# File 'lib/livefyre/domain.rb', line 14

def sites
  response = client.get "/sites/?actor_token=#{CGI.escape client.system_token}"
  if response.success?
    JSON.parse(response.body).map do |site|
      Site.new(site["id"], client, site)
    end
  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



216
217
218
# File 'lib/livefyre/domain.rb', line 216

def to_s
  "#<#{self.class.name}:0x#{object_id.to_s(16).rjust(14, "0")} host='#{client.host}'>"
end

#usersObject

Public: Get a list of users on this domain

Returns [Array<User>] An array of users Raises [APIException] when response is not valid



63
64
65
66
67
68
69
70
71
72
# File 'lib/livefyre/domain.rb', line 63

def users
  response = client.get "/profiles/?actor_token=#{CGI.escape client.system_token}"
  if response.success?
    JSON.parse(response.body).map do |site|
      User.new(site["id"], client, site["display_name"])
    end
  else
    raise APIException.new(response.body)
  end
end