v3: add avatar_url

This commit is contained in:
Konstantin Haase 2015-04-15 16:22:16 +02:00
parent 23c72dfa69
commit a12240a0eb
9 changed files with 135 additions and 51 deletions

View File

@ -0,0 +1,25 @@
require 'digest/md5'
module Travis::API::V3
module Renderer::AvatarURL
GRAVATAR_URL = 'https://0.gravatar.com/avatar/%s'
private_constant :GRAVATAR_URL
extend self
def avatar_url(object = @model)
case object
when has(:avatar_url) then object.avatar_url
when has(:gravatar_url) then object.gravatar_url
when has(:gravatar_id) then GRAVATAR_URL % object.gravatar_id
when has(:email) then GRAVATAR_URL % Digest::MD5.hexdigest(object.email)
end
end
def has(field)
proc { |o| o.respond_to?(field) and o.send(field).present? }
end
private :has
end
end

View File

@ -2,7 +2,9 @@ require 'travis/api/v3/renderer/model_renderer'
module Travis::API::V3
class Renderer::Organization < Renderer::ModelRenderer
include Renderer::AvatarURL
representation(:minimal, :id, :login)
representation(:standard, :id, :login, :name, :github_id)
representation(:standard, :id, :login, :name, :github_id, :avatar_url)
end
end

View File

@ -2,7 +2,9 @@ require 'travis/api/v3/renderer/model_renderer'
module Travis::API::V3
class Renderer::User < Renderer::ModelRenderer
include Renderer::AvatarURL
representation(:minimal, :id, :login)
representation(:standard, :id, :login, :name, :github_id, :is_syncing, :synced_at)
representation(:standard, :id, :login, :name, :github_id, :avatar_url, :is_syncing, :synced_at)
end
end

View File

@ -0,0 +1,45 @@
require 'spec_helper'
describe Travis::API::V3::Renderer::AvatarURL do
let(:object) { Object.new }
subject { Travis::API::V3::Renderer::AvatarURL.avatar_url(object) }
describe 'without any useful information' do
it { should be_nil }
end
describe 'with valid avatar_url' do
let(:object) { stub('input', avatar_url: "http://foo") }
it { should be == "http://foo" }
end
describe 'with valid avatar_url' do
let(:object) { stub('input', gravatar_url: "http://foo") }
it { should be == "http://foo" }
end
describe 'with valid gravatar_id' do
let(:object) { stub('input', gravatar_id: "foo") }
it { should be == "https://0.gravatar.com/avatar/foo" }
end
describe 'with valid avatar_url and gravatar_id' do
let(:object) { stub('input', avatar_url: "http://foo", gravatar_id: "https://0.gravatar.com/avatar/foo") }
it { should be == "http://foo" }
end
describe 'with missing avatar_url and valid gravatar_id' do
let(:object) { stub('input', avatar_url: nil, gravatar_id: "foo") }
it { should be == "https://0.gravatar.com/avatar/foo" }
end
describe 'with email' do
let(:object) { stub('input', email: "foo") }
it { should be == "https://0.gravatar.com/avatar/acbd18db4cc2f85cedef654fccc4a4d8" }
end
describe 'with missing email' do
let(:object) { stub('input', email: nil) }
it { should be_nil }
end
end

View File

@ -10,12 +10,13 @@ describe Travis::API::V3::Services::Account::Find do
before { get("/v3/account/example-org") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil
}}
end
@ -23,12 +24,13 @@ describe Travis::API::V3::Services::Account::Find do
before { get("/v3/account/example-ORG") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil
}}
end
@ -40,12 +42,13 @@ describe Travis::API::V3::Services::Account::Find do
before { get("/v3/account/example-org?organization.id=#{other.id}") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil
}}
end
end
@ -59,14 +62,15 @@ describe Travis::API::V3::Services::Account::Find do
before { get("/v3/account/example-user") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "example-user",
"name" => nil,
"github_id" => nil,
"is_syncing"=> nil,
"synced_at" => nil
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "example-user",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil,
"is_syncing" => nil,
"synced_at" => nil
}}
end
@ -74,14 +78,15 @@ describe Travis::API::V3::Services::Account::Find do
before { get("/v3/account/example-USER") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "example-user",
"name" => nil,
"github_id" => nil,
"is_syncing"=> nil,
"synced_at" => nil
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "example-user",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil,
"is_syncing" => nil,
"synced_at" => nil
}}
end
@ -93,14 +98,15 @@ describe Travis::API::V3::Services::Account::Find do
before { get("/v3/account/example-user?user.id=#{other.id}") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "example-user",
"name" => nil,
"github_id" => nil,
"is_syncing"=> nil,
"synced_at" => nil
"@type" => "user",
"@href" => "/v3/user/#{user.id}",
"id" => user.id,
"login" => "example-user",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil,
"is_syncing" => nil,
"synced_at" => nil
}}
end
end

View File

@ -9,12 +9,13 @@ describe Travis::API::V3::Services::Organization::Find do
before { get("/v3/org/#{org.id}") }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
"@type" => "organization",
"@href" => "/v3/org/#{org.id}",
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil,
"avatar_url" => nil
}}
end
end

View File

@ -26,7 +26,8 @@ describe Travis::API::V3::Services::Organizations::ForCurrentUser do
"id" => org.id,
"login" => "example-org",
"name" => nil,
"github_id" => nil
"github_id" => nil,
"avatar_url" => nil
}]
}}
end

View File

@ -16,6 +16,7 @@ describe Travis::API::V3::Services::User::Current do
"login" => "svenfuchs",
"name" =>"Sven Fuchs",
"github_id" => user.github_id,
"avatar_url" => "https://0.gravatar.com/avatar/07fb84848e68b96b69022d333ca8a3e2",
"is_syncing" => user.is_syncing,
"synced_at" => user.synced_at
}}

View File

@ -16,6 +16,7 @@ describe Travis::API::V3::Services::User::Find do
"login" => "svenfuchs",
"name" =>"Sven Fuchs",
"github_id" => user.github_id,
"avatar_url" => "https://0.gravatar.com/avatar/07fb84848e68b96b69022d333ca8a3e2",
"is_syncing" => user.is_syncing,
"synced_at" => user.synced_at
}}