Skip to content

Instantly share code, notes, and snippets.

View aishek's full-sized avatar
👨‍💻

Aleksandr Borisov aishek

👨‍💻
  • Tver', Russia
View GitHub Profile
@aishek
aishek / v1.rb
Created January 20, 2015 12:20
grape error handling
# app/controllers/api/v1.rb
class Api::V1 < Grape::API
class Grape::Middleware::Error
def error_message(code, text)
{
:error => {
:code => code,
:message => text
}
@aishek
aishek / statuses_api.rb
Created January 17, 2015 18:23
grape-entity statuses api example
module Api
module V1
class StatusesAPI < Api::V1::BaseAPI
get '/' do
statuses = Status.all
detailed_statuses = DetailedStatus.all
present statuses, with: Api::V1::StatusEntity, type: :full
present detailed_statuses, with: Api::V1::StatusDetailedEntity
end
@aishek
aishek / base_entity.rb
Last active August 29, 2015 14:13
grape-entity example
# app/presenters/api/v1/base_entity.rb
module Api
module V1
module BaseEntity < Grape::Entity
format_with(:unix_timestamp) { |dt| dt.to_time.to_i }
end
end
end
@aishek
aishek / base_api.rb
Last active August 29, 2015 14:07
Gape api auth example
# app/controllers/api/v1/base_api.rb
class Api::V1::BaseAPI < Grape::API
class AccessDenied < ArgumentError; end
class InvalidToken < ArgumentError; end
Grape::Endpoint.class_eval do
def abilities
@abilities ||= Six.new
end
@aishek
aishek / place_helper.rb
Last active August 29, 2015 14:07
resource helper example
# app/helpers/api/v1/city_helper.rb
module Api::V1::CityHelper
def resource_city
@resource_city ||= ::City.find(params[:city_id] || params[:id])
end
end
# app/controllers/api/v1/cities/places_api.rb
class Api::V1::Cities::PlacesAPI < Api::V1::BaseAPI
namespace 'cities/:city_id' do
resource :places do
helpers ::Api::V1::CityHelper
params do
requires :city_id, :type => Integer
end
@aishek
aishek / cities_api.rb
Created September 29, 2014 10:27
grape api example
# app/controllers/api/v1/cities_api.rb
class Api::V1::CitiesAPI < Api::V1::BaseAPI
resource :cities do
params do
optional :term, :type => String
end
get '/' do
cities = ::City.ordered_by_name_asc
@aishek
aishek / v1.rb
Last active August 29, 2015 14:07
grape api version class example
# app/controllers/api/v1.rb
class Api::V1 < Grape::API
format :json
content_type :json, 'application/json; charset=utf-8'
version 'v1'
mount CitiesAPI
end
@aishek
aishek / api.rb
Created September 29, 2014 10:21
grape api Api class usage example
# app/controllers/api.rb
class Api < Grape::API
mount ::Api::V1
end
@aishek
aishek / git.diff
Last active August 29, 2015 14:06
No newline at end of file Git warning example
-end
+end
\ No newline at end of file