rory 0.5.1 → 0.5.2

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: f460afe6851a0ebbff77cd5459bf5dc2e4637c6c
4
- data.tar.gz: f22a44a3d6141f2db251415ce18724e172fb9ca1
3
+ metadata.gz: f32801437785ab13928284bd7d3085846dc8f04d
4
+ data.tar.gz: 60e766bfd8db4e300c3b661dfc47f7cbb55dbecd
5
5
  SHA512:
6
- metadata.gz: 101bc4e7b9de76e550b74eae963664d84f04a70e497d3016f6f117b74ef0140a9939362b6a338fadb6b82558b430682a6a677550c9513341b103723ab176325a
7
- data.tar.gz: afc2f14d5c59c4e4ba834813b3c2e82f5f6cbec9f8518c8a7f29e90c28dc9dc135904e70aa79da71189e5765b9e9826d24c5faba2dcae2766be8f5b94cafe227
6
+ metadata.gz: 6d51ac0184c96b228640b1169229794bc79443a0e7c10c4d399d0646926d66805595aa37d1be0d5bd5f0955159b3109be4971f8ffcd5101a195ef2af0f364092
7
+ data.tar.gz: 86475e130a4d29a19875e1c81b3da04d4bb2fc0bd2a55850d0cc9ed816dd0b3632436b8acc6d2ef308b8ee0cfac95237864c828a6d818ae560758852373c1b0a
@@ -91,10 +91,10 @@ module Rory
91
91
  end
92
92
 
93
93
  def set_response_defaults(opts)
94
- opts[:content_type] ||= default_content_type
94
+ opts[:content_type] ||= default_content_type(opts)
95
95
  opts[:status] ||= 200
96
96
  opts[:headers] = {
97
- 'Content-type' => default_content_type,
97
+ 'Content-type' => opts[:content_type],
98
98
  'charset' => 'UTF-8'
99
99
  }.merge(opts[:headers] || {})
100
100
  end
@@ -134,8 +134,8 @@ module Rory
134
134
  @response = dispatcher.render_not_found
135
135
  end
136
136
 
137
- def default_content_type
138
- if json_requested?
137
+ def default_content_type(opts = {})
138
+ if json_requested? || opts[:json]
139
139
  'application/json'
140
140
  else
141
141
  'text/html'
data/lib/rory/support.rb CHANGED
@@ -31,16 +31,15 @@ module Rory
31
31
  end
32
32
 
33
33
  def encode_as_json(object)
34
- hashed = if object.is_a?(Array)
35
- object.map { |o| try_to_hash(o) }
36
- else
37
- try_to_hash(object)
38
- end
39
- hashed.to_json
34
+ try_to_hash(object).to_json
40
35
  end
41
36
 
42
37
  def try_to_hash(object)
43
- if object.respond_to?(:to_hash)
38
+ if object.is_a?(Array)
39
+ object.map { |val| try_to_hash(val) }
40
+ elsif object.is_a?(Hash)
41
+ Hash[object.map { |key, val| [key, try_to_hash(val)] }]
42
+ elsif object.respond_to?(:to_hash)
44
43
  object.to_hash
45
44
  else
46
45
  object
data/lib/rory/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rory
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -236,5 +236,10 @@ describe Rory::Controller do
236
236
  allow(subject).to receive(:json_requested?).and_return(true)
237
237
  expect(subject.default_content_type).to eq('application/json')
238
238
  end
239
+
240
+ it "returns 'application/json' if options has :json key" do
241
+ allow(subject).to receive(:json_requested?).and_return(false)
242
+ expect(subject.default_content_type(:json => 'woo')).to eq('application/json')
243
+ end
239
244
  end
240
245
  end
@@ -65,15 +65,15 @@ describe Rory::Support do
65
65
  end
66
66
  end
67
67
 
68
- describe ".encode_as_json" do
69
- it "returns given object as json" do
70
- object = double(:to_json => :jsonified)
71
- expect(described_class.encode_as_json(object)).to eq(:jsonified)
68
+ describe ".try_to_hash" do
69
+ it "returns given object if it does not respond to #to_hash" do
70
+ object = double
71
+ expect(described_class.try_to_hash(object)).to eq(object)
72
72
  end
73
73
 
74
74
  it "calls to_hash first if object responds to it" do
75
75
  object = double(:to_hash => { 'april' => 'friday' })
76
- expect(described_class.encode_as_json(object)).to eq({ 'april' => 'friday' }.to_json)
76
+ expect(described_class.try_to_hash(object)).to eq({ 'april' => 'friday' })
77
77
  end
78
78
 
79
79
  it "converts each member of an array" do
@@ -81,7 +81,39 @@ describe Rory::Support do
81
81
  double(:to_hash => :smurf),
82
82
  double(:to_hash => :nerf)
83
83
  ]
84
- expect(described_class.encode_as_json(object)).to eq([:smurf, :nerf].to_json)
84
+ expect(described_class.try_to_hash(object)).to eq([:smurf, :nerf])
85
+ end
86
+
87
+ it "converts deeply" do
88
+ object = [
89
+ {
90
+ :perf => double(:to_hash => :smurf),
91
+ :kerf => [
92
+ double(:to_hash => :plurf),
93
+ 'yurf'
94
+ ],
95
+ :erf => { :burf => double(:to_hash => :wurf) }
96
+ },
97
+ double(:to_hash => :nerf)
98
+ ]
99
+ expect(described_class.try_to_hash(object)).to eq(
100
+ [
101
+ {
102
+ :perf => :smurf,
103
+ :kerf => [ :plurf, 'yurf' ],
104
+ :erf => { :burf => :wurf }
105
+ },
106
+ :nerf
107
+ ]
108
+ )
109
+ end
110
+ end
111
+
112
+ describe ".encode_as_json" do
113
+ it "calls #try_to_hash on object then jsonifies" do
114
+ foo_hashed = double(:to_json => :jsonified)
115
+ allow(described_class).to receive(:try_to_hash).with(:foo).and_return(foo_hashed)
116
+ expect(described_class.encode_as_json(:foo)).to eq(:jsonified)
85
117
  end
86
118
  end
87
119
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Gadad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-14 00:00:00.000000000 Z
11
+ date: 2015-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack