Repository: fair-software/howfairis

Issue:
Compliance add public method to get color of the badge
Relevant code:

https://github.com/fair-software/howfairis/blob/738f2f9a0c5fc4409199af9bb189bc86b8dfaa47/howfairis/compliance.py#L78-L87

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a0bcbac..b59e214 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Added
+- Compliance.color() [#301](https://github.com/fair-software/howfairis/issues/301)
+- Compliance.badge_image_url() [#304](https://github.com/fair-software/howfairis/issues/304)
 
 ## [0.13.0] - 2021-Feb-18
 
diff --git a/howfairis/compliance.py b/howfairis/compliance.py
index ceae7bd..6725a52 100644
--- a/howfairis/compliance.py
+++ b/howfairis/compliance.py
@@ -75,8 +75,27 @@ class Compliance:
         Returns:
             Badge image link or None when format of README is unsupported.
         """
-        score = self.count(True)
+        badge_url = self.badge_image_url()
+        if readme_file_format == ReadmeFormat.RESTRUCTUREDTEXT:
+            return ".. image:: {0}\n   :target: {1}".format(badge_url, "https://fair-software.eu")
+        if readme_file_format == ReadmeFormat.MARKDOWN:
+            return "[![fair-software.eu]({0})]({1})".format(badge_url, "https://fair-software.eu")
 
+        return None
+
+    def badge_image_url(self) -> str:
+        """FAIR software badge image URL"""
+        color_string = self.color()
+        compliance_string = self.urlencode()
+        return "https://img.shields.io/badge/fair--software.eu-{0}-{1}".format(compliance_string, color_string)
+
+    def color(self) -> str:
+        """Traffic light color for badge based on compliance count
+
+        Returns:
+            CSS friendly color name
+        """
+        score = self.count(True)
         if score in [0, 1]:
             color_string = "red"
         elif score in [2, 3]:
@@ -85,14 +104,7 @@ class Compliance:
             color_string = "yellow"
         elif score == 5:
             color_string = "green"
-
-        badge_url = "https://img.shields.io/badge/fair--software.eu-{0}-{1}".format(self.urlencode(), color_string)
-        if readme_file_format == ReadmeFormat.RESTRUCTUREDTEXT:
-            return ".. image:: {0}\n   :target: {1}".format(badge_url, "https://fair-software.eu")
-        if readme_file_format == ReadmeFormat.MARKDOWN:
-            return "[![fair-software.eu]({0})]({1})".format(badge_url, "https://fair-software.eu")
-
-        return None
+        return color_string
 
     def count(self, value=True) -> int:
         """Number of recommendations which are compliant or non-compliant


Test patch:
diff --git a/tests/test_compliance.py b/tests/test_compliance.py
index 542365e..2529d09 100644
--- a/tests/test_compliance.py
+++ b/tests/test_compliance.py
@@ -1,3 +1,4 @@
+import pytest
 from howfairis import Compliance
 from howfairis.readme import Readme
 from howfairis.readme_format import ReadmeFormat
@@ -9,3 +10,15 @@ def test_get_compliance():
     actual_compliance = Readme(filename="README.md", text=text, file_format=ReadmeFormat.MARKDOWN).get_compliance()
     expected_compliance = Compliance(repository=True, license_=True, registry=True, citation=True, checklist=False)
     assert actual_compliance == expected_compliance
+
+
+@pytest.mark.parametrize("compliance,expected", [
+    (Compliance(True, True, True, True, True), "green"),
+    (Compliance(True, True, True, True, False), "yellow"),
+    (Compliance(True, True, True, False, False), "orange"),
+    (Compliance(True, True, False, False, False), "orange"),
+    (Compliance(True, False, False, False, False), "red"),
+    (Compliance(False, False, False, False, False), "red"),
+])
+def test_color(compliance, expected):
+    assert compliance.color() == expected
diff --git a/tests/test_compliance_no_args.py b/tests/test_compliance_no_args.py
index 725d15a..4fdf85a 100644
--- a/tests/test_compliance_no_args.py
+++ b/tests/test_compliance_no_args.py
@@ -57,3 +57,8 @@ class TestComplianceNoArgs(Contract):
     def test_urlencode(self, compliance_fixture):
         expected_urlstr = "%E2%97%8B%20%20%E2%97%8B%20%20%E2%97%8B%20%20%E2%97%8B%20%20%E2%97%8B"
         assert compliance_fixture.urlencode() == expected_urlstr
+
+    def test_badge_image_url(self, compliance_fixture):
+        expected = "https://img.shields.io/badge/fair--software.eu-" \
+                   "%E2%97%8B%20%20%E2%97%8B%20%20%E2%97%8B%20%20%E2%97%8B%20%20%E2%97%8B-red"
+        assert compliance_fixture.badge_image_url() == expected