Module: OpenCNAM
- Defined in:
- lib/opencnam.rb,
lib/opencnam/util.rb,
lib/opencnam/version.rb,
lib/opencnam/errors/no_info_error.rb,
lib/opencnam/errors/opencnam_error.rb,
lib/opencnam/errors/throttled_error.rb,
lib/opencnam/errors/bad_request_error.rb,
lib/opencnam/errors/running_lookup_error.rb,
lib/opencnam/errors/invalid_phone_number_error.rb
Defined Under Namespace
Modules: Util
Classes: BadRequestError, InvalidPhoneNumberError, NoInfoError, OpenCNAMError, RunningLookupError, ThrottledError
Constant Summary
collapse
- VERSION =
'0.0.1'
- @@api_base =
'https://api.opencnam.com'
- @@api_user =
nil
- @@api_key =
nil
Class Method Summary
collapse
Class Method Details
.api_key=(api_key) ⇒ Object
21
22
23
|
# File 'lib/opencnam.rb', line 21
def self.api_key=(api_key)
@@api_key = api_key
end
|
.api_user=(api_user) ⇒ Object
17
18
19
|
# File 'lib/opencnam.rb', line 17
def self.api_user=(api_user)
@@api_user = api_user
end
|
.lookup(number) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/opencnam.rb', line 25
def self.lookup(number)
clean_number = OpenCNAM::Util.clean_phone_number(number)
query = URI.encode_www_form(
:format => 'text',
:username => @@api_user,
:api_key => @@api_key,
)
uri = URI.parse(@@api_base)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
res = http.request_get("/v1/phone/#{clean_number}?#{query}")
if res.kind_of?(Net::HTTPOK)
return {
:name => res.body,
:number => clean_number,
}
end
error_attrs = {
:http_status => res.code,
:message => res.message,
}
if res.kind_of?(Net::HTTPAccepted)
raise RunningLookupError.new(error_attrs)
end
if res.kind_of?(Net::HTTPForbidden)
raise ThrottledError.new(error_attrs)
end
if res.kind_of?(Net::HTTPBadRequest)
raise BadRequestError.new(error_attrs)
end
if res.kind_of?(Net::HTTPNotFound)
raise NoInfoError.new(error_attrs)
end
raise OpenCNAMError.new(error_attrs)
end
|