Class: Platforms::Yammer::Client

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

Overview

A REST client for Yammer

Assumes all of the OAuth2 authentication has been done previously, and a valid token is available.

Uses Faraday to conduct HTTP requests and receive responses, with OAuth2 FaradayMiddleWare.

Author:

  • Benjamin Elias

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, errors = true) {|f| ... } ⇒ Client

Initialize class with a token

Construct a new Faraday #connection which uses the token provided. Uses OAuth2 with Bearer authentication.

Requests are sent with JSON encoding by default, and responses are parsed as JSON if the Content-type header of the response is set accordingly.

To change the default base URL, use the Platforms::Yammer::Configuration settings in an initializer.

Connection configuration can be performed using the yield block, or can be done through the #connection variable as shown in the examples.

To override default connection headers with request-specific headers, use the headers parameter in any of the request functions.

Errors can be turned off if the application explicitly checks the HTTP status codes. By default these are left on.

Examples:

Configure Faraday at initialization

client = Client.new(token) do |f|
  f.use(Faraday::Response::RaiseError)
end

Configure Faraday after initialization

client = Client.new(token)
client.connection.use(Faraday::Response::RaiseError)

Parameters:

  • token (String)

    the token to use

  • errors (Boolean) (defaults to: true)

    whether to raise errors for non-2XX responses

Yield Parameters:

  • f (::Faraday)

    the new Faraday connection

Since:

  • 0.1.0



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/platforms/yammer/client.rb', line 57

def initialize token, errors=true, &block
  api_base = Platforms::Yammer.configuration.api_base

  @connection = ::Faraday.new api_base do |faraday|
    faraday.request :oauth2, token, token_type: 'bearer'
    faraday.request :json
    faraday.response :json, :content_type => /\bjson$/
    faraday.use Faraday::Response::RaiseError if errors
    block.call(faraday) if block_given?
  end
end

Instance Attribute Details

#connectionFaraday::Connection (readonly)

Giving access to the underlying Faraday connection allows setting headers and other more detailed configuration.

Returns:

  • (Faraday::Connection)

    the Faraday connection

Since:

  • 0.1.0



22
23
24
# File 'lib/platforms/yammer/client.rb', line 22

def connection
  @connection
end

Instance Method Details

#group_membershipsApi::GroupMemberships

Examples:

Add a User to a Group

client.group_memberships.post

Returns:

Since:

  • 0.1.0



124
125
126
# File 'lib/platforms/yammer/client.rb', line 124

def group_memberships
  Api::GroupMemberships.new @connection
end

#groupsApi::Groups

Create new Api::Groups using #connection

Examples:

Get groups for a user

client.groups.for_user

Returns:

Since:

  • 0.1.0



132
133
134
# File 'lib/platforms/yammer/client.rb', line 132

def groups
  Api::Groups.new @connection
end

#invitationsApi::Invitations

Create new Api::Invitations using #connection

Examples:

Send an invitation

client.invitations.post

Returns:

Since:

  • 0.1.0



180
181
182
# File 'lib/platforms/yammer/client.rb', line 180

def invitations
  Api::Invitations.new @connection
end

#messagesApi::Messages

Create new Api::Messages using #connection

Examples:

Get messages

client.messages.get

Returns:

Since:

  • 0.1.0



84
85
86
# File 'lib/platforms/yammer/client.rb', line 84

def messages
  Api::Messages.new @connection
end

#networksApi::Networks

Create new Api::Networks using #connection

Examples:

Get current Network

client.networks.current

Returns:

Since:

  • 0.1.0



196
197
198
# File 'lib/platforms/yammer/client.rb', line 196

def networks
  Api::Networks.new @connection
end

#oauthApi::Oauth

Create new Api::Oauth using #connection

Examples:

Get OAuth2 tokens

client.oauth.tokens

Returns:

See Also:

Since:

  • 0.1.0



222
223
224
# File 'lib/platforms/yammer/client.rb', line 222

def oauth
  Api::Oauth.new @connection
end

#open_graph_objectsApi::OpenGraphObjets

Examples:

Get OG objects

client.open_graph_objects.get

Returns:

  • (Api::OpenGraphObjets)

    the API class

