lunetas 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,8 +13,8 @@ a Regular Expression. It may respond to one or many HTTP methods. These response
13
13
  defined overwritting the methods get, put, post, delete, trace, head, etc.. It can
14
14
  also handle other non-native HTTP methods, using overwritting other_verb.
15
15
 
16
- What does all these means? Checkout an example. (Also you may want to take a look at
17
- the examples folder).
16
+ What does all these means? Checkout an example. (You can also take a look at the
17
+ examples folder).
18
18
 
19
19
  Usage
20
20
  -----
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -0,0 +1,23 @@
1
+ require 'lunetas'
2
+ require 'base64'
3
+
4
+ class ContentTypes
5
+ include Lunetas::Candy
6
+ matches '^/test\.(\w+)', :format
7
+ set_content_type 'text/plain'
8
+
9
+ def get
10
+ case @format
11
+ when 'png'
12
+ set_content_type 'image/png'
13
+ Base64.decode64(DATA)
14
+ when 'html'
15
+ set_content_type 'text/html'
16
+ '<center>cool <img src="/test.png" alt="test" /></center>'
17
+ when 'txt'
18
+ 'OH HAI ;D'
19
+ end
20
+ end
21
+
22
+ DATA = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/I\nNwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJa\nSURBVDjLpZNPSFVREIe/c9/r+tD36vW/IA2rTUYlZbp0FQTRQqmgXFW7loIr\na+NWCFxFENQmgsyEQnGlSIFUVIsURKuHBZmlWWnee985Z6aNaWa0qFkOwzcz\nv/mNUVX+J9K/J9yAyarSrMIxEWpVQYUnIvSpcCV3Qud/rTe/TuAGTL0KN0xF\nS6XJ16LhBlCPRtP42Rck4+0FEc6tb9DBVQDXbxo0X38v2NaElq7DL4wiyQzq\nYjAZgswejA1I3naSTA02bj6t3UsA2282qDKR2n87K8E3fFwAO4e4BPUx32YW\nePk84kBdJfktNXwdOj8vws7tTfo5AFChOdjZmtUQJCnQdfUZXdfH6L45Ab7I\n2MgCmRLP8ONxfPKa0r2tWfE0L4kowvHU+jp8PIy6iMYLu1EXoz5BbcyhGhBf\nRG2M+/KMcOsZxHMcuPQTUEXJWiT6gorlzcgmTDqHQUEVWNTJOXbk7wMJ3lO5\nNIEKkbo4xDvwRcqPnAUTrjiviqc0v525x12gigip5RU8BWxUDSEqlmy+jCBT\nsco06mNMUIr4NDbhFUCwuEKPnX6BCStQAff1EahbBbAzg6TXHiSansAW6VkG\neDoWRtrmcTmCsJzixwckk7eR4qfFzhHFqV6S991oyUEmH7bN24SOFUb6dMec\nTG8+2pmpaITUHG72KT56j7oYk86RylXj2cXsaC+zY32nDrXq3VVWnrxljorn\nWllVS2W4cR/BmgDE4RLP98kxPgy1F5zl4uFL2vfHXwB4d9NkxdMiwjHvqXVF\ncJYnztLnLO01l//yTP8SPwD79F9Uvnnx1AAAAABJRU5ErkJggg==\n"
23
+ end
@@ -4,6 +4,8 @@ module Lunetas::Candy::Initialization
4
4
  attr_reader :url
5
5
  # The url parameters hold by the instance.
6
6
  attr_reader :lunetas_url_instance_params
7
+ # Holds the current headers for the response.
8
+ attr_reader :lunetas_headers
7
9
 
8
10
  # The initialization of a class that includes a Candy, should be done
9
11
  # with rack environment and the url matches from its regular expression.
@@ -14,6 +16,7 @@ module Lunetas::Candy::Initialization
14
16
  # (MatchData instance).to_a
15
17
  def initialize(env, url_matches)
16
18
  @req = Rack::Request.new(env)
19
+ @lunetas_headers = {}
17
20
  @lunetas_url_instance_params = url_matches
18
21
  @url = @lunetas_url_instance_params.shift
19
22
  begin
