From dd3e5689552cc5ed94d6f6939d3c54cae192489d Mon Sep 17 00:00:00 2001 From: Parahat Melayev Date: Wed, 18 Dec 2019 18:05:04 +0000 Subject: [PATCH] Add support for Slack --- lib/omnicontacts/importer.rb | 1 + lib/omnicontacts/importer/slack.rb | 58 ++++++++++++++++++++++++ spec/omnicontacts/importer/slack_spec.rb | 47 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 lib/omnicontacts/importer/slack.rb create mode 100644 spec/omnicontacts/importer/slack_spec.rb diff --git a/lib/omnicontacts/importer.rb b/lib/omnicontacts/importer.rb index 8cce325..2723e78 100644 --- a/lib/omnicontacts/importer.rb +++ b/lib/omnicontacts/importer.rb @@ -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" diff --git a/lib/omnicontacts/importer/slack.rb b/lib/omnicontacts/importer/slack.rb new file mode 100644 index 0000000..a604a8c --- /dev/null +++ b/lib/omnicontacts/importer/slack.rb @@ -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 diff --git a/spec/omnicontacts/importer/slack_spec.rb b/spec/omnicontacts/importer/slack_spec.rb new file mode 100644 index 0000000..3ae4f60 --- /dev/null +++ b/spec/omnicontacts/importer/slack_spec.rb @@ -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