Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Slack #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/omnicontacts/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module OmniContacts
module Importer

autoload :Gmail, "omnicontacts/importer/gmail"
autoload :Slack, "omnicontacts/importer/slack"
autoload :Yahoo, "omnicontacts/importer/yahoo"
autoload :Hotmail, "omnicontacts/importer/hotmail"
autoload :Outlook, "omnicontacts/importer/outlook"
Expand Down
58 changes: 58 additions & 0 deletions lib/omnicontacts/importer/slack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "omnicontacts/parse_utils"
require "omnicontacts/middleware/oauth2"

module OmniContacts
module Importer
class Slack < Middleware::OAuth2
include ParseUtils

attr_reader :auth_host, :authorize_path, :auth_token_path, :scope

def initialize *args
super *args
@auth_host = "slack.com"
@authorize_path = "/oauth/authorize"
@auth_token_path = "/api/oauth.access"
@scope = (args[3] && args[3][:scope]) || "users:read users:read.email"
@contacts_host = "slack.com"
@contacts_path = "/api/users.list"
@max_results = (args[3] && args[3][:max_results]) || 500
end

def fetch_contacts_using_access_token access_token, token_type
contacts_response = https_get(@contacts_host, @contacts_path, contacts_req_params(access_token))
contacts_from_response(contacts_response)
end

private

def contacts_req_params access_token
{'token' => access_token}
end

def contacts_from_response(response_as_json)
response = JSON.parse(response_as_json)
contacts = []
response["members"].each do |member|
contacts << {
:id => member["id"],
:profile_picture => member["profile"]["image_original"],
:first_name => member["profile"]["first_name"],
:last_name => member["profile"]["last_name"],
:name => member["profile"]["real_name"],
:email => member["profile"]["email"],
:emails => [
{
:name => member["profile"]["real_name"],
:email => member["profile"]["email"]
}
]
}
end

contacts
end

end
end
end
47 changes: 47 additions & 0 deletions spec/omnicontacts/importer/slack_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "spec_helper"
require "omnicontacts/importer/slack"

describe OmniContacts::Importer::Slack do
let(:slack) { OmniContacts::Importer::Slack.new({}, "client_id", "client_secret") }

let(:response_as_json) {
'{
"ok": true,
"members": [
{
"id": "SFSDFSDFS",
"profile":
{
"real_name": "John Smith",
"image_original": "https://avatars.slack-edge.com/2017-11-30/sdfswersdfsfsdfsdfsfasdf.png",
"email": "[email protected]",
"first_name": "John",
"last_name": "Smith"
}
}
]
}'
}

describe "fetch_contacts_using_access_token" do
let(:token) { "token" }
let(:token_type) { "token_type" }

before(:each) do
slack.instance_variable_set(:@env, {})
end

it "should correctly parse id, name, email, profile picture" do
slack.should_receive(:https_get).and_return(response_as_json)
result = slack.fetch_contacts_using_access_token token, token_type

result.first[:id].should eq('SFSDFSDFS')
result.first[:first_name].should eq('John')
result.first[:last_name].should eq('Smith')
result.first[:name].should eq("John Smith")
result.first[:email].should eq("[email protected]")
result.first[:profile_picture].should eq("https://avatars.slack-edge.com/2017-11-30/sdfswersdfsfsdfsdfsfasdf.png")
end

end
end