nap 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,4 +1,24 @@
1
- Nap
2
- ---
1
+ = Nap
3
2
 
4
- It be an extremely simple REST library, yo!
3
+ It be an extremely simple REST library, yo!
4
+
5
+ == Example
6
+
7
+ gem 'nap'
8
+ require 'rest'
9
+ require 'json'
10
+
11
+ response = REST.get('http://twitter.com/statuses/friends_timeline.json', {},
12
+ {:username => '_evan', :password => 'buttonscat'}
13
+ )
14
+ if response.ok?
15
+ timeline = JSON.parse(response.body)
16
+ puts(timeline.map do |item|
17
+ "#{item['user']['name']}\n\n#{item['text']}"
18
+ end.join("\n\n--\n\n"))
19
+ elsif response.forbidden?
20
+ puts "Are you sure you're `_evan' and your password is the name of your cat?"
21
+ else
22
+ puts "Something went wrong (#{response.status_code})"
23
+ puts response.body
24
+ end
@@ -1,20 +1,67 @@
1
1
  require 'uri'
2
2
 
3
+ # REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API for doing REST-style
4
+ # HTTP calls.
3
5
  module REST
4
- def self.get(uri, headers={})
5
- REST::Request.perform(:get, URI.parse(uri), nil, headers)
6
+ # Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.
7
+ #
8
+ # response = REST.get('http://example.com/pigeons/12',
9
+ # {'Accept' => 'text/plain'},
10
+ # {:username => 'admin', :password => 'secret'}
11
+ # )
12
+ # if response.ok?
13
+ # puts response.body
14
+ # else
15
+ # puts "Couldn't fetch your pigeon (#{response.status_code})"
16
+ # end
17
+ def self.get(uri, headers={}, options={})
18
+ REST::Request.perform(:get, URI.parse(uri), nil, headers, options)
6
19
  end
7
20
 
8
- def self.head(uri, headers={})
9
- REST::Request.perform(:head, URI.parse(uri), nil, headers)
21
+ # Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.
22
+ #
23
+ # response = REST.head('http://example.com/pigeons/12')
24
+ # if response.ok?
25
+ # puts "Your pigeon exists!"
26
+ # elsif response.found?
27
+ # puts "Someone moved your pigeon!"
28
+ # else
29
+ # puts "Couldn't fetch your pigeon (#{response.status_code})"
30
+ # end
31
+ def self.head(uri, headers={}, options={})
32
+ REST::Request.perform(:head, URI.parse(uri), nil, headers, options)
10
33
  end
11
34
 
12
- def self.put(uri, body, headers={})
13
- REST::Request.perform(:put, URI.parse(uri), body, headers)
35
+ # Performs a PUT on a resource. See REST::Request.new for a complete discussion of options.
36
+ #
37
+ # response = REST.put('http://example.com/pigeons/12',
38
+ # {'Name' => 'Homer'}.to_xml,
39
+ # {'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
40
+ # )
41
+ # if response.ok?
42
+ # puts "Your pigeon was renamed to 'Homer'!"
43
+ # else
44
+ # puts "Couldn't rename your pigeon (#{response.status_code})"
45
+ # puts XML.parse(response.body).reason
46
+ # end
47
+ def self.put(uri, body, headers={}, options={})
48
+ REST::Request.perform(:put, URI.parse(uri), body, headers, options)
14
49
  end
15
50
 
16
- def self.post(uri, body, headers={})
17
- REST::Request.perform(:post, URI.parse(uri), body, headers)
51
+ # Performs a POST on a resource. See REST::Request.new for a complete discussion of options.
52
+ #
53
+ # response = REST.post('http://example.com/pigeons',
54
+ # {'Name' => 'Bowser'}.to_xml,
55
+ # {'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
56
+ # )
57
+ # if response.created?
58
+ # puts "Created a new pigeon called 'Bowser'"
59
+ # else
60
+ # puts "Couldn't create your pigeon (#{response.status_code})"
61
+ # puts XML.parse(response.body).reason
62
+ # end
63
+ def self.post(uri, body, headers={}, options={})
64
+ REST::Request.perform(:post, URI.parse(uri), body, headers, options)
18
65
  end
19
66
  end
20
67
 
@@ -1,17 +1,46 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
3
 
4
- module REST
4
+ module REST #:nodoc:
5
+ # Request holds a HTTP request
5
6
  class Request
