cynic 0.0.3.4 → 0.0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4eba83ffc70d526a9bd125bd744a45d6d1523646
4
- data.tar.gz: 08016075298c30877a1002fa864af77e21f1b309
3
+ metadata.gz: 4d3f7a1b07f7ec9818bdda6bd95a0f8699ad8446
4
+ data.tar.gz: 46214dc5a9ed1b61e18f652e4bfd96cfc699340b
5
5
  SHA512:
6
- metadata.gz: 4e8fb783d5827d5d765637b2e1d62532f0508c40795590f632c23f5d756c2aa0668d92fd89f7fef3af74588a2dfc3bf0ae940fbc8c689b6027211e5ae1c76a11
7
- data.tar.gz: 30defcf46ad77ab6bfca77b7647ba9e40cbed8f6443d7e9e8f244b6415a09857f18dcc2a3532fee01351414ea7d96235c30da95cd48d2c180d0bfa908d0b4d8d
6
+ metadata.gz: 78b60f352309b42f6b0229a694a5b09f9b2736aaa06029af31b51dcdc6ff16e2b2a5ffbde19cad4d49bfd0de5577bed1940d785bf389fc563186f66dc6041b0c
7
+ data.tar.gz: a600c0bd234a9b9aa29dc5e6a0e15f8063f10e6fc43cd7b3f761687a3cbb6a3681944e3c8c9d5f677b4e280466d2bec3d5ee5878f3e18d20200aed038295bc9d
data/lib/cynic/app.rb CHANGED
@@ -18,47 +18,31 @@ module Cynic
18
18
 
19
19
  def call(env)
20
20
  @env = env
21
- send_response
21
+ response.send
22
22
  end
23
23
 
24
24
  def request
25
- @request ||= Rack::Request.new(@env)
25
+ Rack::Request.new(@env)
26
26
  end
27
27
 
28
28
  def routing_to_request
29
29
  routing.go_to request.request_method.downcase.to_sym, @env["REQUEST_PATH"]
30
30
  end
31
31
 
32
- def send_response
33
- [status, headers, response]
34
- end
35
-
36
32
  def response
37
- Response.new(execute_controller_actions)
33
+ Response.new(execute_controller_actions, request)
38
34
  end
39
35
 
40
36
  def execute_controller_actions
41
37
  route = self.routing_to_request
42
- if route.object.respond_to? :request=
43
- route.params.each do |k,v|
44
- request.update_param(k, v)
45
- end
46
- route.object.request = request
38
+
39
+ route.params.each do |k,v|
40
+ request.update_param(k, v)
47
41
  end
42
+ route.object.request = request
43
+
48
44
  route.object.response(route.method)
49
45
  end
50
46
 
51
- def status
52
- 200
53
- end
54
-
55
- def content_type
56
- request.content_type || "text/html"
57
- end
58
-
59
- def headers
60
- {"CONTENT-TYPE" => content_type}
61
- end
62
-
63
47
  end
64
48
  end
@@ -3,6 +3,7 @@ module Cynic
3
3
  attr_accessor :request
4
4
  class << self
5
5
  attr_accessor :before_actions
6
+
6
7
  def before_actions
7
8
  @before_actions ||= Hash.new { Set.new }
8
9
  end
@@ -32,6 +33,10 @@ module Cynic
32
33
  end
33
34
 
34
35
  def params
36
+ request.params.each do |k,v|
37
+ request.update_param(k.to_sym, v) if k.is_a? String
38
+ end
39
+
35
40
  request.params
36
41
  end
37
42
 
@@ -1,11 +1,35 @@
1
1
  module Cynic
2
2
  class Response
3
- attr_reader :body
4
- def initialize(body)
3
+ attr_reader :body, :content_type, :headers, :status
4
+
5
+ def initialize(body, request)
5
6
  @body = body
7
+ @content_type = set_content_type(request)
6
8
  end
9
+
7
10
  def each(&block)
8
11
  block.call @body
9
12
  end
13
+
14
+ def status
15
+ 200
16
+ end
17
+
18
+ def set_content_type(request)
19
+ if request.params.has_key? "format"
20
+ "text/#{request.params['format']}"
21
+ else
22
+ request.content_type =~ /text/ ? request.content_type : "text/html"
23
+ end
24
+ end
25
+
26
+ def headers
27
+ {"CONTENT-TYPE" => content_type}
28
+ end
29
+
30
+ def send
31
+ [status, headers, self]
32
+ end
33
+
10
34
  end
11
35
  end
data/lib/cynic/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cynic
2
- VERSION = "0.0.3.4"
2
+ VERSION = "0.0.3.5"
3
3
  end
@@ -13,16 +13,17 @@ end
13
13
 
14
14
  describe Cynic::Controller do
15
15
  let(:controller) { CynicController.new }
16
+ let(:env) { {"REQUEST_METHOD" => "GET", "REQUEST_PATH" => "/cynic", "rack.input" => "wtf", "QUERY_STRING" => "id=1", "CONTENT_TYPE" => "text/json"} }
17
+
16
18
  before { CynicController.instance_variable_set(:@before_actions, nil)}
17
19
 
18
20
  describe "#request" do
19
21
  before {
20
- request = double("Rack::Request")
21
- request.stub(:params) { {id: 1} }
22
+ request = Rack::Request.new(env)
22
23
  controller.request = request
23
24
  }
24
25
  it "has params" do
25
- expect(controller.params).to eq({id: 1})
26
+ expect(controller.params[:id]).to eq "1"
26
27
  end
27
28
  end
28
29
  describe "#render" do
@@ -1,7 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Cynic::Response do
4
- let(:response) { Cynic::Response.new("This is the body") }
4
+ let(:env) { {"REQUEST_METHOD" => "GET", "REQUEST_PATH" => "/cynic", "rack.input" => "wtf", "CONTENT_TYPE" => "text/json"} }
5
+ let(:response) { Cynic::Response.new("This is the body", Rack::Request.new(env)) }
5
6
  describe "#each" do
6
7
  it "responds" do
7
8
  expect(response).to respond_to :each
@@ -11,4 +12,20 @@ describe Cynic::Response do
11
12
  expect(response.each {|k| k }).to eq "This is the body"
12
13
  end
13
14
  end
15
+
16
+ describe "content_type" do
17
+ it "sets it to json" do
18
+ expect(response.content_type).to eq "text/json"
19
+ end
20
+
21
+ it "sets it to html if unknown" do
22
+ env["CONTENT_TYPE"] = "app/something"
23
+ expect(response.content_type).to eq "text/html"
24
+ end
25
+
26
+ it "the params takes prority" do
27
+ env["QUERY_STRING"] = "format=jsonp"
28
+ expect(response.content_type).to eq "text/jsonp"
29
+ end
30
+ end
14
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cynic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.4
4
+ version: 0.0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bookis Smuin