domainapi 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/domainapi.rb +159 -0
  2. metadata +54 -0
data/lib/domainapi.rb ADDED
@@ -0,0 +1,159 @@
1
+ module DomainAPI
2
+ require 'net/http'
3
+ # HTTPS not supported yet
4
+ #require 'net/https'
5
+
6
+ class ServiceException < StandardError; end
7
+ class BadRequestException < ServiceException; end
8
+ class NotAuthorizedException < ServiceException; end
9
+ class NotFoundException < ServiceException; end
10
+ class InternalServerErrorException < ServiceException; end
11
+ class ServiceUnavailableException < ServiceException; end
12
+ class WrongParmatersException < ServiceException; end
13
+ class NoDomainException < ServiceException; end
14
+ class UnknownException < ServiceException; end
15
+
16
+ # Defaut HOST for the request
17
+ HOST = "api.domainapi.com"
18
+ # Default PORT for the request
19
+ PORT = "80"
20
+ # Default VERSION for the request
21
+ VERSION = "v1"
22
+ # Default FORMAT for the request
23
+ FORMAT = "json"
24
+
25
+ # Authentication of the user
26
+ def self.use(username,password=false)
27
+ if username.kind_of? Hash
28
+ @username,@password = username[:username],username[:password]
29
+ else
30
+ @username, @password = username, password
31
+ end
32
+ self
33
+ end
34
+
35
+ # Select which service must be called for this request
36
+ def self.get(service)
37
+ @service = service
38
+ self
39
+ end
40
+
41
+ # change the format of the response (only XML or JSON)
42
+ def self.as(format)
43
+ @format = format
44
+ self
45
+ end
46
+
47
+ # to specify options to the service
48
+ def self.where(options)
49
+ @options = options
50
+ @options = self.vars_hash_to_string(@options) if @options.kind_of? Hash
51
+ self
52
+ end
53
+
54
+ # to overide settings, only for specific cases or for testing
55
+ def self.with(settings={})
56
+ @host = settings[:host] ? settings[:host] : HOST
57
+ @port = settings[:port] ? settings[:port] : PORT
58
+ @version = settings[:version] ? settings[:version] : VERSION
59
+ self
60
+ end
61
+
62
+ # Param of the request, usually a domain name or sometime the first part of a domain
63
+ def self.on(domain,execute=true)
64
+ @domain = domain
65
+ return self.do if execute
66
+ self
67
+ end
68
+
69
+ # Alias to make a request with only one hash
70
+ def self.execute(params={})
71
+ self.prepare(params)
72
+ self.on params[:on] if params[:on]
73
+ end
74
+
75
+ # used to set data but don't start the request
76
+ def self.prepare(params={})
77
+ raise DomainAPI::WrongParmatersException unless params.kind_of? Hash
78
+ self.use params[:auth] if params[:auth]
79
+ self.get params[:service] if params[:service]
80
+ self.as params[:format] if params[:format]
81
+ self.where params[:options] if params[:options]
82
+ self.with params[:with] if params[:with]
83
+ self.on(params[:on],false) if params[:on]
84
+ self
85
+ end
86
+
87
+ # check first, raise exception if needed, execute the HTTP request
88
+ def self.do
89
+ self.validate
90
+ self.build_url
91
+ self.do_request
92
+ end
93
+
94
+ private
95
+
96
+ # convert a Hash into inline string for GET params
97
+ def self.vars_hash_to_string(hash)
98
+ vars = Array.new
99
+ hash.each {|key, value| vars.push key.to_s+"="+value.to_s}
100
+ vars.join('&')
101
+ end
102
+
103
+ # build service url
104
+ def self.build_url
105
+ @url = "/"+@version+"/"+@service+"/"+@format+"/"+@domain
106
+ @url += "?"+@options if @options
107
+ end
108
+
109
+ def self.validate
110
+ raise DomainAPI::NoDomainException unless @domain
111
+ #must be a valid format (will be default TYPE constant if empty or wrong)
112
+ @format = FORMAT if @format!="json" && @format!="xml"
113
+ #if not set with "with" function, use default
114
+ @host = HOST if !@host
115
+ @port = PORT if !@port
116
+ @version = VERSION if !@version
117
+ end
118
+
119
+ # Connect to the server and execute the request
120
+ def self.do_request
121
+ begin
122
+ @debug = @url
123
+ Net::HTTP.start(@host) do |http|
124
+ req = Net::HTTP::Get.new(@url)
125
+ # HTTP Basic Auth for the user
126
+ req.basic_auth @username, @password
127
+ # Requesting
128
+ @response = http.request(req)
129
+ # Check for errors
130
+ self.check_request_status
131
+ return @response.body
132
+ end
133
+ rescue Exception => e
134
+ # Can be usefull to rescue specific NET::HTTP exceptions here
135
+ raise e
136
+ end
137
+ end
138
+
139
+ # Check HTTP request status and raise an exception if needed
140
+ def self.check_request_status
141
+ case @response.code.to_i
142
+ when 200
143
+ return true
144
+ when 400
145
+ raise DomainAPI::BadRequestException
146
+ when 401
147
+ raise DomainAPI::NotAuthorizedException
148
+ when 404
149
+ raise DomainAPI::NotFoundException
150
+ when 500
151
+ raise DomainAPI::InternalServerErrorException
152
+ when 503
153
+ raise DomainAPI::ServiceUnavailableException
154
+ else
155
+ raise DomainAPI::UnknownException
156
+ end
157
+ end
158
+
159
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: domainapi
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Domain API
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-11 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Ruby wrapper of the domainAPI webservice
17
+ email: info@domainapi.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/domainapi.rb
26
+ homepage: http://github.com/domainAPI/domainAPI_ruby_wrapper
27
+ licenses: []
28
+
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.11
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Domain API Wrapper
53
+ test_files: []
54
+