altmetric_badger 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d9d1ab56412d2975c02203170f5b776d6bc5410
4
+ data.tar.gz: e5641af3af85d0ffa3f5760d660c67c471a05c05
5
+ SHA512:
6
+ metadata.gz: e4a22eddfa3448b3a5cfcc05decee2aa90dd89d7e4f53444c26fe80292452627ce0381a4428425c571d96962bf41de09f410530d65f938390d8a34224d7f2cfe
7
+ data.tar.gz: 09f99061ca3ebde114c2ff3b0d309df9bd44683b953817fa4041d22452056c85a02b11f4006f34d6827659726966ef7d39fb173e3ecf0c7d5852ea5a58954a60
data/data/sources.json ADDED
@@ -0,0 +1,82 @@
1
+ [
2
+ {
3
+ "code": "1",
4
+ "color": "#F4006E",
5
+ "key": "f1000"
6
+ },
7
+ {
8
+ "code": "?",
9
+ "color": "#DEDEDE",
10
+ "key": null
11
+ },
12
+ {
13
+ "code": "b",
14
+ "color": "#FFD140",
15
+ "key": "blogs"
16
+ },
17
+ {
18
+ "code": "d",
19
+ "color": "#9F79F2",
20
+ "key": "policy"
21
+ },
22
+ {
23
+ "code": "e",
24
+ "color": "#EFEFEF",
25
+ "key": "peer_reviews"
26
+ },
27
+ {
28
+ "code": "f",
29
+ "color": "#2445BD",
30
+ "key": "facebook"
31
+ },
32
+ {
33
+ "code": "g",
34
+ "color": "#E065BB",
35
+ "key": "googleplus"
36
+ },
37
+ {
38
+ "code": "l",
39
+ "color": "#1E90FF",
40
+ "key": "linkedin"
41
+ },
42
+ {
43
+ "code": "m",
44
+ "color": "#FF0000",
45
+ "key": "news"
46
+ },
47
+ {
48
+ "code": "p",
49
+ "color": "#CC6600",
50
+ "key": "pinterest"
51
+ },
52
+ {
53
+ "code": "q",
54
+ "color": "#DEDEDE",
55
+ "key": "q&a"
56
+ },
57
+ {
58
+ "code": "r",
59
+ "color": "#D5E8F0",
60
+ "key": "reddit"
61
+ },
62
+ {
63
+ "code": "s",
64
+ "color": "#FFB33B",
65
+ "key": "weibo"
66
+ },
67
+ {
68
+ "code": "t",
69
+ "color": "#74CFED",
70
+ "key": "twitter"
71
+ },
72
+ {
73
+ "code": "v",
74
+ "color": "#94DB5E",
75
+ "key": "video"
76
+ },
77
+ {
78
+ "code": "w",
79
+ "color": "#E7E7E7",
80
+ "key": "wikipedia"
81
+ }
82
+ ]
@@ -0,0 +1,118 @@
1
+ require 'color'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ # Wrap an Altmetric data hash and provide translation to badge URIs
6
+ class AltmetricBadger
7
+ BADGE_TYPES = %i(donut pie helix bar)
8
+ BASE_HOST = 'altmetric-badges.a.ssl.fastly.net'
9
+
10
+ class InvalidDataError < StandardError; end
11
+
12
+ def initialize(score: nil, data:)
13
+ fail InvalidDataError unless data && data.respond_to?(:sort_by)
14
+ @data = data
15
+ @score = score
16
+ end
17
+
18
+ BADGE_TYPES.each do |type|
19
+ define_method "to_#{type}_uri" do |size: 640, svg: false, show_score: true, ssl: true|
20
+ (ssl ? URI::HTTPS : URI::HTTP).build(
21
+ host: BASE_HOST,
22
+ path: '/',
23
+ query: "style=#{ type }&score=#{ score if show_score }&types=#{ type_string }&size=#{ size }#{ '&svg' if svg }"
24
+ )
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :data, :score
31
+
32
+ SEGMENT_COUNT = 8
33
+ DEFAULT_TYPE_STRING = '?' * SEGMENT_COUNT
34
+
35
+ class << self
36
+ def sources
37
+ @sources ||= JSON.parse(
38
+ File.read(
39
+ File.join(__dir__, '..', 'data', 'sources.json')
40
+ )
41
+ )
42
+ end
43
+
44
+ def sources_key_map
45
+ @sources_key_map ||= sources.map { |e| [e['key'], e] }.to_h
46
+ end
47
+
48
+ def sources_code_map
49
+ @sources_code_map ||= sources.map { |e| [e['code'], e] }.to_h
50
+ end
51
+
52
+ def key_for_code(code)
53
+ sources_code_map.fetch(code, {})['key']
54
+ end
55
+
56
+ def code_for_key(type)
57
+ sources_key_map.fetch(type, sources_key_map[nil])['code']
58
+ end
59
+
60
+ def color_for_code(code)
61
+ sources_code_map.fetch(code, sources_key_map[nil])['color']
62
+ end
63
+ end
64
+
65
+ # Return string of type codes that Altmetric badge URI uses
66
+ def type_string
67
+ return DEFAULT_TYPE_STRING unless total_posts > 0
68
+
69
+ segments_to_string(
70
+ apportion_segments(
71
+ post_percentages.keys.last(SEGMENT_COUNT)
72
+ )
73
+ )
74
+ end
75
+
76
+ # Return a hash of each post type to the percentage of posts of that type
77
+ def post_percentages
78
+ @post_percentages ||= data.sort_by { |_, v| v }.map do |type, count|
79
+ [self.class.code_for_key(type), (count / total_posts.to_f)]
80
+ end.to_h
81
+ end
82
+
83
+ # Divide segments among all post types, reflecting their proportion, and
84
+ # ensuring that the most popular SEGMENT_COUNT post types receive at least
85
+ # one segment each
86
+ def apportion_segments(segments)
87
+ (SEGMENT_COUNT - segments.length).times do
88
+ segments << post_percentages.max_by { |_, v| v }.first
89
+ post_percentages[segments.last] -= (1 / SEGMENT_COUNT.to_f)
90
+ end
91
+ segments
92
+ end
93
+
94
+ # Return the total number of posts in the data hash
95
+ def total_posts
96
+ @total_posts ||= data.values.reduce(:+) || 0
97
+ end
98
+
99
+ # Sort list of segments by apparent brightness and return combined string
100
+ def segments_to_string(segments)
101
+ segments.sort do |segment1, segment2|
102
+ color_difference_value(segment1, segment2)
103
+ end.join
104
+ end
105
+
106
+ # Calculate an absolute difference value between two colors
107
+ # This is pretty stupid and broken, but we need to keep the same behaviour
108
+ # as the Altmetric API so that the donuts don't look different.
109
+ def color_difference_value(segment1, segment2)
110
+ color1, color2 = [segment1, segment2].map do |color|
111
+ Color::RGB.from_html(self.class.color_for_code(color))
112
+ end.map(&:to_hsl)
113
+
114
+ %i(hue saturation brightness).map do |n|
115
+ color1.send(n) - color2.send(n)
116
+ end.reduce(:+)
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: altmetric_badger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Matthew MacLeod
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: color
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A gem which produces Altmetric badge URIs from a data hash
56
+ email: matt@umm.io
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - data/sources.json
62
+ - lib/altmetric_badger.rb
63
+ homepage: http://github.com/altmetric/altmetric-badger
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Altmetric Badge generator
87
+ test_files: []