ananke 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -3,25 +3,14 @@
3
3
  Ananke is a DSL that extends the functionality of Sinatra for easy creation of Restful Services and Resources:
4
4
 
5
5
  #myapp.rb
6
- require 'ananke'
7
- require 'sinatra/main' #This is only for Demo purposes
8
- #--------------------Repositories---------------------
9
- module Repository
10
- module User
11
- @data = [{:id => '1', :name => 'One'}, {:id => '2', :name => 'Two'}]
12
- def self.all
13
- @data.to_s
14
- end
15
- def self.one(id)
16
- index = @data.index{ |d| d[:id] == id}
17
- (index.nil? && '') || @data[index].to_s
18
- end
6
+ require 'sinatra/base'
7
+ require 'sinatra/ananke'
8
+
9
+ resource :user do
10
+ all do
11
+ [{:id => ]
19
12
  end
20
13
  end
21
- #-------------------REST Resources--------------------
22
- route :user do
23
- id :id
24
- end
25
14
 
26
15
  Install the gem and run with:
27
16
 
@@ -125,17 +125,27 @@ module Sinatra
125
125
  :link_self => resource_link_self?,
126
126
  :link_to => resource_link_to,
127
127
  :classes => resource_classes,
128
- :remove_empty => resource_remove_empty
128
+ :remove_empty => resource_remove_empty,
129
+ :optional => options.delete(:optional) || []
129
130
  }
130
131
  id_param = options.delete(:id)
131
- optional = options.delete(:optional) || []
132
132
  block_params = block.parameters.collect {|p| p[1]}
133
+ path_sym = path.to_s.delete(':').to_sym
133
134
  if [:get,:put,:delete].include?(type)
134
- path = "#{path}/:#{block_params[0]}" if (block_params.length == 1 && path != ":#{block_params[0]}") || id_param
135
+ path = case
136
+ when id_param
137
+ "#{path}/:#{id_param}"
138
+ when path_sym == res[:id]
139
+ ":#{block_params[0]}"
140
+ when block_params.length == 1
141
+ "#{path}/:#{block_params[0]}"
142
+ else
143
+ path
144
+ end
135
145
  end
136
146
  method(type).call "/#{resource_name}/#{path}", options, do
137
147
  inject_app(res[:classes])
138
- input_params = collect_input_params(params, optional, &block)
148
+ input_params = collect_input_params(params, res[:optional], &block)
139
149
 
140
150
  result = instance_exec(*input_params, &block)
141
151
  result = Serialize.to_h(result, :remove_empty => res[:remove_empty])
@@ -145,8 +155,8 @@ module Sinatra
145
155
  (result.class == Array && result || [result]).each do |item|
146
156
  next unless item.respond_to?(:has_key?) && item.has_key?(res[:id])
147
157
  links = []
148
- links << {:rel => :self, :href => "/#{res[:name]}/#{item[res[:id]]}"} if res[:link_self]
149
- links.concat(res[:link_to].collect { |link| {:rel => link, :href => "/#{link}/#{res[:name]}/#{item[res[:id]]}"}})
158
+ links << {:rel => :self, :action => "GET", :href => "/#{res[:name]}/#{item[res[:id]]}"} if res[:link_self]
159
+ links.concat(res[:link_to].collect { |link| {:rel => link, :action => "GET", :href => "/#{link}/#{res[:name]}/#{item[res[:id]]}"}})
150
160
  item[:links] = links unless links.empty?
151
161
  end
152
162
 
@@ -1,3 +1,3 @@
1
1
  module Ananke
2
- VERSION = "2.0.3"
2
+ VERSION = "2.0.4"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ananke
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.3
5
+ version: 2.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andries Coetzee
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-21 00:00:00 Z
13
+ date: 2011-09-23 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: colored