-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from it3s/api
Api
- Loading branch information
Showing
15 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module API | ||
module V1 | ||
class APIController < ApplicationController | ||
|
||
before_action :authenticate_or_resquest | ||
|
||
respond_to :json, :xml | ||
|
||
def show | ||
respond_with model.find(params[:id]) | ||
end | ||
|
||
def index | ||
respond_with model.all | ||
end | ||
|
||
protected | ||
|
||
# curl -H "Authorization: Token token=usertokengoeshere" http://... | ||
def authenticate_or_resquest | ||
authenticate_or_request_with_http_token do |token, opt| | ||
User.find_by(auth_token: token).present? | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module API | ||
module V1 | ||
class GeoDataController < APIController | ||
|
||
def model; GeoData end | ||
|
||
end | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module API | ||
module V1 | ||
class MapsController < APIController | ||
|
||
def model; Map end | ||
|
||
end | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module API | ||
module V1 | ||
class UsersController < APIController | ||
|
||
def model; User end | ||
|
||
end | ||
end | ||
end |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class UserSerializer < ActiveModel::Serializer | ||
include BaseSerializer | ||
|
||
attributes :id, :name, :about_me, :language, :location, :contacts, | ||
:interests, :avatar, :created_at, :active | ||
|
||
def active | ||
object.activation_state == 'active' | ||
end | ||
|
||
def avatar | ||
object.avatar_url | ||
end | ||
|
||
end |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddAuthTokenToUsers < ActiveRecord::Migration | ||
def change | ||
change_table :users do |t| | ||
t.string :auth_token | ||
end | ||
add_index :users, :auth_token | ||
end | ||
end |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'spec_helper' | ||
|
||
describe "API::V1::GeoData" do | ||
let(:geo_data) { FactoryGirl.create :geo_data } | ||
let(:user) { FactoryGirl.create :user } | ||
|
||
describe "GET show" do | ||
describe "deny non authenticated requests" do | ||
it "401 unauthorized when auth_token is empty" do | ||
get "/api/v1/geo_data/#{geo_data.id}" | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
it "401 unauthorized when auth_token is invalid" do | ||
get "/api/v1/geo_data/#{geo_data.id}", {}, {'Authorization'=>"Token token=fake_token"} | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
end | ||
|
||
context "authenticated" do | ||
before { get "/api/v1/geo_data/#{geo_data.id}", {}, headers } | ||
|
||
context "json" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::JSON} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::JSON } | ||
end | ||
|
||
context "xml" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::XML} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::XML } | ||
end | ||
end | ||
end | ||
|
||
describe "GET index" do | ||
describe "deny non authenticated requests" do | ||
it "401 unauthorized when auth_token is empty" do | ||
get "/api/v1/geo_data" | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
it "401 unauthorized when auth_token is invalid" do | ||
get "/api/v1/geo_data", {}, {'Authorization'=>"Token token=fake_token"} | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
end | ||
|
||
context "authenticated" do | ||
before { get "/api/v1/geo_data", {}, headers } | ||
|
||
context "json" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::JSON} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::JSON } | ||
end | ||
|
||
context "xml" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::XML} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::XML } | ||
end | ||
end | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'spec_helper' | ||
|
||
describe "API::V1::Maps" do | ||
let(:map) { FactoryGirl.create :map } | ||
let(:user) { FactoryGirl.create :user } | ||
|
||
describe "GET show" do | ||
describe "deny non authenticated requests" do | ||
it "401 unauthorized when auth_token is empty" do | ||
get "/api/v1/maps/#{map.id}" | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
it "401 unauthorized when auth_token is invalid" do | ||
get "/api/v1/maps/#{map.id}", {}, {'Authorization'=>"Token token=fake_token"} | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
end | ||
|
||
context "authenticated" do | ||
before { get "/api/v1/maps/#{map.id}", {}, headers } | ||
|
||
context "json" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::JSON} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::JSON } | ||
end | ||
|
||
context "xml" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::XML} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::XML } | ||
end | ||
end | ||
end | ||
|
||
describe "GET index" do | ||
describe "deny non authenticated requests" do | ||
it "401 unauthorized when auth_token is empty" do | ||
get "/api/v1/maps" | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
it "401 unauthorized when auth_token is invalid" do | ||
get "/api/v1/maps", {}, {'Authorization'=>"Token token=fake_token"} | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
end | ||
|
||
context "authenticated" do | ||
before { get "/api/v1/maps", {}, headers } | ||
|
||
context "json" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::JSON} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::JSON } | ||
end | ||
|
||
context "xml" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::XML} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::XML } | ||
end | ||
end | ||
end | ||
end |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'spec_helper' | ||
|
||
describe "API::V1::Users" do | ||
let(:user) { FactoryGirl.create :user } | ||
|
||
describe "GET show" do | ||
describe "deny non authenticated requests" do | ||
it "401 unauthorized when auth_token is empty" do | ||
get "/api/v1/users/#{user.id}" | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
it "401 unauthorized when auth_token is invalid" do | ||
get "/api/v1/users/#{user.id}", {}, {'Authorization'=>"Token token=fake_token"} | ||
expect(response.status).to eq 401 | ||
expect(response.body).to match 'HTTP Token: Access denied.' | ||
end | ||
end | ||
|
||
context "authenticated" do | ||
before { get "/api/v1/users/#{user.id}", {}, headers } | ||
|
||
context "json" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::JSON} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::JSON } | ||
end | ||
|
||
context "xml" do | ||
let(:headers) { {'Authorization'=>"Token token=#{user.auth_token}", "Accept"=>Mime::XML} } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response.content_type).to eq Mime::XML } | ||
end | ||
end | ||
end | ||
|
||
end |