castle 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,3 +1,18 @@
1
1
  # castle
2
- ### make http requests like a king
3
- a wrapper for ruby's net/http library inspired by [dave thomas](http://pragdave.pragprog.com/) and [john nunemaker](http://railstips.org/)
2
+ ## make http requests like a king
3
+
4
+ *a wrapper for ruby's net/http library inspired by [dave thomas](http://pragdave.pragprog.com/) and [john nunemaker](http://railstips.org/)*
5
+
6
+ ### a quick example
7
+
8
+ class Google
9
+ extend Castle
10
+ base_uri "www.google.com"
11
+
12
+ def self.search query
13
+ get "/search", :q => query.to_s
14
+ end
15
+ end
16
+
17
+ @request = Google.search "i love ruby"
18
+ puts @request.response
@@ -1,11 +1,16 @@
1
1
  %w[net/http uri castle/request castle/response].each {|lib| require lib}
2
2
 
3
3
  module Castle
4
- VERSION = "0.0.2"
5
-
4
+ VERSION = "0.0.3"
5
+
6
+ def base_uri(base_uri = nil)
7
+ return @base_uri unless base_uri
8
+ @base_uri = base_uri
9
+ end
10
+
6
11
  Request::METHODS.each do |method|
7
12
  define_method method do |uri, *query|
8
- Request.new method, uri, *query
13
+ Request.new method, @base_uri.to_s + uri, *query
9
14
  end
10
15
  end
11
16
  end
@@ -2,7 +2,7 @@ module Castle
2
2
  class Request
3
3
  METHODS = [:get, :put, :post, :delete, :head, :options]
4
4
 
5
- attr_accessor :method, :response
5
+ attr_accessor :method, :uri, :response
6
6
 
7
7
  @@default_method = :get
8
8
 
@@ -11,11 +11,11 @@ module Castle
11
11
  @uri = URI.parse(uri.include?("http://") ? "#{uri}" : "http://#{uri}")
12
12
  @query = {:hash => query[0], :string => ""}
13
13
 
14
- i = 0; @query[:hash].to_a.flatten.each {|q| @query[:string] += q.to_s + ((i%2 == 1) ? "&" : "="); i += 1}; @query[:string].chomp! "&"
14
+ i = 0; @query[:hash].to_a.flatten.each {|q| @query[:string] << q.to_s + ((i%2 == 1) ? "&" : "="); i += 1}; @query[:string].chomp! "&"
15
15
 
16
16
  if !@query[:string].empty?
17
17
  @query[:string] = URI.encode @query[:string]
18
- @uri.query = @query[:string] if !@query[:string].empty?
18
+ @uri.query = @query[:string]
19
19
  end
20
20
 
21
21
  args = case @method
@@ -2,8 +2,8 @@ module Castle
2
2
  class Response
3
3
  attr_reader :body
4
4
 
5
- def initialize response
6
- @body = response.body
7
- end
5
+ def initialize response; @body = response.body; end
6
+
7
+ def to_s; @body; end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: castle
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zachary Adam Kaplan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-14 00:00:00 -07:00
18
+ date: 2010-06-15 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency