Permalink
Browse files

Update web-services topics to 1.9-style hashes (#313)

  • Loading branch information...
1 parent 9ab2524 commit ceef97951d2742c9a7bffa2bee71d9dcede31ec8 @kytrinyx kytrinyx committed Nov 17, 2012
@@ -51,10 +51,10 @@ Alternatively, you can use SSL certificates to ensure trust between the servers:
```ruby
class Person < ActiveResource::Base
self.site = "https://secure.api.people.com/"
- self.ssl_options = {:cert => OpenSSL::X509::Certificate.new(File.open(pem_file))
- :key => OpenSSL::PKey::RSA.new(File.open(pem_file)),
- :ca_path => "/path/to/OpenSSL/formatted/CA_Certs",
- :verify_mode => OpenSSL::SSL::VERIFY_PEER}
+ self.ssl_options = {cert: OpenSSL::X509::Certificate.new(File.open(pem_file))
+ key: OpenSSL::PKey::RSA.new(File.open(pem_file)),
+ ca_path: "/path/to/OpenSSL/formatted/CA_Certs",
+ verify_mode: OpenSSL::SSL::VERIFY_PEER}
end
```
@@ -76,10 +76,10 @@ New objects are created as usual and fields are defined automatically upon insta
```ruby
# Create a new person inline
-Person.create :first_name => "John", :last_name => "Doe"
+Person.create first_name: "John", last_name: "Doe"
# Create a new person then save them
-p = Person.new :first_name => "Jane", :last_name => "Doe"
+p = Person.new first_name: "Jane", last_name: "Doe"
p.save
```
### Updating with `save`
@@ -21,10 +21,10 @@ class ArticlesController < ApplicationController
def create
@article = Article.new(params[:article])
if @article.save
- redirect_to article_path(@article), :notice => "Your article was created."
+ redirect_to article_path(@article), notice: "Your article was created."
else
flash[:notice] = "Article failed to save."
- render :action => :new
+ render action: :new
end
end
#...
@@ -65,8 +65,8 @@ def index
@articles = Article.all
respond_to do |format|
format.html
- format.xml { render :xml => @articles }
- format.json { render :json => @articles }
+ format.xml { render xml: @articles }
+ format.json { render json: @articles }
end
end
```
@@ -92,10 +92,10 @@ But how is this technique used when we write a `create` action that checks objec
def create
@article = Article.new(params[:article])
if @article.save
- redirect_to article_path(@article), :notice => "Your article was created."
+ redirect_to article_path(@article), notice: "Your article was created."
else
flash[:notice] = "Article failed to save."
- render :action => :new
+ render action: :new
end
end
```
@@ -136,7 +136,7 @@ Maybe you would rather redirect to the `index` after successful creation. You ca
def create
@article = Article.new(params[:article])
flash[:notice] = @article.save ? "Your article was created." : "Article failed to save."
- respond_with @article, :location => articles_path
+ respond_with @article, location: articles_path
end
```
@@ -150,8 +150,8 @@ def create
flash[:notice] = @article.save ? "Your article was created." : "Article failed to save."
respond_with @article do |format|
format.html { @article.valid? ? redirect_to(@article) : render(:new) }
- format.json { render :json => @article }
- format.xml { render :xml => @article }
+ format.json { render json: @article }
+ format.xml { render xml: @article }
end
end
```
@@ -30,15 +30,15 @@ We open the `article.rb` model and add this method:
```ruby
def to_json
- super(:except => :editor_id)
+ super(except: :editor_id)
end
```
This method relies on the `ActiveRecord::Base` implementation of `to_json` which accepts an `:except` blacklist of attributes. It can also accept an array of keys:
```ruby
def to_json
- super(:except => [:editor_id, :updated_at])
+ super(except: [:editor_id, :updated_at])
end
```
@@ -50,7 +50,7 @@ Using a whitelist is more secure but takes more maintenance. Create a `to_json`
```ruby
def to_json
- super(:only => [:title, :body, :created_at])
+ super(only: [:title, :body, :created_at])
end
```
@@ -66,11 +66,11 @@ Option one is to define a constant and reference it twice:
WHITELIST_ATTRIBUTES = [:title, :body, :created_at]
def to_json
- super(:only => WHITELIST_ATTRIBUTES)
+ super(only: WHITELIST_ATTRIBUTES)
end
def to_xml
- super(:only => WHITELIST_ATTRIBUTES)
+ super(only: WHITELIST_ATTRIBUTES)
end
```
@@ -80,7 +80,7 @@ Option two is to use a bit of metaprogramming:
WHITELIST_ATTRIBUTES = [:title, :body, :created_at]
[:to_json, :to_xml].each do |name|
- define_method(name){ super(:only => WHITELIST_ATTRIBUTES) }
+ define_method(name){ super(only: WHITELIST_ATTRIBUTES) }
end
```
@@ -192,7 +192,7 @@ def to_json
if context == :admin
model.to_json
else
- model.to_json(:only => :title)
+ model.to_json(only: :title)
end
end
```
@@ -229,4 +229,4 @@ Success! When context is blank you see the filtered output. When the context is
* Rails Serialization: http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html
* Rails `to_json` API: http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
-* Draper decorators: https://github.com/jcasimir/draper
+* Draper decorators: https://github.com/jcasimir/draper

0 comments on commit ceef979

Please sign in to comment.