-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |