omnicontacts 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/Gemfile.lock +1 -1
- data/lib/omnicontacts.rb +1 -3
- data/lib/omnicontacts/authorization/oauth1.rb +6 -6
- data/lib/omnicontacts/authorization/oauth2.rb +9 -9
- data/lib/omnicontacts/builder.rb +1 -1
- data/lib/omnicontacts/http_utils.rb +9 -9
- data/lib/omnicontacts/importer/gmail.rb +5 -5
- data/lib/omnicontacts/importer/hotmail.rb +3 -3
- data/lib/omnicontacts/importer/yahoo.rb +4 -4
- data/lib/omnicontacts/middleware/base_oauth.rb +4 -4
- data/lib/omnicontacts/middleware/oauth1.rb +2 -2
- data/lib/omnicontacts/middleware/oauth2.rb +1 -1
- data/spec/omnicontacts/authorization/oauth1_spec.rb +17 -17
- data/spec/omnicontacts/authorization/oauth2_spec.rb +21 -21
- data/spec/omnicontacts/http_utils_spec.rb +5 -5
- data/spec/omnicontacts/importer/gmail_spec.rb +6 -6
- data/spec/omnicontacts/importer/hotmail_spec.rb +19 -19
- data/spec/omnicontacts/importer/yahoo_spec.rb +2 -2
- data/spec/omnicontacts/middleware/oauth1_spec.rb +6 -5
- data/spec/omnicontacts/middleware/oauth2_spec.rb +6 -6
- metadata +2 -2
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/omnicontacts.rb
CHANGED
@@ -14,7 +14,7 @@ require "base64"
|
|
14
14
|
# * access_token_path -> the path to query in order to obtain the access token
|
15
15
|
module OmniContacts
|
16
16
|
module Authorization
|
17
|
-
module OAuth1
|
17
|
+
module OAuth1
|
18
18
|
include HTTPUtils
|
19
19
|
|
20
20
|
OAUTH_VERSION = "1.0"
|
@@ -41,11 +41,11 @@ module OmniContacts
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def random_string
|
44
|
-
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
44
|
+
(0...50).map { ('a'..'z').to_a[rand(26)] }.join
|
45
45
|
end
|
46
46
|
|
47
47
|
def timestamp
|
48
|
-
Time.now.to_i.to_s
|
48
|
+
Time.now.to_i.to_s
|
49
49
|
end
|
50
50
|
|
51
51
|
def values_from_query_string query_string, keys_to_extract
|
@@ -71,11 +71,11 @@ module OmniContacts
|
|
71
71
|
# The result comprises the access token, the access token secret and a list of additional fields extracted from the server's response.
|
72
72
|
# The list of additional fields to extract is specified as last parameter
|
73
73
|
def fetch_access_token auth_token, auth_token_secret, auth_verifier, additional_fields_to_extract = []
|
74
|
-
access_token_resp = https_post(auth_host, access_token_path, access_token_req_params(auth_token, auth_token_secret, auth_verifier))
|
75
|
-
values_from_query_string(access_token_resp, (
|
74
|
+
access_token_resp = https_post(auth_host, access_token_path, access_token_req_params(auth_token, auth_token_secret, auth_verifier))
|
75
|
+
values_from_query_string(access_token_resp, (["oauth_token", "oauth_token_secret"] + additional_fields_to_extract))
|
76
76
|
end
|
77
77
|
|
78
|
-
private
|
78
|
+
private
|
79
79
|
|
80
80
|
def access_token_req_params auth_token, auth_token_secret, auth_verifier
|
81
81
|
{
|
@@ -27,13 +27,13 @@ module OmniContacts
|
|
27
27
|
|
28
28
|
def authorize_url_params
|
29
29
|
to_query_string({
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
:client_id => client_id,
|
31
|
+
:scope => encode(scope),
|
32
|
+
:response_type => "code",
|
33
|
+
:access_type => "offline",
|
34
|
+
:approval_prompt => "force",
|
35
|
+
:redirect_uri => encode(redirect_uri)
|
36
|
+
})
|
37
37
|
end
|
38
38
|
|
39
39
|
public
|
@@ -58,7 +58,7 @@ module OmniContacts
|
|
58
58
|
def access_token_from_response response
|
59
59
|
json = JSON.parse(response)
|
60
60
|
raise json["error"] if json["error"]
|
61
|
-
[
|
61
|
+
[json["access_token"], json["token_type"], json["refresh_token"]]
|
62
62
|
end
|
63
63
|
|
64
64
|
public
|
@@ -77,7 +77,7 @@ module OmniContacts
|
|
77
77
|
:refresh_token => refresh_token,
|
78
78
|
:grant_type => "refresh_token"
|
79
79
|
}
|
80
|
-
|
80
|
+
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
data/lib/omnicontacts/builder.rb
CHANGED
@@ -10,13 +10,13 @@ module OmniContacts
|
|
10
10
|
|
11
11
|
module_function
|
12
12
|
|
13
|
-
def query_string_to_map query_string
|
13
|
+
def query_string_to_map query_string
|
14
14
|
query_string.split('&').reduce({}) do |memo, key_value|
|
15
|
-
(key,value) = key_value.split('=')
|
15
|
+
(key, value) = key_value.split('=')
|
16
16
|
memo[key]= value
|
17
17
|
memo
|
18
|
-
end
|
19
|
-
end
|
18
|
+
end
|
19
|
+
end
|
20
20
|
|
21
21
|
def to_query_string map
|
22
22
|
map.collect do |key, value|
|
@@ -34,11 +34,11 @@ module OmniContacts
|
|
34
34
|
# If port is 80 the result is scheme://host
|
35
35
|
# According to Rack specification the HTTP_HOST variable is preferred over SERVER_NAME.
|
36
36
|
def host_url_from_rack_env env
|
37
|
-
port = (
|
37
|
+
port = ((env["SERVER_PORT"] == 80) && "") || ":#{env['SERVER_PORT']}"
|
38
38
|
host = (env["HTTP_HOST"]) || (env["SERVER_NAME"] + port)
|
39
39
|
scheme(env) + "://" + host
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def scheme env
|
43
43
|
if env['HTTPS'] == 'on'
|
44
44
|
'https'
|
@@ -65,8 +65,8 @@ module OmniContacts
|
|
65
65
|
|
66
66
|
# Executes an HTTP POST request over SSL
|
67
67
|
# It raises a RuntimeError if the response code is not equal to 200
|
68
|
-
def https_post host,path, params
|
69
|
-
https_connection host do |connection|
|
68
|
+
def https_post host, path, params
|
69
|
+
https_connection host do |connection|
|
70
70
|
connection.request_post(path, to_query_string(params))
|
71
71
|
end
|
72
72
|
end
|
@@ -74,7 +74,7 @@ module OmniContacts
|
|
74
74
|
# Executes an HTTP GET request over SSL
|
75
75
|
# It raises a RuntimeError if the response code is not equal to 200
|
76
76
|
def https_get host, path, params, headers =[]
|
77
|
-
https_connection host
|
77
|
+
https_connection host do |connection|
|
78
78
|
connection.request_get(path + "?" + to_query_string(params), headers)
|
79
79
|
end
|
80
80
|
end
|
@@ -18,18 +18,18 @@ module OmniContacts
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def fetch_contacts_using_access_token access_token, token_type
|
21
|
-
contacts_response = https_get(@contacts_host, @contacts_path, contacts_req_params, contacts_req_headers(access_token, token_type))
|
21
|
+
contacts_response = https_get(@contacts_host, @contacts_path, contacts_req_params, contacts_req_headers(access_token, token_type))
|
22
22
|
parse_contacts contacts_response
|
23
23
|
end
|
24
24
|
|
25
|
-
private
|
25
|
+
private
|
26
26
|
|
27
27
|
def contacts_req_params
|
28
|
-
{
|
28
|
+
{"max-results" => "100"}
|
29
29
|
end
|
30
30
|
|
31
31
|
def contacts_req_headers token, token_type
|
32
|
-
{"GData-Version" => "3.0",
|
32
|
+
{"GData-Version" => "3.0", "Authorization" => "#{token_type} #{token}"}
|
33
33
|
end
|
34
34
|
|
35
35
|
def parse_contacts contacts_as_xml
|
@@ -46,7 +46,7 @@ module OmniContacts
|
|
46
46
|
contacts << contact
|
47
47
|
end
|
48
48
|
end
|
49
|
-
contacts
|
49
|
+
contacts
|
50
50
|
end
|
51
51
|
|
52
52
|
end
|
@@ -18,14 +18,14 @@ module OmniContacts
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def fetch_contacts_using_access_token access_token, access_token_secret
|
21
|
-
contacts_response = https_get(@contacts_host, @contacts_path, :access_token =>access_token)
|
21
|
+
contacts_response = https_get(@contacts_host, @contacts_path, :access_token => access_token)
|
22
22
|
contacts_from_response contacts_response
|
23
23
|
end
|
24
24
|
|
25
25
|
private
|
26
26
|
|
27
27
|
def contacts_from_response contacts_as_json
|
28
|
-
json = JSON.parse(escape_windows_format(contacts_as_json))
|
28
|
+
json = JSON.parse(escape_windows_format(contacts_as_json))
|
29
29
|
result = []
|
30
30
|
json["data"].each do |contact|
|
31
31
|
result << {:email => contact["name"]} if valid_email? contact["name"]
|
@@ -34,7 +34,7 @@ module OmniContacts
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def escape_windows_format value
|
37
|
-
value.gsub(/[\r\s]/,'')
|
37
|
+
value.gsub(/[\r\s]/, '')
|
38
38
|
end
|
39
39
|
|
40
40
|
def valid_email? value
|
@@ -3,7 +3,7 @@ require "json"
|
|
3
3
|
|
4
4
|
module OmniContacts
|
5
5
|
module Importer
|
6
|
-
class Yahoo < Middleware::OAuth1
|
6
|
+
class Yahoo < Middleware::OAuth1
|
7
7
|
|
8
8
|
attr_reader :auth_host, :auth_token_path, :auth_path, :access_token_path
|
9
9
|
|
@@ -19,7 +19,7 @@ module OmniContacts
|
|
19
19
|
def fetch_contacts_from_token_and_verifier auth_token, auth_token_secret, auth_verifier
|
20
20
|
(access_token, access_token_secret, guid) = fetch_access_token(auth_token, auth_token_secret, auth_verifier, ["xoauth_yahoo_guid"])
|
21
21
|
contacts_path = "/v1/user/#{guid}/contacts"
|
22
|
-
contacts_response = http_get(@contacts_host, contacts_path, contacts_req_params(access_token, access_token_secret, contacts_path)
|
22
|
+
contacts_response = http_get(@contacts_host, contacts_path, contacts_req_params(access_token, access_token_secret, contacts_path))
|
23
23
|
contacts_from_response contacts_response
|
24
24
|
end
|
25
25
|
|
@@ -35,13 +35,13 @@ module OmniContacts
|
|
35
35
|
:oauth_token => access_token,
|
36
36
|
:oauth_version => OmniContacts::Authorization::OAuth1::OAUTH_VERSION,
|
37
37
|
:view => "compact"
|
38
|
-
}
|
38
|
+
}
|
39
39
|
contacts_url = "http://#{@contacts_host}#{contacts_path}"
|
40
40
|
params["oauth_signature"] = oauth_signature("GET", contacts_url, params, access_token_secret)
|
41
41
|
params
|
42
42
|
end
|
43
43
|
|
44
|
-
def contacts_from_response contacts_as_json
|
44
|
+
def contacts_from_response contacts_as_json
|
45
45
|
json = JSON.parse(contacts_as_json)
|
46
46
|
result = []
|
47
47
|
return result unless json["contacts"]["contact"]
|
@@ -49,13 +49,13 @@ module OmniContacts
|
|
49
49
|
private
|
50
50
|
|
51
51
|
def handle_initial_request
|
52
|
-
execute_and_rescue_exceptions do
|
52
|
+
execute_and_rescue_exceptions do
|
53
53
|
request_authorization_from_user
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
def handle_callback
|
58
|
-
execute_and_rescue_exceptions do
|
58
|
+
execute_and_rescue_exceptions do
|
59
59
|
@env["omnicontacts.contacts"] = fetch_contacts
|
60
60
|
@app.call(@env)
|
61
61
|
end
|
@@ -64,13 +64,13 @@ module OmniContacts
|
|
64
64
|
# This method rescues executes a block of code and
|
65
65
|
# rescue all exceptions. In case of an exception the
|
66
66
|
# user is redirected to the failure endpoint.
|
67
|
-
def execute_and_rescue_exceptions
|
67
|
+
def execute_and_rescue_exceptions
|
68
68
|
yield
|
69
69
|
rescue AuthorizationError => e
|
70
70
|
handle_error :not_authorized, e
|
71
71
|
rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
|
72
72
|
handle_error :timeout, e
|
73
|
-
rescue
|
73
|
+
rescue ::RuntimeError => e
|
74
74
|
handle_error :internal_error, e
|
75
75
|
end
|
76
76
|
|
@@ -35,11 +35,11 @@ module OmniContacts
|
|
35
35
|
(auth_token, auth_token_secret) = fetch_authorization_token
|
36
36
|
session[@token_prop_name] = auth_token
|
37
37
|
session[token_secret_prop_name(auth_token)] = auth_token_secret
|
38
|
-
redirect_to_authorization_site(auth_token)
|
38
|
+
redirect_to_authorization_site(auth_token)
|
39
39
|
end
|
40
40
|
|
41
41
|
def token_secret_prop_name oauth_token
|
42
|
-
"#{base_prop_name}.#{oauth_token}.oauth_token_secret"
|
42
|
+
"#{base_prop_name}.#{oauth_token}.oauth_token_secret"
|
43
43
|
end
|
44
44
|
|
45
45
|
def redirect_to_authorization_site auth_token
|
@@ -41,7 +41,7 @@ module OmniContacts
|
|
41
41
|
# If no authorization code is found in the query string an
|
42
42
|
# AuthoriazationError is raised.
|
43
43
|
def fetch_contacts
|
44
|
-
code =
|
44
|
+
code = query_string_to_map(@env["QUERY_STRING"])["code"]
|
45
45
|
if code
|
46
46
|
refresh_token = session[refresh_token_prop_name(code)]
|
47
47
|
(access_token, token_type, refresh_token) = if refresh_token
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "omnicontacts/authorization/oauth1"
|
3
3
|
|
4
|
-
describe OmniContacts::Authorization::OAuth1 do
|
4
|
+
describe OmniContacts::Authorization::OAuth1 do
|
5
5
|
|
6
|
-
before(:all) do
|
6
|
+
before(:all) do
|
7
7
|
OAuth1TestClass= Struct.new(:consumer_key, :consumer_secret, :auth_host, :auth_token_path, :auth_path, :access_token_path, :callback)
|
8
|
-
class OAuth1TestClass
|
8
|
+
class OAuth1TestClass
|
9
9
|
include OmniContacts::Authorization::OAuth1
|
10
10
|
end
|
11
11
|
end
|
@@ -14,9 +14,9 @@ describe OmniContacts::Authorization::OAuth1 do
|
|
14
14
|
OAuth1TestClass.new("consumer_key", "secret1", "auth_host", "auth_token_path", "auth_path", "access_token_path", "callback")
|
15
15
|
end
|
16
16
|
|
17
|
-
describe "fetch_authorization_token" do
|
17
|
+
describe "fetch_authorization_token" do
|
18
18
|
|
19
|
-
it "should request the token providing all mandatory parameters" do
|
19
|
+
it "should request the token providing all mandatory parameters" do
|
20
20
|
test_target.should_receive(:https_post) do |host, path, params|
|
21
21
|
host.should eq(test_target.auth_host)
|
22
22
|
path.should eq(test_target.auth_token_path)
|
@@ -32,25 +32,25 @@ describe OmniContacts::Authorization::OAuth1 do
|
|
32
32
|
test_target.fetch_authorization_token
|
33
33
|
end
|
34
34
|
|
35
|
-
it "should successfully parse the result" do
|
35
|
+
it "should successfully parse the result" do
|
36
36
|
test_target.should_receive(:https_post).and_return("oauth_token=token&oauth_token_secret=token_secret")
|
37
37
|
test_target.fetch_authorization_token.should eq(["token", "token_secret"])
|
38
38
|
end
|
39
39
|
|
40
|
-
it "should raise an error if request is invalid" do
|
40
|
+
it "should raise an error if request is invalid" do
|
41
41
|
test_target.should_receive(:https_post).and_return("invalid_request")
|
42
|
-
expect{test_target.fetch_authorization_token}.should raise_error
|
42
|
+
expect { test_target.fetch_authorization_token }.should raise_error
|
43
43
|
end
|
44
44
|
|
45
45
|
end
|
46
46
|
|
47
|
-
describe "authorization_url" do
|
48
|
-
subject{test_target.authorization_url("token")}
|
49
|
-
it{should eq("https://#{test_target.auth_host}#{test_target.auth_path}?oauth_token=token")}
|
47
|
+
describe "authorization_url" do
|
48
|
+
subject { test_target.authorization_url("token") }
|
49
|
+
it { should eq("https://#{test_target.auth_host}#{test_target.auth_path}?oauth_token=token") }
|
50
50
|
end
|
51
51
|
|
52
|
-
describe "fetch_access_token" do
|
53
|
-
it "should request the access token using all required parameters" do
|
52
|
+
describe "fetch_access_token" do
|
53
|
+
it "should request the access token using all required parameters" do
|
54
54
|
auth_token = "token"
|
55
55
|
auth_token_secret = "token_secret"
|
56
56
|
auth_verifier = "verifier"
|
@@ -69,14 +69,14 @@ describe OmniContacts::Authorization::OAuth1 do
|
|
69
69
|
test_target.fetch_access_token auth_token, auth_token_secret, auth_verifier, ["other_param"]
|
70
70
|
end
|
71
71
|
|
72
|
-
it "should successfully extract access_token and the other fields" do
|
72
|
+
it "should successfully extract access_token and the other fields" do
|
73
73
|
test_target.should_receive(:https_post).and_return("oauth_token=access_token&oauth_token_secret=access_token_secret&other_param=other_value")
|
74
|
-
test_target.fetch_access_token("token","token_scret","verified",["other_param"]).should eq(["access_token", "access_token_secret", "other_value"])
|
74
|
+
test_target.fetch_access_token("token", "token_scret", "verified", ["other_param"]).should eq(["access_token", "access_token_secret", "other_value"])
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
78
|
describe "oauth_signature" do
|
79
|
-
subject{ test_target.oauth_signature("GET", "http://social.yahooapis.com/v1/user", {:name => "diego", :surname => "castorina"}, "secret2")}
|
80
|
-
it{ should eq("ZqWoQISWcuz%2FSDnDxWihtsFDKwc%3D")}
|
79
|
+
subject { test_target.oauth_signature("GET", "http://social.yahooapis.com/v1/user", {:name => "diego", :surname => "castorina"}, "secret2") }
|
80
|
+
it { should eq("ZqWoQISWcuz%2FSDnDxWihtsFDKwc%3D") }
|
81
81
|
end
|
82
82
|
end
|
@@ -1,32 +1,32 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "omnicontacts/authorization/oauth2"
|
3
3
|
|
4
|
-
describe OmniContacts::Authorization::OAuth2 do
|
4
|
+
describe OmniContacts::Authorization::OAuth2 do
|
5
5
|
|
6
|
-
before(:all) do
|
7
|
-
OAuth2TestClass= Struct.new(:auth_host, :authorize_path, :client_id, :client_secret, :scope, :redirect_uri
|
8
|
-
class OAuth2TestClass
|
6
|
+
before(:all) do
|
7
|
+
OAuth2TestClass= Struct.new(:auth_host, :authorize_path, :client_id, :client_secret, :scope, :redirect_uri, :auth_token_path)
|
8
|
+
class OAuth2TestClass
|
9
9
|
include OmniContacts::Authorization::OAuth2
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
let(:test_target) do
|
13
|
+
let(:test_target) do
|
14
14
|
OAuth2TestClass.new("auth_host", "authorize_path", "client_id", "client_secret", "scope", "redirect_uri", "auth_token_path")
|
15
15
|
end
|
16
16
|
|
17
|
-
describe "authorization_url" do
|
17
|
+
describe "authorization_url" do
|
18
18
|
|
19
|
-
subject {test_target.authorization_url}
|
19
|
+
subject { test_target.authorization_url }
|
20
20
|
|
21
|
-
it {should include("https://#{test_target.auth_host}#{test_target.authorize_path}")}
|
22
|
-
it {should include("client_id=#{test_target.client_id}")}
|
23
|
-
it {should include("scope=#{test_target.scope}")}
|
24
|
-
it {should include("redirect_uri=#{test_target.redirect_uri}")}
|
25
|
-
it {should include("access_type=offline")}
|
26
|
-
it {should include("response_type=code")}
|
21
|
+
it { should include("https://#{test_target.auth_host}#{test_target.authorize_path}") }
|
22
|
+
it { should include("client_id=#{test_target.client_id}") }
|
23
|
+
it { should include("scope=#{test_target.scope}") }
|
24
|
+
it { should include("redirect_uri=#{test_target.redirect_uri}") }
|
25
|
+
it { should include("access_type=offline") }
|
26
|
+
it { should include("response_type=code") }
|
27
27
|
end
|
28
28
|
|
29
|
-
let(:access_token_response) {%[{"access_token": "access_token", "token_type":"token_type", "refresh_token":"refresh_token"}] }
|
29
|
+
let(:access_token_response) { %[{"access_token": "access_token", "token_type":"token_type", "refresh_token":"refresh_token"}] }
|
30
30
|
|
31
31
|
describe "fetch_access_token" do
|
32
32
|
|
@@ -45,7 +45,7 @@ describe OmniContacts::Authorization::OAuth2 do
|
|
45
45
|
test_target.fetch_access_token code
|
46
46
|
end
|
47
47
|
|
48
|
-
it "should successfully parse the token from the JSON response" do
|
48
|
+
it "should successfully parse the token from the JSON response" do
|
49
49
|
test_target.should_receive(:https_post).and_return(access_token_response)
|
50
50
|
(access_token, token_type, refresh_token) = test_target.fetch_access_token "code"
|
51
51
|
access_token.should eq("access_token")
|
@@ -53,19 +53,19 @@ describe OmniContacts::Authorization::OAuth2 do
|
|
53
53
|
refresh_token.should eq("refresh_token")
|
54
54
|
end
|
55
55
|
|
56
|
-
it "should raise if the http request fails" do
|
56
|
+
it "should raise if the http request fails" do
|
57
57
|
test_target.should_receive(:https_post).and_raise("Invalid code")
|
58
|
-
expect{test_target.fetch_access_token("code")}.should raise_error
|
58
|
+
expect { test_target.fetch_access_token("code") }.should raise_error
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should raise an error if the JSON response contains an error field" do
|
62
62
|
test_target.should_receive(:https_post).and_return(%[{"error": "error_message"}])
|
63
|
-
expect{test_target.fetch_access_token("code")}.should raise_error
|
63
|
+
expect { test_target.fetch_access_token("code") }.should raise_error
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
describe "refresh_access_token" do
|
68
|
-
it "should provide all mandatory fields in a https post request" do
|
67
|
+
describe "refresh_access_token" do
|
68
|
+
it "should provide all mandatory fields in a https post request" do
|
69
69
|
refresh_token = "refresh_token"
|
70
70
|
test_target.should_receive(:https_post) do |host, path, params|
|
71
71
|
host.should eq(test_target.auth_host)
|
@@ -79,7 +79,7 @@ describe OmniContacts::Authorization::OAuth2 do
|
|
79
79
|
test_target.refresh_access_token refresh_token
|
80
80
|
end
|
81
81
|
|
82
|
-
it "should successfully parse the token from the JSON response" do
|
82
|
+
it "should successfully parse the token from the JSON response" do
|
83
83
|
test_target.should_receive(:https_post).and_return(access_token_response)
|
84
84
|
(access_token, token_type, refresh_token) = test_target.refresh_access_token "refresh_token"
|
85
85
|
access_token.should eq("access_token")
|
@@ -6,13 +6,13 @@ describe OmniContacts::HTTPUtils do
|
|
6
6
|
describe "to_query_string" do
|
7
7
|
it "should create a query string from a map" do
|
8
8
|
OmniContacts::HTTPUtils.to_query_string(:name => "john", :surname => "doe").should eq("name=john&surname=doe")
|
9
|
-
end
|
9
|
+
end
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "encode" do
|
13
13
|
it "should encode the space" do
|
14
14
|
OmniContacts::HTTPUtils.encode("name=\"john\"").should eq("name%3D%22john%22")
|
15
|
-
end
|
15
|
+
end
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "query_string_to_map" do
|
@@ -26,7 +26,7 @@ describe OmniContacts::HTTPUtils do
|
|
26
26
|
|
27
27
|
describe "host_url_from_rack_env" do
|
28
28
|
it "should calculate the host url using the HTTP_HOST variable" do
|
29
|
-
env = {"rack.url_scheme" => "http", "HTTP_HOST" => "localhost:8080","SERVER_NAME" => "localhost", "SERVER_PORT" => 8080}
|
29
|
+
env = {"rack.url_scheme" => "http", "HTTP_HOST" => "localhost:8080", "SERVER_NAME" => "localhost", "SERVER_PORT" => 8080}
|
30
30
|
OmniContacts::HTTPUtils.host_url_from_rack_env(env).should eq("http://localhost:8080")
|
31
31
|
end
|
32
32
|
|
@@ -38,7 +38,7 @@ describe OmniContacts::HTTPUtils do
|
|
38
38
|
|
39
39
|
describe "https_post" do
|
40
40
|
|
41
|
-
before(:each) do
|
41
|
+
before(:each) do
|
42
42
|
@connection = double
|
43
43
|
Net::HTTP.should_receive(:new).and_return(@connection)
|
44
44
|
@connection.should_receive(:use_ssl=).with(true)
|
@@ -62,7 +62,7 @@ describe OmniContacts::HTTPUtils do
|
|
62
62
|
@connection.should_receive(:request_get).and_return(@response)
|
63
63
|
@response.should_receive(:code).and_return("500")
|
64
64
|
@response.should_receive(:body).and_return("some error message")
|
65
|
-
expect {@test_target.send(:https_get, "host", "path", {})}.should raise_error
|
65
|
+
expect { @test_target.send(:https_get, "host", "path", {}) }.should raise_error
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "omnicontacts/importer/gmail"
|
3
3
|
|
4
|
-
describe OmniContacts::Importer::Gmail do
|
4
|
+
describe OmniContacts::Importer::Gmail do
|
5
5
|
|
6
|
-
let(:gmail) { OmniContacts::Importer::Gmail.new(
|
6
|
+
let(:gmail) { OmniContacts::Importer::Gmail.new({}, "client_id", "client_secret") }
|
7
7
|
|
8
8
|
let(:contacts_as_xml) {
|
9
9
|
"<entry xmlns:gd='http://schemas.google.com/g/2005'>
|
@@ -14,12 +14,12 @@ describe OmniContacts::Importer::Gmail do
|
|
14
14
|
</entry>"
|
15
15
|
}
|
16
16
|
|
17
|
-
describe
|
17
|
+
describe "fetch_contacts_using_access_token" do
|
18
18
|
|
19
|
-
let(:token) { "token"}
|
19
|
+
let(:token) { "token" }
|
20
20
|
let(:token_type) { "token_type" }
|
21
21
|
|
22
|
-
it "should request the contacts by specifying version and code in the http headers" do
|
22
|
+
it "should request the contacts by specifying version and code in the http headers" do
|
23
23
|
gmail.should_receive(:https_get) do |host, path, params, headers|
|
24
24
|
headers["GData-Version"].should eq("3.0")
|
25
25
|
headers["Authorization"].should eq("#{token_type} #{token}")
|
@@ -28,7 +28,7 @@ describe OmniContacts::Importer::Gmail do
|
|
28
28
|
gmail.fetch_contacts_using_access_token token, token_type
|
29
29
|
end
|
30
30
|
|
31
|
-
it "should correctly parse name and email" do
|
31
|
+
it "should correctly parse name and email" do
|
32
32
|
gmail.should_receive(:https_get).and_return(contacts_as_xml)
|
33
33
|
result = gmail.fetch_contacts_using_access_token token, token_type
|
34
34
|
result.size.should be(1)
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "omnicontacts/importer/hotmail"
|
3
3
|
|
4
|
-
describe OmniContacts::Importer::Hotmail do
|
4
|
+
describe OmniContacts::Importer::Hotmail do
|
5
5
|
|
6
|
-
let(:hotmail) {OmniContacts::Importer::Hotmail.new({}, "client_id", "client_secret") }
|
6
|
+
let(:hotmail) { OmniContacts::Importer::Hotmail.new({}, "client_id", "client_secret") }
|
7
7
|
|
8
8
|
let(:contacts_as_json) {
|
9
9
|
"{
|
@@ -20,28 +20,28 @@ describe OmniContacts::Importer::Hotmail do
|
|
20
20
|
\"birth_day\": 29,
|
21
21
|
\"birth_month\": 3
|
22
22
|
}]
|
23
|
-
}"}
|
23
|
+
}" }
|
24
24
|
|
25
|
-
|
25
|
+
describe "fetch_contacts_using_access_token" do
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
let(:token) { "token" }
|
28
|
+
let(:token_type) { "token_type" }
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
-
hotmail.fetch_contacts_using_access_token token, token_type
|
30
|
+
it "should request the contacts by providing the token in the url" do
|
31
|
+
hotmail.should_receive(:https_get) do |host, path, params, headers|
|
32
|
+
params[:access_token].should eq(token)
|
33
|
+
contacts_as_json
|
36
34
|
end
|
35
|
+
hotmail.fetch_contacts_using_access_token token, token_type
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
38
|
+
it "should correctly parse the contacts" do
|
39
|
+
hotmail.should_receive(:https_get).and_return(contacts_as_json)
|
40
|
+
result = hotmail.fetch_contacts_using_access_token token, token_type
|
41
|
+
result.size.should be(1)
|
42
|
+
result.first[:name].should be_nil
|
43
|
+
result.first[:email].should eq("henrik@hotmail.com")
|
45
44
|
end
|
45
|
+
end
|
46
46
|
|
47
47
|
end
|
@@ -9,9 +9,9 @@ describe OmniContacts::Importer::Yahoo do
|
|
9
9
|
{"start":1, "count":1,
|
10
10
|
"contact":[{"id":10, "fields":[{"id":819, "type":"email", "value":"john@yahoo.com"},
|
11
11
|
{"type":"name", "value": { "givenName":"John", "familyName":"Doe"} }] }]
|
12
|
-
} }'}
|
12
|
+
} }' }
|
13
13
|
|
14
|
-
let(:yahoo) { OmniContacts::Importer::Yahoo.new(
|
14
|
+
let(:yahoo) { OmniContacts::Importer::Yahoo.new({}, "consumer_key", "consumer_secret") }
|
15
15
|
|
16
16
|
it "should request the contacts by specifying all required parameters" do
|
17
17
|
yahoo.should_receive(:fetch_access_token).and_return(["access_token", "access_token_secret", "guid"])
|
@@ -17,13 +17,14 @@ describe OmniContacts::Middleware::OAuth1 do
|
|
17
17
|
"http://www.example.com"
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
def fetch_contacts_from_token_and_verifier oauth_token, ouath_token_secret, oauth_verifier
|
21
21
|
[{:name => "John Doe", :email => "john@example.com"}]
|
22
22
|
end
|
23
23
|
|
24
24
|
def self.mock_session
|
25
25
|
@mock_session ||= {}
|
26
26
|
end
|
27
|
+
|
27
28
|
def session
|
28
29
|
OAuth1Middleware.mock_session
|
29
30
|
end
|
@@ -32,8 +33,8 @@ describe OmniContacts::Middleware::OAuth1 do
|
|
32
33
|
|
33
34
|
let(:app) {
|
34
35
|
Rack::Builder.new do |b|
|
35
|
-
|
36
|
-
|
36
|
+
b.use OAuth1Middleware, "consumer_id", "consumer_secret"
|
37
|
+
b.run lambda { |env| [200, {"Content-Type" => "text/html"}, ["Hello World"]] }
|
37
38
|
end.to_app
|
38
39
|
}
|
39
40
|
|
@@ -61,11 +62,11 @@ describe OmniContacts::Middleware::OAuth1 do
|
|
61
62
|
last_request.env["omnicontacts.contacts"].size.should be(1)
|
62
63
|
end
|
63
64
|
|
64
|
-
it "should redirect to failure url if oauth_token_secret is not found in the session" do
|
65
|
+
it "should redirect to failure url if oauth_token_secret is not found in the session" do
|
65
66
|
OAuth1Middleware.mock_session.should_receive(:[]).and_return(nil)
|
66
67
|
get "/contacts/oauth1middleware/callback?oauth_token=token&oauth_verifier=verifier"
|
67
68
|
last_response.should be_redirect
|
68
|
-
last_response.headers["location"].should eq("/contacts/failure?error_message=not_authorized")
|
69
|
+
last_response.headers["location"].should eq("/contacts/failure?error_message=not_authorized")
|
69
70
|
end
|
70
71
|
end
|
71
72
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "omnicontacts/middleware/oauth2"
|
3
3
|
|
4
|
-
describe OmniContacts::Middleware::OAuth2 do
|
4
|
+
describe OmniContacts::Middleware::OAuth2 do
|
5
5
|
|
6
|
-
before(:all) do
|
6
|
+
before(:all) do
|
7
7
|
class OAuth2Middleware < OmniContacts::Middleware::OAuth2
|
8
8
|
def authorization_url
|
9
9
|
"http://www.example.com"
|
@@ -18,11 +18,11 @@ describe OmniContacts::Middleware::OAuth2 do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def session
|
21
|
-
OAuth2Middleware.mock_session
|
21
|
+
OAuth2Middleware.mock_session
|
22
22
|
end
|
23
23
|
|
24
24
|
def fetch_access_token code
|
25
|
-
["access_token", "token_type", "token_refresh"]
|
25
|
+
["access_token", "token_type", "token_refresh"]
|
26
26
|
end
|
27
27
|
|
28
28
|
def fetch_contacts_using_access_token token, token_type
|
@@ -33,8 +33,8 @@ describe OmniContacts::Middleware::OAuth2 do
|
|
33
33
|
|
34
34
|
let(:app) {
|
35
35
|
Rack::Builder.new do |b|
|
36
|
-
|
37
|
-
|
36
|
+
b.use OAuth2Middleware, "client_id", "client_secret"
|
37
|
+
b.run lambda { |env| [200, {"Content-Type" => "text/html"}, ["Hello World"]] }
|
38
38
|
end.to_app
|
39
39
|
}
|
40
40
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omnicontacts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|