Skip to content

Commit

Permalink
Adding more examples [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
plribeiro3000 committed Jun 15, 2013
1 parent a152deb commit 77c467c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/models/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Admin < ApiClient::Base
self.association = { :groups => 'Group' }

# Any of this fields can be called to manage rails form.
attr_accessor :id, :email, :password, :password_confirmation
attr_accessor :email, :password, :password_confirmation

# Validations will work as well
validates :email, :presence => true, :uniqueness => true
Expand Down
3 changes: 3 additions & 0 deletions examples/models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Author < ApiClient::Base
attr_accessor :name
end
5 changes: 5 additions & 0 deletions examples/models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Book < ApiClient::Base
self.association = { :author => 'Author' }

attr_accessor :name, :author_id, :publish_date
end
4 changes: 2 additions & 2 deletions examples/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class User < ApiClient::Base
self.association = { :groups => 'Group' }
self.associations = { :groups => 'Group', :books => 'Book' }

# Any of this fields can be called to manage rails form.
attr_accessor :id, :email, :password, :password_confirmation
attr_accessor :email, :password, :password_confirmation, :type

# Validations will work as well
validates :email, :presence => true, :uniqueness => true
Expand Down
42 changes: 42 additions & 0 deletions examples/scripts/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'api-client'

# Make a get requisition to http://api.example.com/users and initialize an user object for each user returned
users = User.collection

# Get the first user
user = users.first

# Set the attribute type to 'admin'
user.type = 'admin'

# Make a put requisition to http://api.example.com/users/:id to update the user attributes
user.put # or user.patch

# Iterate over the books of the given user
user.books.each do |book|
# Make a get requisition to http://api.example.com/books/:id to update the given book attributes
book.get
# Make a delete requisition to http://api.example.com/books/:id destroy the object if the publish_date is equal '1990'
book.delete if book.publish_date == '1990'
end

# Make a get requisition to http://api.example.com/authors and initialize an author object for each author returned
authors = Author.collection

# Iterate over the authors
authors.each do |author|
# Set @author to the current author if it is the one
@author = author if author.name == 'Author Name'
end

# Initialize a new object
book = Book.new

# Set the name on the book object
book.name = 'book'
# Set the author_id of the book object with the chosen author id
book.author_id = @author.id
# Set the publish date of the book object
book.publish_date = '2002'
# Make a post requisition to http://api.example.com/books to create the book with the object attributes
book.post

0 comments on commit 77c467c

Please sign in to comment.