Skip to content

Commit

Permalink
Add support for Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
melayev committed Dec 18, 2019
1 parent 105b40f commit dd3e568
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
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": "johnsmith@domain.tld",
"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("johnsmith@domain.tld")
result.first[:profile_picture].should eq("https://avatars.slack-edge.com/2017-11-30/sdfswersdfsfsdfsdfsfasdf.png")
end

end
end

0 comments on commit dd3e568

Please sign in to comment.