Class: Rightnow::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rightnow/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, opts = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rightnow/client.rb', line 13

def initialize host, opts = {}
  @host = host
  @api_key = opts[:api_key]
  @secret_key = opts[:secret_key]
  @user = opts[:user] || '[email protected]'
  @version = opts[:version] || '2010-05-15'
  @debug = opts[:debug]

  @conn = Faraday.new(:url => host) do |faraday|
    faraday.response :logger if @debug
    faraday.adapter  :typhoeus
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



11
12
13
# File 'lib/rightnow/client.rb', line 11

def api_key
  @api_key
end

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/rightnow/client.rb', line 11

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/rightnow/client.rb', line 11

def host
  @host
end

#secret_keyObject

Returns the value of attribute secret_key.



11
12
13
# File 'lib/rightnow/client.rb', line 11

def secret_key
  @secret_key
end

#userObject

Returns the value of attribute user.



11
12
13
# File 'lib/rightnow/client.rb', line 11

def user
  @user
end

#versionObject

Returns the value of attribute version.



11
12
13
# File 'lib/rightnow/client.rb', line 11

def version
  @version
end

Instance Method Details

#comment_add(post, body, opts = {}) ⇒ Object

Add a comment to a post.

post

An instance of Rightnow::Models::Post or a post hash (String)

body

The body of the comment (String)

returns

The instance of the newly created Rightnow::Comment

example

comment_add “fa8e6cc713”, “1”, :as => ‘[email protected]’+

Raises:



149
150
151
152
153
154
# File 'lib/rightnow/client.rb', line 149

def comment_add post, body, opts = {}
  hash = post.is_a?(Models::Post) ? post.hash : post
  results = request 'CommentAdd', opts.merge('postHash' => hash, 'payload' => comment_xml_payload(body).to_s, :verb => :post)
  raise Rightnow::Error.new("Missing `comment` key in CommentAdd response: #{results.inspect}") if not results['comment']
  Rightnow::Models::Comment.new results['comment'].underscore
end

#comment_delete(comment, opts = {}) ⇒ Object

Delete a comment.

comment

The id of the comment (Integer)

example

comment_delete 777



185
186
187
# File 'lib/rightnow/client.rb', line 185

def comment_delete comment, opts = {}
  request 'CommentDelete', opts.merge('commentId' => comment)
end

#comment_list(post, opts = {}) ⇒ Object

Retrieve comment list for a post.

post

An instance of Rightnow::Models::Post or a post hash (String)

returns

An array of Rightnow::Comment

example

comment_list “fa8e6cc713”

Raises:



128
129
130
131
132
133
# File 'lib/rightnow/client.rb', line 128

def comment_list post, opts = {}
  hash = post.is_a?(Models::Post) ? post.hash : post
  results = request 'CommentList', opts.merge('postHash' => hash)
  raise Rightnow::Error.new("Missing `comments` key in CommentList response: #{results.inspect}") if not results['comments']
  results.underscore['comments'].map { |r| Rightnow::Models::Comment.new(r) }
end

#comment_update(comment, body, opts = {}) ⇒ Object

Edit a comment.

comment

An instance of Rightnow::Models::Comment or a comment id (Integer)

comment

The updated body of the comment (String)

returns

The instance of the updated Rightnow::Comment

example

comment_update 94224, “1”, :as => ‘[email protected]’+

Raises:



170
171
172
173
174
175
# File 'lib/rightnow/client.rb', line 170

def comment_update comment, body, opts = {}
  id = comment.is_a?(Models::Comment) ? comment.id : comment
  results = request 'CommentUpdate', opts.merge('commentId' => id, 'payload' => comment_xml_payload(body, :for => :update).to_s, :verb => :post)
  raise Rightnow::Error.new("Missing `comment` key in CommentUpdate response: #{results.inspect}") if not results['comment']
  Rightnow::Models::Comment.new results['comment'].underscore
end

#post_get(posts) ⇒ Object

Retrieve full details for one or more posts. Run multiple queries in parallel.

posts

Either a single element or an array of Rightnow::Models::Post or post hash

returns

A single element or an array of Rightnow::Models::Post depending on the argument (single value or array)

example

post_get [“fa8e6cc713”, “fa8e6cb714”]



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rightnow/client.rb', line 60

def post_get posts
  responses = nil
  @conn.in_parallel do
    responses = [posts].flatten.map do |post|
      hash = post.is_a?(Models::Post) ? post.hash : post
      @conn.get 'api/endpoint', signed_params('PostGet', 'postHash' => hash)
    end
  end
  result = responses.zip([posts].flatten).map do |res, post|
    data = parse(res).underscore['post']
    if post.is_a? Models::Post
      post.attributes = data
      post
    elsif data.is_a? Hash
      Rightnow::Models::Post.new(data.merge(:hash => post))
    else
      nil
    end
  end
  posts.is_a?(Array) ? result : result.first
end

#request(action, opts = {}) ⇒ Object



189
190
191
192
193
194
# File 'lib/rightnow/client.rb', line 189

def request action, opts = {}
  verb = opts.delete(:verb) || :get
  response = @conn.send(verb, 'api/endpoint', signed_params(action, opts))
  puts response.body if @debug
  parse response
end

#search(opts = {}) ⇒ Object

Send a search query, returning an array of Rightnow::Models::Post results are limited to a few fields

opts

A hash of options accepted by Rightnow’s Search method

returns

An array of Rightnow::Models::Post

example:

+search :term => 'white', :sort => 'az', :limit => 50, :page => 1+


39
40
41
42
43
44
45
# File 'lib/rightnow/client.rb', line 39

def search opts = {}
  opts[:limit] ||= 20
  opts[:objects] ||= 'Posts'
  opts[:start] ||= (opts.delete(:page) - 1) * opts[:limit] + 1 if opts[:page]
  results = request 'Search', opts
  results.map {|r| Rightnow::Models::Post.new(r.underscore) }
end

#user_get(users) ⇒ Object

Retrieve full details for one or more users. Run multiple queries in parallel.

users

Either a single element or an array of Rightnow::Models::User or user hash

returns

A single element or an array of Rightnow::Models::User depending on the argument (single value or array)

example

user_get [“fa8e6cc713”, “fa8e6cb714”]



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rightnow/client.rb', line 95

def user_get users
  responses = nil
  @conn.in_parallel do
    responses = [users].flatten.map do |user|
      hash = user.is_a?(Models::User) ? user.hash : user
      @conn.get 'api/endpoint', signed_params('UserGet', 'UserHash' => hash)
    end
  end
  result = responses.zip([users].flatten).map do |res, user|
    data = parse(res).underscore['user']
    if user.is_a? Models::User
      user.attributes = data
      user
    elsif data.is_a? Hash
      Rightnow::Models::User.new(data.merge(:hash => user))
    else
      nil
      end
  end
  users.is_a?(Array) ? result : result.first
end