From b43ecca9060b3d7ffd84d700caecf5f35fd403a9 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Tue, 11 Apr 2017 22:34:59 -0300 Subject: Add basic representations for the Github API results --- lib/github/representation/branch.rb | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 lib/github/representation/branch.rb (limited to 'lib/github/representation/branch.rb') diff --git a/lib/github/representation/branch.rb b/lib/github/representation/branch.rb new file mode 100644 index 00000000000..7c65a948ede --- /dev/null +++ b/lib/github/representation/branch.rb @@ -0,0 +1,52 @@ +module Github + module Representation + class Branch < Representation::Base + attr_reader :repository + + def initialize(repository, raw) + @repository = repository + @raw = raw + end + + def user + raw.dig('user', 'login') || 'unknown' + end + + def repo + return @repo if defined?(@repo) + + @repo = Github::Representation::Repo.new(raw['repo']) if raw['repo'].present? + end + + def ref + raw['ref'] + end + + def sha + raw['sha'] + end + + def short_sha + Commit.truncate_sha(sha) + end + + def exists? + branch_exists? && commit_exists? + end + + def valid? + sha.present? && ref.present? + end + + private + + def branch_exists? + repository.branch_exists?(ref) + end + + def commit_exists? + repository.branch_names_contains(sha).include?(ref) + end + end + end +end -- cgit v1.2.1 From 5d106f2597bb009f0e7b8d5ed02b6f2206237e7e Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Mon, 24 Apr 2017 21:13:51 -0300 Subject: Use the base initiliazer for representations --- lib/github/representation/branch.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lib/github/representation/branch.rb') diff --git a/lib/github/representation/branch.rb b/lib/github/representation/branch.rb index 7c65a948ede..d1dac6944f0 100644 --- a/lib/github/representation/branch.rb +++ b/lib/github/representation/branch.rb @@ -3,11 +3,6 @@ module Github class Branch < Representation::Base attr_reader :repository - def initialize(repository, raw) - @repository = repository - @raw = raw - end - def user raw.dig('user', 'login') || 'unknown' end @@ -47,6 +42,10 @@ module Github def commit_exists? repository.branch_names_contains(sha).include?(ref) end + + def repository + @repository ||= options.fetch(:repository) + end end end end -- cgit v1.2.1