6
- attr_accessor :verb, :url, :body, :headers, :request
7
+ attr_accessor :verb, :url, :body, :headers, :options, :request
7
8
 
8
- def initialize(verb, url, body=nil, headers={})
9
+ # * <tt>verb</tt>: The verb to use in the request, either :get, :head, :put, or :post
10
+ # * <tt>url</tt>: The URL to send the request to, must be a URI instance
11
+ # * <tt>body</tt>: The body to use in the request
12
+ # * <tt>headers</tt>: A hash of headers to add to the request
13
+ # * <tt>options</tt>: A hash of additional options
14
+ # * <tt>username</tt>: Username to use for basic authentication
15
+ # * <tt>password</tt>: Password to use for basic authentication
16
+ #
17
+ # Examples
18
+ #
19
+ # request = REST::Request.new(:get, URI.parse('http://example.com/pigeons/1'))
20
+ #
21
+ # request = REST::Request.new(:head, URI.parse('http://example.com/pigeons/1'))
22
+ #
23
+ # request = REST::Request.new(:post,
24
+ # URI.parse('http://example.com/pigeons'),
25
+ # {'name' => 'Homr'}.to_json,
26
+ # {'Accept' => 'application/json, */*', 'Content-Type' => 'application/json; charset=utf-8'}
27
+ # )
28
+ #
29
+ # request = REST::Request.new(:put,
30
+ # URI.parse('http://example.com/pigeons/1'),
31
+ # {'name' => 'Homer'}.to_json,
32
+ # {'Accept' => 'application/json, */*', 'Content-Type' => 'application/json; charset=utf-8'},
33
+ # {:username => 'Admin', :password => 'secret'}
34
+ # )
35
+ def initialize(verb, url, body=nil, headers={}, options={})
9
36
  @verb = verb
10
37
  @url = url
11
38
  @body = body
12
39
  @headers = headers
40
+ @options = options
13
41
  end
14
42
 
43
+ # Performs the actual request and returns a REST::Response object with the response
15
44
  def perform
16
45
  case verb
17
46
  when :get
@@ -24,11 +53,21 @@ module REST
24
53
  when :post
25
54
  self.request = Net::HTTP::Post.new(url.path, headers)
26
55
  self.request.body = body
56
+ else
57
+ raise ArgumentError, "Unknown HTTP verb `#{verb}'"
27
58
  end
59
+
60
+ if options[:username] and options[:password]
61
+ request.basic_auth(options[:username], options[:password])
62
+ end
63
+
28
64
  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)
65
+ REST::Response.new(response.code, response.instance_variable_get('@header'), response.body)
30
66
  end
31
67
 
68
+ # Shortcut for REST::Request.new(*args).perform.
69
+ #
70
+ # See new for options.
32
71
  def self.perform(*args)
33
72
  request = new(*args)
34
73
  request.perform
@@ -1,7 +1,16 @@
1
- module REST
1
+ module REST #:nodoc:
2
+ # Response holds a HTTP response
2
3
  class Response
4
+ # These codes are used to define convenience boolean accessors on the response object.
5
+ #
6
+ # Examples
7
+ #
8
+ # REST::Response.new(200).ok? #=> true
9
+ # REST::Response.new(201).ok? #=> falses
10
+ # REST::Response.new(403).forbidden? #=> true
3
11
  CODES = [
4
12
  [200, :ok],
13
+ [201, :created],
5
14
  [301, :moved_permanently],
6
15
  [302, :found],
7
16
  [400, :bad_request],
@@ -14,6 +23,9 @@ module REST
14
23
 
15
24
  attr_accessor :body, :headers, :status_code
16
25
 
26
+ # * <tt>status_code</tt>: The status code of the response (ie. 200 or '404')
27
+ # * <tt>headers</tt>: The headers of the response
28
+ # * <tt>body</tt>: The body of the response
17
29
  def initialize(status_code, headers={}, body='')
18
30
  @status_code = status_code.to_i
19
31
  @headers = headers
@@ -26,6 +38,7 @@ module REST
26
38
  end
27
39
  end
28
40
 
41
+ # Returns _true_ when the status code is in the 2XX range. Returns false otherwise.
29
42
  def success?
30
43
  (status_code.to_s =~ /2../) ? true : false
31
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nap
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manfred Stienstra
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-09 00:00:00 +02:00
12
+ date: 2008-10-09 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15