@@ -31,7 +31,8 @@ module Lunetas::Candy::RequestWrapper
31
31
  # @param [Fixnum] status the redirect status.
32
32
  # @return [nil]
33
33
  def redirect(target, status = 302)
34
- @lunetas_redirect = [status, {'Content-Type' => 'text/plain', 'Location' => target}, []]
34
+ set_header 'Location', target
35
+ @lunetas_redirect = [302, "Moved to #{target}"]
35
36
  end
36
37
 
37
38
  # Is lunetas running in development?
@@ -43,6 +44,22 @@ module Lunetas::Candy::RequestWrapper
43
44
  ENV['RACK_ENV'].nil? || ENV['RACK_ENV'] == 'development'
44
45
  end
45
46
  end
47
+
48
+ # Sets the ContentType for this instance. This overrides the class
49
+ # ContentType.
50
+ # @param [String] content_type the ContentType.
51
+ # @return [nil]
52
+ def set_content_type(content_type)
53
+ set_header 'Content-Type', content_type
54
+ end
55
+
56
+ # Sets a Header for this instance.
57
+ # @param [String, Symbol] header the Header to be set.
58
+ # @param [String] value the value of the Header.
59
+ # @return [nil]
60
+ def set_header(header, value)
61
+ @lunetas_headers[header.to_s] = value
62
+ end
46
63
  end
47
64
  end
48
65
 
@@ -60,11 +60,11 @@ module Lunetas::Candy::ResponseHandler
60
60
  # @param [Fixnum] code the response code.
61
61
  # @return [Array] a Rack::Request response.
62
62
  def response(object, code = 200)
63
- if @lunetas_redirect && @lunetas_redirect.is_a?(Array)
64
- @lunetas_redirect
65
- else
66
- [code, {'Content-Type' => self.class.lunetas_content_type}, [object.to_s]]
63
+ @lunetas_headers['Content-Type'] ||= self.class.lunetas_content_type
64
+ if @lunetas_redirect
65
+ code, object = @lunetas_redirect
67
66
  end
67
+ [code, @lunetas_headers, [object.to_s]]
68
68
  end
69
69
  end
70
70
 
@@ -73,6 +73,27 @@ describe Lunetas::Candy do
73
73
  @instance.bite[1]["Content-Type"].should == "text/plain"
74
74
  end
75
75
 
76
+ it 'should be able to change the ContentType per instance' do
77
+ @instance.set_content_type 'application/json'
78
+ @instance.bite[1]["Content-Type"].should == 'application/json'
79
+ end
80
+
81
+ it 'should be able to set a custom Header' do
82
+ @instance.set_header 'Chunky', 'bacon'
83
+ @instance.bite[1]['Chunky'].should == 'bacon'
84
+ end
85
+
86
+ it 'should be able to add a custom Header with a symbol' do
87
+ @instance.set_header :Chunky, 'bacon'
88
+ @instance.bite[1]['Chunky'].should == 'bacon'
89
+ end
90
+
91
+ it 'should keep the last Header set' do
92
+ @instance.set_header :Chunky, 'bacon'
93
+ @instance.set_header 'Chunky', 'b4c0n'
94
+ @instance.bite[1]['Chunky'].should == 'b4c0n'
95
+ end
96
+
76
97
  it 'should be able to redirect' do
77
98
  mock_env = mock_env('/just_a_test')
78
99
  mock_env['REQUEST_METHOD'] = 'REDIRECT'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lunetas
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Iv\xC3\xA1n Vald\xC3\xA9s (@ivanvc)"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-26 00:00:00 -05:00
18
+ date: 2010-08-27 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -93,6 +93,7 @@ files:
93
93
  - Rakefile
94
94
  - VERSION
95
95
  - examples/config.ru
96
+ - examples/content_types.rb
96
97
  - examples/jay_son.rb
97
98
  - examples/redirect.rb
98
99
  - examples/testing.rb
@@ -146,6 +147,7 @@ test_files:
146
147
  - spec/lunetas/bag_spec.rb
147
148
  - spec/lunetas/candy_spec.rb
148
149
  - spec/spec_helper.rb
150
+ - examples/content_types.rb
149
151
  - examples/jay_son.rb
150
152
  - examples/redirect.rb
151
153
  - examples/testing.rb