nap 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2008 Manfred Stienstra, Fingertips <manfred@fngtps.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,4 @@
1
+ Nap
2
+ ---
3
+
4
+ It be an extremely simple REST library, yo!
data/lib/rest.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'uri'
2
+
3
+ module REST
4
+ def self.get(uri, headers={})
5
+ REST::Request.perform(:get, URI.parse(uri), nil, headers)
6
+ end
7
+
8
+ def self.head(uri, headers={})
9
+ REST::Request.perform(:head, URI.parse(uri), nil, headers)
10
+ end
11
+
12
+ def self.put(uri, body, headers={})
13
+ REST::Request.perform(:put, URI.parse(uri), body, headers)
14
+ end
15
+
16
+ def self.post(uri, body, headers={})
17
+ REST::Request.perform(:post, URI.parse(uri), body, headers)
18
+ end
19
+ end
20
+
21
+ require File.expand_path('../rest/request', __FILE__)
22
+ require File.expand_path('../rest/response', __FILE__)
@@ -0,0 +1,37 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module REST
5
+ class Request
6
+ attr_accessor :verb, :url, :body, :headers, :request
7
+
8
+ def initialize(verb, url, body=nil, headers={})
9
+ @verb = verb
10
+ @url = url
11
+ @body = body
12
+ @headers = headers
13
+ end
14
+
15
+ def perform
16
+ case verb
17
+ when :get
18
+ self.request = Net::HTTP::Get.new(url.path, headers)
19
+ when :head
20
+ self.request = Net::HTTP::Head.new(url.path, headers)
21
+ when :put
22
+ self.request = Net::HTTP::Put.new(url.path, headers)
23
+ self.request.body = body
24
+ when :post
25
+ self.request = Net::HTTP::Post.new(url.path, headers)
26
+ self.request.body = body
27
+ end
28
+ response = Net::HTTP.new(url.host, url.port).start {|http| http.request(request) }
29
+ REST::Response.new(response.code, response.__send__(:instance_variable_get, '@header'), response.body)
30
+ end
31
+
32
+ def self.perform(*args)
33
+ request = new(*args)
34
+ request.perform
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ module REST
2
+ class Response
3
+ CODES = [
4
+ [200, :ok],
5
+ [301, :moved_permanently],
6
+ [302, :found],
7
+ [400, :bad_request],
8
+ [401, :unauthorized],
9
+ [403, :forbidden],
10
+ [422, :unprocessable_entity],
11
+ [404, :not_found],
12
+ [500, :internal_server_error]
13
+ ]
14
+
15
+ attr_accessor :body, :headers, :status_code
16
+
17
+ def initialize(status_code, headers={}, body='')
18
+ @status_code = status_code.to_i
19
+ @headers = headers
20
+ @body = body
21
+ end
22
+
23
+ CODES.each do |code, name|
24
+ define_method "#{name}?" do
25
+ status_code == code
26
+ end
27
+ end
28
+
29
+ def success?
30
+ (status_code.to_s =~ /2../) ? true : false
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nap
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Manfred Stienstra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-09 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Nap is a really simple REST API.
17
+ email: manfred@fngtps.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - lib/rest/request.rb
27
+ - lib/rest/response.rb
28
+ - lib/rest.rb
29
+ - README
30
+ - LICENSE
31
+ has_rdoc: true
32
+ homepage:
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --charset=utf-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: Nap is a really simple REST API.
57
+ test_files: []
58
+