-
-
Save rondy/2503022 to your computer and use it in GitHub Desktop.
Revisions
-
RStankov revised this gist
Mar 11, 2012 . 3 changed files with 18 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,15 +7,15 @@ class Credential validate :user_exists, :unless => :errors? def initialize(attributes = {}) attributes.each { |k, v| set_recognized_attribute(k, v) } end def save valid? && create_device end def api_key @device.api_key end private @@ -48,5 +48,4 @@ def create_device def user_exists errors.add(:user, 'not found') unless user.present? end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ class CredentialSerializer def initialize(credential) @credential = credential end def to_json {:api_key => @credential.api_key}.to_json end def errors @credential.errors.map { |k, m| "#{k} #{m}" } end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,8 @@ class CredentialsController < ApplicationController def create credential = Credential.new(params) serializer = CredentialSerializer.new(credential) respond_with serializer end end -
reagent created this gist
Sep 28, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ class Credential include ActiveModel::Validations attr_accessor :screen_name, :oauth_token, :oauth_secret, :token, :description validates_presence_of :screen_name, :oauth_token, :oauth_secret, :message => 'required' validate :user_exists, :unless => :errors? def initialize(attributes = {}) attributes.each {|k, v| set_recognized_attribute(k, v) } end def save valid? && create_device end def as_json(options = {}) {:api_key => @device.api_key} end private def errors? errors.any? end def set_recognized_attribute(name, value) setter_method = "#{name}=" self.send(setter_method, value) if respond_to?(setter_method) end def user @user ||= User.by_screen_name(screen_name).where({ :oauth_token => oauth_token, :oauth_secret => oauth_secret }).first end def create_device @device = Device.find_or_create_by_token!({ :token => token, :description => description, :user_id => user.id }) !@device.nil? end def user_exists errors.add(:user, 'not found') unless user.present? end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ class CredentialsController < ApplicationController def create credential = Credential.new(params) if credential.save render :json => credential else render :json => {:error => errors_for(credential)}, :status => :unprocessable_entity end end private def errors_for(object) object.errors.map {|k, m| "#{k} #{m}" } end end