iprofiler 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/iprofiler/client.rb +51 -15
- data/lib/iprofiler/version.rb +1 -1
- data/spec/cases/api_spec.rb +7 -0
- metadata +1 -1
data/lib/iprofiler/client.rb
CHANGED
@@ -9,34 +9,46 @@ module Iprofiler
|
|
9
9
|
class Client
|
10
10
|
include Api::QueryMethods
|
11
11
|
|
12
|
-
|
12
|
+
CONNECTION_PARAMETERS = [:api_key, :api_secret, :api_host]
|
13
|
+
attr_reader :api_key, :api_secret, :api_host, :missing_credentials
|
14
|
+
|
15
|
+
CONNECTION_PARAMETERS.each do |attr|
|
16
|
+
define_method "#{attr}=" do | val |
|
17
|
+
instance_variable_set("@#{attr}", val).tap do |v|
|
18
|
+
validate_credential_presence
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
13
22
|
|
14
23
|
def initialize(api_key=Iprofiler.api_key, api_secret=Iprofiler.api_secret, api_host=Iprofiler.api_host)
|
15
24
|
@api_key = api_key
|
16
25
|
@api_secret = api_secret
|
17
26
|
@api_host = api_host
|
27
|
+
validate_credential_presence
|
18
28
|
end
|
19
29
|
|
20
30
|
def valid_credentials?
|
21
|
-
company_lookup({}).code == 200
|
31
|
+
credentials_present? && (company_lookup({}).code == 200)
|
22
32
|
end
|
23
33
|
|
24
34
|
protected
|
25
35
|
|
36
|
+
def credentials_present?
|
37
|
+
missing_credentials.size == 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def credentials_missing?
|
41
|
+
!credentials_present?
|
42
|
+
end
|
43
|
+
|
44
|
+
def validate_credential_presence
|
45
|
+
@missing_credentials = CONNECTION_PARAMETERS.select{ |attr| send(attr).nil?}
|
46
|
+
end
|
47
|
+
|
26
48
|
def get(api_path, options)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
reply.code = code
|
31
|
-
if code == 200
|
32
|
-
reply.status = reply.status.to_sym
|
33
|
-
else
|
34
|
-
reply.status = :error
|
35
|
-
end
|
36
|
-
# email requests are served by Leadiq server
|
37
|
-
email = options['email'] || options[:email]
|
38
|
-
reply.email = email unless email.nil?
|
39
|
-
end
|
49
|
+
return credentials_missing_error if credentials_missing?
|
50
|
+
http_reply = Net::HTTP.get_response(request_uri(api_path, options))
|
51
|
+
construct_reply(http_reply, options)
|
40
52
|
end
|
41
53
|
|
42
54
|
private
|
@@ -64,6 +76,30 @@ module Iprofiler
|
|
64
76
|
)
|
65
77
|
end
|
66
78
|
|
79
|
+
def credentials_missing_error
|
80
|
+
return Mash.new.tap do |reply|
|
81
|
+
reply.status = :error
|
82
|
+
reply.code = 400
|
83
|
+
|
84
|
+
missing = CONNECTION_PARAMETERS.none?{ |attr| send(attr).nil?}
|
85
|
+
reply.error = "Invalid or missing API connection parameter(s)[#{missing_credentials.join(",")}]"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def construct_reply(response, options)
|
90
|
+
Mash.from_json(response.body).tap do |reply|
|
91
|
+
code = response.code.to_i
|
92
|
+
reply.code = code
|
93
|
+
if code == 200
|
94
|
+
reply.status = reply.status.to_sym
|
95
|
+
else
|
96
|
+
reply.status = :error
|
97
|
+
end
|
98
|
+
# email requests are served by Leadiq server
|
99
|
+
email = options['email'] || options[:email]
|
100
|
+
reply.email = email unless email.nil?
|
101
|
+
end
|
102
|
+
end
|
67
103
|
end
|
68
104
|
|
69
105
|
end
|
data/lib/iprofiler/version.rb
CHANGED
data/spec/cases/api_spec.rb
CHANGED
@@ -55,6 +55,7 @@ describe Iprofiler::Api do
|
|
55
55
|
client.api_host.should eq(new_api_host)
|
56
56
|
end
|
57
57
|
|
58
|
+
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
@@ -68,6 +69,12 @@ describe Iprofiler::Api do
|
|
68
69
|
client.api_key = "DUMMY KEY"
|
69
70
|
client.valid_credentials?.should eq(false)
|
70
71
|
end
|
72
|
+
|
73
|
+
it "empty authentication parameters should not be allowed" do
|
74
|
+
client.api_key = nil
|
75
|
+
reply = client.company_lookup({})
|
76
|
+
reply.status.should eq(:error)
|
77
|
+
end
|
71
78
|
end
|
72
79
|
|
73
80
|
|