Skip to content
Browse files

Merge branch 'feature/messaging_api' into dev

  • Loading branch information...
2 parents 60abe59 + a6c14ac commit 28f081c32e9da2249b2272c08515c0836d1a48c2 @jsboulanger committed
View
1 lib/linked_in/api.rb
@@ -2,5 +2,6 @@ module LinkedIn
module Api
autoload :QueryMethods, "linked_in/api/query_methods"
autoload :UpdateMethods, "linked_in/api/update_methods"
+ autoload :Communications, "linked_in/api/communications"
end
end
View
33 lib/linked_in/api/communications.rb
@@ -0,0 +1,33 @@
+module LinkedIn
+ module Api
+ module Communications
+
+ # Send a message from the authenticated user to the recipients
+ #
+ # @param recipient_uids [Enumerable, String] The list of uids of all the recipients or one uid if there is only one recipient.
+ # @param subject [String] The subject of the message.
+ # @param body [String] The body of the message. Cannot contain HTML.
+ def send_message(recipient_uids, subject, body)
+ path = "/people/~/mailbox"
+ recipients_uids = [recipient_uids] if recipients_uids.is_a?(String)
+
+ rcpts = recipient_uids.map do |uid|
+ {
+ :person => {
+ :_path => "/people/#{uid}"
+ }
+ }
+ end
+
+ params = {
+ :subject => subject,
+ :body => body,
+ :recipients => { :values => rcpts }
+ }
+
+ result = post(path, ::MultiJson.encode(params), "Content-Type" => "application/json")
+ result.code
+ end
+ end
+ end
+end
View
1 lib/linked_in/client.rb
@@ -7,6 +7,7 @@ class Client
include Helpers::Authorization
include Api::QueryMethods
include Api::UpdateMethods
+ include Api::Communications
include Search
attr_reader :consumer_token, :consumer_secret, :consumer_options
View
36 spec/linked_in/api/communications_spec.rb
@@ -0,0 +1,36 @@
+require 'helper'
+
+describe LinkedIn::Api::Communications do
+ before do
+ @client = LinkedIn::Client.new('token', 'secret')
+ @consumer = OAuth::Consumer.new('token', 'secret', { :site => 'https://api.linkedin.com' })
+ @client.stub(:consumer).and_return(@consumer)
+ @client.authorize_from_access('atoken', 'asecret')
+ end
+
+ describe "#send_message" do
+ before do
+ stub_request(:post, "https://api.linkedin.com/v1/people/~/mailbox").to_return(:body => "{}", :status => 201)
+ @result = @client.send_message("1234abcd", "My Subject", "My body")
+ end
+
+ it "should get the correct resource" do
+ a_request(:post, "https://api.linkedin.com/v1/people/~/mailbox").with(
+ :headers => { "Content-Type" => "application/json" }
+ ).should have_been_made
+ end
+
+ it "should pass the parameters to the request" do
+ a_request(:post, "https://api.linkedin.com/v1/people/~/mailbox").with do |req|
+ preq = ::MultiJson.decode(req.body)
+ preq["subject"] == "My Subject" &&
+ preq["body"] == "My body" &&
+ preq["recipients"] == { "values" => ["person" => { "_path" => "/people/1234abcd" }] }
+ end.should have_been_made
+ end
+
+ it "should return 201 created when successful" do
+ @result.should == "201"
+ end
+ end
+end

0 comments on commit 28f081c

Please sign in to comment.
Something went wrong with that request. Please try again.