domain_tools 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ DomainTools::where("var1=value1&var2=value2")
14
14
  # Useful to separate in a settings file your default configuration
15
15
  DomainTools::with({
16
16
  :username => DOMAINTOOLS_USERNAME,
17
- :key => DOMAINTOOLS_KEY,
17
+ :key => DOMAINTOOLS_KEY,
18
18
  :service => "whois",
19
19
  :format => "json",
20
20
  :domain => "domaintools.com",
@@ -0,0 +1,36 @@
1
+ # -----------------------------------------------------------------------------
2
+ # 6. USING THE FREE API
3
+ # -----------------------------------------------------------------------------
4
+
5
+ # 6.A Define settings
6
+ # This a a clear way to define each settings
7
+ DomainTools::use(DOMAINTOOLS_USERNAME,DOMAINTOOLS_KEY)
8
+ DomainTools::get("whois")
9
+ # Set the API type using the "with_api" method
10
+ DomainTools::with_api("free")
11
+ DomainTools::on("domaintools.com")
12
+
13
+ # 6.B All in one with a hash
14
+ # Useful to separate in a settings file your default configuration
15
+ DomainTools::with({
16
+ :username => DOMAINTOOLS_USERNAME,
17
+ :key => DOMAINTOOLS_KEY,
18
+ :service => "whois",
19
+ :domain => "domaintools.com",
20
+ :api => "free"
21
+ })
22
+
23
+ # 1.C Inline
24
+ # Each call can be chained, useful in some case to gain space in your code
25
+ DomainTools::use(DOMAINTOOLS_USERNAME,DOMAINTOOLS_KEY)::get("whois")::as("json")::on("domaintools.com")::with_api("free")
26
+ # -----------------------------------------------------------------------------
27
+ # TIPS
28
+ # -----------------------------------------------------------------------------
29
+ # You can use "is_free?" on a request object to test if it is using the freeapi
30
+
31
+ request = DomainTools::get("whois")::as("json")::on("domaintools.com")::with_api("free").request
32
+ if request.is_free?
33
+ puts "It's a free request!"
34
+ else
35
+ puts "It's a standard request!"
36
+ end
data/lib/domain_tools.rb CHANGED
@@ -4,6 +4,9 @@ module DomainTools
4
4
 
5
5
  # Defaut HOST for the request
6
6
  HOST = "api.domaintools.com"
7
+ # HOST for the Free API
8
+ FREE_HOST = "freeapi.domaintools.com"
9
+
7
10
  # Use Signed Authentication
8
11
  SIGNED = true
9
12
  # Digest method used for HMAC signature
@@ -22,6 +22,11 @@ module DomainTools
22
22
  self
23
23
  end
24
24
 
25
+ # Change the host, to swith between free and standard API
26
+ def self.with_api(api)
27
+ self.set_data :api, api
28
+ end
29
+
25
30
  # Select which service must be called for this request
26
31
  def self.get(service)
27
32
  self.set_data :service, service
@@ -93,7 +98,11 @@ module DomainTools
93
98
 
94
99
  def self.success?
95
100
  self.request.success?
96
- end
101
+ end
102
+
103
+ def self.is_free?
104
+ self.request.is_free?
105
+ end
97
106
 
98
107
  def self.to_s
99
108
  self.request.to_s
@@ -127,6 +136,7 @@ module DomainTools
127
136
 
128
137
  def self.set_data(key,val)
129
138
  @data = {} unless @data
139
+ key = :host if key.to_s == 'api'
130
140
  @data[key.to_sym] = val
131
141
  # Update data for future request
132
142
  @request = DomainTools::Request.new @data
@@ -1,6 +1,6 @@
1
1
  module DomainTools
2
2
  class Request
3
- attr_accessor :domain, :format, :service, :parameters
3
+ attr_accessor :username, :key, :domain, :format, :service, :parameters, :api
4
4
 
5
5
  def initialize(data)
6
6
  data.each{|key, value| set_data(key,value)}
@@ -42,10 +42,11 @@ module DomainTools
42
42
  @url = parts.join("")
43
43
  end
44
44
 
45
- def authentication_params(uri)
45
+ def authentication_params(uri)
46
+ return '' if is_free?
46
47
  return "&api_username=#{@username}&api_key=#{@key}" unless @signed
47
48
  timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
48
- data = @username+timestamp+uri
49
+ data = @username+timestamp+uri
49
50
  require 'openssl'
50
51
  digester = OpenSSL::Digest::Digest.new(DomainTools::DIGEST)
51
52
  signature = OpenSSL::HMAC.hexdigest(digester, @key, data)
@@ -55,12 +56,12 @@ module DomainTools
55
56
 
56
57
  def validate
57
58
  raise DomainTools::NoDomainException unless @domain || @parameters
58
- raise DomainTools::NoCredentialsException unless @username || @key
59
+ raise DomainTools::NoCredentialsException unless @username && @key
59
60
  # must be a valid format (will be default FORMAT constant if empty or wrong)
60
- @format = DomainTools::FORMAT if @format!="json" && @format!="xml" && @format != "html"
61
+ @format = DomainTools::FORMAT unless ['json','xml','html'].include?(@format)
61
62
  # if not already defined, use default
62
63
  @host = DomainTools::HOST if @host.nil?
63
- @port = DomainTools::PORT if @port.nil?
64
+ @port = DomainTools::PORT if @port.nil?
64
65
  @signed = DomainTools::SIGNED if @signed.nil?
65
66
  @version = DomainTools::VERSION if @version.nil?
66
67
  end
@@ -103,7 +104,7 @@ module DomainTools
103
104
  end
104
105
 
105
106
  # Check HTTP request status and raise an exception if needed
106
- def validate_http_status
107
+ def validate_http_status
107
108
  return true if @http.code.to_i == 200
108
109
  DomainTools::Exceptions::raise_by_code(@http.code)
109
110
  end
@@ -155,10 +156,22 @@ module DomainTools
155
156
  nil
156
157
  end
157
158
 
158
- private
159
+ def is_free?
160
+ @host==DomainTools::FREE_HOST
161
+ end
162
+
163
+ def api=(api)
164
+ self.host = api
165
+ end
166
+
167
+ def host=(h)
168
+ @host = ['freeapi','free'].include?(h.to_s) ? DomainTools::FREE_HOST : DomainTools::HOST
169
+ end
170
+
171
+ private
159
172
 
160
173
  def set_data(key,val)
161
- eval("@#{key.to_s} = val")
174
+ eval("self.#{key.to_s} = val")
162
175
  end
163
176
 
164
177
  def format_parameters
metadata CHANGED
@@ -1,32 +1,23 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: domain_tools
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - DomainTools, LLC
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-10-26 00:00:00 Z
12
+ date: 2012-07-17 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
- description: Ruby wrapper of the domaintools.com API, allowing you to easily request any service available on domaintools.com
14
+ description: Ruby wrapper of the domaintools.com API, allowing you to easily request
15
+ any service available on domaintools.com
22
16
  email: MemberServices@DomainTools.com
23
17
  executables: []
24
-
25
18
  extensions: []
26
-
27
19
  extra_rdoc_files: []
28
-
29
- files:
20
+ files:
30
21
  - lib/domain_tools/core.rb
31
22
  - lib/domain_tools/error.rb
32
23
  - lib/domain_tools/error_parser.rb
@@ -42,40 +33,29 @@ files:
42
33
  - examples/3_browsing.rb
43
34
  - examples/4_error.rb
44
35
  - examples/5_complete.rb
36
+ - examples/6_using_freeapi.rb
45
37
  homepage: http://www.domaintools.com/api/
46
38
  licenses: []
47
-
48
39
  post_install_message:
49
40
  rdoc_options: []
50
-
51
- require_paths:
41
+ require_paths:
52
42
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
43
+ required_ruby_version: !ruby/object:Gem::Requirement
54
44
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
62
- required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
50
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- hash: 19
68
- segments:
69
- - 1
70
- - 3
71
- - 4
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
72
54
  version: 1.3.4
73
55
  requirements: []
74
-
75
56
  rubyforge_project: domain_tools
76
- rubygems_version: 1.8.11
57
+ rubygems_version: 1.8.24
77
58
  signing_key:
78
59
  specification_version: 3
79
60
  summary: Ruby gem for requesting domaintools.com webservices
80
61
  test_files: []
81
-