diff --git a/lib/travis/api/v3/renderer/avatar_url.rb b/lib/travis/api/v3/renderer/avatar_url.rb new file mode 100644 index 00000000..5226962f --- /dev/null +++ b/lib/travis/api/v3/renderer/avatar_url.rb @@ -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 diff --git a/lib/travis/api/v3/renderer/organization.rb b/lib/travis/api/v3/renderer/organization.rb index 1916151c..483d46b3 100644 --- a/lib/travis/api/v3/renderer/organization.rb +++ b/lib/travis/api/v3/renderer/organization.rb @@ -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 diff --git a/lib/travis/api/v3/renderer/user.rb b/lib/travis/api/v3/renderer/user.rb index f6fc870f..83e1c30c 100644 --- a/lib/travis/api/v3/renderer/user.rb +++ b/lib/travis/api/v3/renderer/user.rb @@ -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 diff --git a/spec/v3/renderer/avatar_url_spec.rb b/spec/v3/renderer/avatar_url_spec.rb new file mode 100644 index 00000000..a154325c --- /dev/null +++ b/spec/v3/renderer/avatar_url_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/v3/services/account/find_spec.rb b/spec/v3/services/account/find_spec.rb index 644311a1..d637fc00 100644 --- a/spec/v3/services/account/find_spec.rb +++ b/spec/v3/services/account/find_spec.rb @@ -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 diff --git a/spec/v3/services/organization/find_spec.rb b/spec/v3/services/organization/find_spec.rb index cc3b248a..030aa172 100644 --- a/spec/v3/services/organization/find_spec.rb +++ b/spec/v3/services/organization/find_spec.rb @@ -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 diff --git a/spec/v3/services/organizations/for_current_user_spec.rb b/spec/v3/services/organizations/for_current_user_spec.rb index d4123ac5..f402e7cd 100644 --- a/spec/v3/services/organizations/for_current_user_spec.rb +++ b/spec/v3/services/organizations/for_current_user_spec.rb @@ -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 diff --git a/spec/v3/services/user/current_spec.rb b/spec/v3/services/user/current_spec.rb index e96ef24d..443671e6 100644 --- a/spec/v3/services/user/current_spec.rb +++ b/spec/v3/services/user/current_spec.rb @@ -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 }} diff --git a/spec/v3/services/user/find_spec.rb b/spec/v3/services/user/find_spec.rb index cf68fe63..30ee4800 100644 --- a/spec/v3/services/user/find_spec.rb +++ b/spec/v3/services/user/find_spec.rb @@ -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 }}