Since:

  • 0.1.0



204
205
206
# File 'lib/platforms/yammer/client.rb', line 204

def open_graph_objects
  Api::OpenGraphObjects.new @connection
end

#pending_attachmentsApi::PendingAttachments

Examples:

Add pending attachment

client.pending_attachments.post

Returns:

Since:

  • 0.1.0



92
93
94
# File 'lib/platforms/yammer/client.rb', line 92

def pending_attachments
  Api::PendingAttachments.new @connection
end

#relationshipsApi::Relationships

Create new Api::Relationships using #connection

Examples:

Get user relationships

client.relationships.get

Returns:

Since:

  • 0.1.0



148
149
150
# File 'lib/platforms/yammer/client.rb', line 148

def relationships
  Api::Relationships.new @connection
end

#request(method, endpoint, options = {}, headers = {}) ⇒ Faraday::Response

Generic request, can be used for new, updated, or undocumented endpoints.

Parameters:

  • method (Symbol)

    The HTTP method (get, post, put, delete)

  • endpoint (String)

    The API endpoint

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

    Options for the request

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

    Headers to include with the request

Returns:

  • (Faraday::Response)

    the Faraday response

Since:

  • 0.1.0



76
77
78
# File 'lib/platforms/yammer/client.rb', line 76

def request method, endpoint, options={}, headers={}
  connection.send(method, endpoint, options, headers)
end

#searchApi::Search

Create new Api::Search using #connection

Examples:

Search Yammer

client.search.get

Returns:

Since:

  • 0.1.0



188
189
190
# File 'lib/platforms/yammer/client.rb', line 188

def search
  Api::Search.new @connection
end

#streamsApi::Streams

Create new Api::Streams using #connection

Examples:

Get notifications

client.streams.notifications

Returns:

Since:

  • 0.1.0



156
157
158
# File 'lib/platforms/yammer/client.rb', line 156

def streams
  Api::Streams.new @connection
end

#subscriptionsApi::Subscriptions

Create new Api::Subscriptions using #connection

Examples:

Get subscriptions to a user

client.subscriptions.to_user

Returns:

Since:

  • 0.1.0



172
173
174
# File 'lib/platforms/yammer/client.rb', line 172

def subscriptions
  Api::Subscriptions.new @connection
end

#suggestionsApi::Suggestions

Create new Api::Suggestions using #connection

Examples:

Get suggestions

client.suggestions.get

Returns:

Since:

  • 0.1.0



164
165
166
# File 'lib/platforms/yammer/client.rb', line 164

def suggestions
  Api::Suggestions.new @connection
end

#supervisor_modeApi::SupervisorMode

Create new Api::SupervisorMode using #connection

Examples:

Toggle supervisor mode

client.supervisor_mode.toggle

Returns:

See Also:

Since:

  • 0.1.0



213
214
215
# File 'lib/platforms/yammer/client.rb', line 213

def supervisor_mode
  Api::SupervisorMode.new @connection
end

#threadsApi::Threads

Create new Api::Threads using #connection

Examples:

Get threads

client.threads.get

Returns:

Since:

  • 0.1.0



108
109
110
# File 'lib/platforms/yammer/client.rb', line 108

def threads
  Api::Threads.new @connection
end

#topicsApi::Topics

Create new Api::Topics using #connection

Examples:

Get topics

client.topics.get

Returns:

Since:

  • 0.1.0



116
117
118
# File 'lib/platforms/yammer/client.rb', line 116

def topics
  Api::Topics.new @connection
end

#uploaded_filesApi::UploadedFiles

Create new Api::UploadedFiles using #connection

Examples:

Delete an uploaded file

client.uploaded_files.delete

Returns:

Since:

  • 0.1.0



100
101
102
# File 'lib/platforms/yammer/client.rb', line 100

def uploaded_files
  Api::UploadedFiles.new @connection
end

#usersApi::Users

Create new Api::Users using #connection

Examples:

Get users

client.users.get_users

Returns:

Since:

  • 0.1.0



140
141
142
# File 'lib/platforms/yammer/client.rb', line 140

def users
  Api::Users.new @connection
end