research_metadata_announcement 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2474614693454017d76644d6a463da347253fa8c
4
- data.tar.gz: 7fe0aba78ef40f5bc3db8c2b5a2f406ec2db7ab5
3
+ metadata.gz: 6d646820ca8f080ad370ed466c22d23ccc35c480
4
+ data.tar.gz: cdbec817ebfa40fb55c32cc46cdc99b21786e7bf
5
5
  SHA512:
6
- metadata.gz: 247bdcac312ab4ef37ede3db3f83bccb05fa00424ddcb92cca51d51fb09cd1b7fb46ed987c81e153822ca1ed3161f60732dcb79280fbfd5fdc32e29362af5a31
7
- data.tar.gz: ff3e0ab75eeb45c2cb744476f0cc5953c723d116a1cbd68dee4ecc690eee3a9d371927d01ac9627ae9f929848a4acdc560a7dc281155e95ae9e434c9f30164c1
6
+ metadata.gz: 7b5e9897a9d99d67627fe7371f6310c3169809b90b8c0d143d4df3fe362c1b6e57b0b5e521902bd8dbb429f2d7e45f0b52c1316f5d445113f0b39c77775ddbf4
7
+ data.tar.gz: 5679dbac4a6dc25b30ff422ebfbc219881e84bd340b90149024c38c8ccdf53893dab2ef308a8ab85d3a585168ff59dc651344f919f1a2d144ae0f084c8a4e367
data/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
5
  ## Unreleased
6
6
 
7
+ ## 0.3.0 - 2017-10-09
8
+ ### Changed
9
+ - Simplified API, allowing flexible permutations of announcement components.
10
+
7
11
  ## 0.2.0 - 2017-09-29
8
12
  ### Added
9
13
  - Publication.
data/README.md CHANGED
@@ -52,38 +52,70 @@ Create a metadata transformer for a Pure dataset.
52
52
  transformer = ResearchMetadataAnnouncement::Transformer::Dataset.new config
53
53
  ```
54
54
 
55
- Give it a Pure identifier and extract the metadata.
55
+ Give it a Pure identifier and get an announcement.
56
56
 
57
57
  ```ruby
58
- transformer.extract uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
58
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
59
59
  ```
60
60
 
61
- An announcement can be obtained in various formats, assuming the metadata is
62
- available and the announcement length does not exceed the optional max_length
63
- argument. Each example uses a different dataset for illustrative purposes.
61
+ Optionally, use the ```:permutation``` keyword argument to pass in an array of
62
+ components (duplicates are ignored). The order of the components determines the
63
+ order of the metadata in the announcement.
64
+
65
+ Possible components:
64
66
 
65
67
  ```ruby
68
+ :new, :title, :keywords, :hashtags, :uri
69
+ ```
70
+
71
+ Ideally, an announcement will be obtained using all the requested components,
72
+ with the following exceptions:
73
+
74
+ + If the permutation includes ```:keywords``` or ```:hashtags``` and the
75
+ resource does not have any descriptors, the metadata is simply omitted.
66
76
 
67
- transformer.title_uri
68
- #=> "Operating Nanobeams in a Quantum Fluid. dx.doi.org/10.17635/lancaster/researchdata/139."
77
+ + If the permutation includes ```:uri```, the resource must have a URI for an
78
+ announcement to be returned.
69
79
 
70
- transformer.title_uri max_length: 140
71
- #=> "Ruthenium Volatilisation from Reprocessed Spent Nuclear Fuel Studying the Baseline Therm... dx.doi.org/10.17635/lancaster/researchdata/14."
80
+ + If, after ```:title``` truncation/removal, the length still exceeds the
81
+ optional ```:max_length``` keyword argument, an announcement will not be returned.
82
+
83
+ If the permutation includes ```:new```, a phrase based upon the resource type
84
+ is generated e.g. New dataset. However, this gem does not determine whether
85
+ the resource is deemed to be new.
86
+
87
+ Each example uses a different resource for illustrative purposes.
88
+
89
+ ```ruby
72
90
 
73
- transformer.uri_title
91
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
92
+ #=> "New publication. Can poly-parameter linear-free energy relationships (pp-LFERs) improve modelling bioaccumulation in fish? #partitioncoefficients #pplfer. dx.doi.org/10.1016/j.chemosphere.2017.10.007."
93
+
94
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', max_length: 140
95
+ #=> "New publication. The Parting of Burroughs and... #americancounterculture #arthurrimbaud. dx.doi.org/10.1179/1477570013Z.00000000045."
96
+
97
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:new, :title, :hashtags] # resource has no descriptors
98
+ #=> "New publication. Deductive and inductive data collection for agent-based modelling."
99
+
100
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:new, :title, :keywords, :uri]
101
+ #=> "New publication. Torsion pairs in a triangulated category generated by a spherical object. Auslander–Reiten theory, Calabi–Yau triangulated category. dx.doi.org/10.1016/j.jalgebra.2015.09.011."
102
+
103
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:new, :title, :uri]
104
+ #=> "New dataset. Operating Nanobeams in a Quantum Fluid. dx.doi.org/10.17635/lancaster/researchdata/139."
105
+
106
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:uri, :title]
74
107
  #=> "dx.doi.org/10.17635/lancaster/researchdata/29. Herpes simplex virus 1 (HSV-1) evolution."
75
108
 
76
- transformer.keywords_uri
109
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:keywords, :uri]
77
110
  #=> "smart cities, sustainability. dx.doi.org/10.17635/lancaster/researchdata/35."
78
111
 
79
- transformer.hashtags_uri
112
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:hashtags, :uri]
80
113
  #=> "#treatedhypertension #microvascularbloodflow. dx.doi.org/10.17635/lancaster/researchdata/148."
81
114
 
82
- transformer.uri_keywords
115
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:uri, :keywords]
83
116
  #=> "dx.doi.org/10.17635/lancaster/researchdata/134. metagenomics, deep sequencing."
84
117
 
85
- transformer.uri_hashtags max_descriptors: 4
118
+ transformer.transform uuid: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', permutation: [:uri, :hashtags], max_descriptors: 4
86
119
  #=> "dx.doi.org/10.17635/lancaster/researchdata/111. #influenza #nasopharynx #virology #virus."
87
120
 
88
- ```
89
-
121
+ ```
@@ -14,86 +14,102 @@ module ResearchMetadataAnnouncement
14
14
  @config = config
15
15
  end
16
16
 
17
- # Extract metadata from Pure
18
- #
19
- # @param id [String]
20
- # @param uuid [String]
21
- def extract(uuid: nil, id: nil)
22
- if !uuid.nil?
23
- @resource = @resource_extractor.find uuid: uuid
24
- else
25
- @resource = @resource_extractor.find id: id
17
+ # @param id [String] Pure ID.
18
+ # @param uuid [String] Pure UUID.
19
+ # @param permutation [Array<Symbol>] Metadata presentation sequence e.g. [:new, :title, :hashtags, :uri].
20
+ # @param max_length [Fixnum] Maximum length of announcement.
21
+ # @param max_descriptors [Fixnum] Maximum number of descriptors (common name for keywords, tags, hashtags).
22
+ # @return [String, nil] Announcement returned if the metadata is available and the announcement length does not exceed the max_length argument.
23
+ def transform(uuid: nil, id: nil, permutation: [:new, :title, :hashtags, :uri],
24
+ max_length: nil, max_descriptors: 2)
25
+ permutation.uniq!
26
+ extract uuid: uuid, id: id
27
+ return nil unless @resource
28
+ if permutation.include? :uri
29
+ return nil unless prepare_uri
26
30
  end
27
- end
31
+ title = @resource.title
32
+ keywords = @resource.keywords
28
33
 
29
- # Title followed by uri format
30
- #
31
- # @param max_length [Fixnum]
32
- # @return [String, nil] Announcement returned if the metadata is available and the announcement length does not exceed the max_length argument.
33
- def title_uri(max_length: nil)
34
- title_formats format: :title_uri_format,
35
- max_length: max_length
36
- end
34
+ # sizing
35
+ if length_constrained? max_length
36
+ chars_needed = 0
37
+ chars_component_end = 2
38
+ permutation.each do |component|
39
+ case component
40
+ when :new
41
+ resource = self.class.name.sub('ResearchMetadataAnnouncement::Transformer::', '')
42
+ phrase = "New research #{resource.downcase}"
43
+ chars_needed += phrase.size + chars_component_end
44
+ when :title
45
+ chars_needed += title.size + chars_component_end
46
+ when :keywords
47
+ chars_needed += build_keywords(keywords, max_descriptors).size + chars_component_end if !keywords.empty?
48
+ when :hashtags
49
+ chars_needed += build_hashtags(keywords, max_descriptors).size + chars_component_end if !keywords.empty?
50
+ when :uri
51
+ uri = prepare_uri
52
+ chars_needed += uri.size if uri
53
+ end
54
+ end
37
55
 
38
- # Uri followed by title format
39
- #
40
- # @see #title_uri
41
- def uri_title(max_length: nil)
42
- title_formats format: :uri_title_format,
43
- max_length: max_length
44
- end
56
+ # determine if title needs truncating/removing before combining
57
+ if chars_needed > max_length
58
+ # truncate title
59
+ if permutation.include? :title
60
+ excess_chars = chars_needed - max_length
61
+ truncated_title_length = title.size - excess_chars
62
+ truncated_title_length = 0 if truncated_title_length < 0
63
+ title = title[0..truncated_title_length - 2].strip + '..'
64
+ permutation -= [:title] if title.size <= 5 # give up on title if just too small
65
+ end
66
+ end
67
+ end
45
68
 
46
- # Keywords followed by uri format
47
- #
48
- # @param max_length [Fixnum] Maximum length of announcement.
49
- # @param max_descriptors [Fixnum] Maximum number of descriptors (common name for keywords, tags, hashtags).
50
- # @return [String, nil] Announcement returned if the metadata is available and the announcement length does not exceed the max_length argument.
51
- def keywords_uri(max_length: nil, max_descriptors: 2)
52
- descriptors_formats format: :keywords_uri_format,
53
- max_length: max_length,
54
- max_descriptors: max_descriptors
55
- end
69
+ # get data for combining
70
+ buffer = []
71
+ permutation.each do |component|
72
+ case component
73
+ when :new
74
+ resource = self.class.name.sub('ResearchMetadataAnnouncement::Transformer::', '')
75
+ phrase = "New #{resource.downcase}"
76
+ buffer << phrase
77
+ when :title
78
+ buffer << title
79
+ when :keywords
80
+ buffer << build_keywords(keywords, max_descriptors) if !keywords.empty?
81
+ when :hashtags
82
+ buffer << build_hashtags(keywords, max_descriptors) if !keywords.empty?
83
+ when :uri
84
+ uri = prepare_uri
85
+ buffer << uri if uri
86
+ end
87
+ end
56
88
 
57
- # Uri followed by keywords format
58
- #
59
- # @see #keywords_uri
60
- def uri_keywords(max_length: nil, max_descriptors: 2)
61
- descriptors_formats format: :uri_keywords_format,
62
- max_length: max_length,
63
- max_descriptors: max_descriptors
64
- end
89
+ # combine, separate by period
90
+ str = buffer.join('. ')
65
91
 
66
- # Hashtags followed by uri format
67
- #
68
- # @see #keywords_uri
69
- def hashtags_uri(max_length: nil, max_descriptors: 2)
70
- descriptors_formats format: :hashtags_uri_format,
71
- max_length: max_length,
72
- max_descriptors: max_descriptors
73
- end
92
+ # make phrase ending grammatically correct
93
+ str = str.gsub('?.', '?')
94
+ str = str.gsub('!.', '!')
74
95
 
75
- # Uri followed by hashtags format
76
- #
77
- # @see #keywords_uri
78
- def uri_hashtags(max_length: nil, max_descriptors: 2)
79
- descriptors_formats format: :uri_hashtags_format,
80
- max_length: max_length,
81
- max_descriptors: max_descriptors
96
+ # terminate entire announcement
97
+ str << '.'
98
+
99
+ validate_string_length str, max_length unless str.empty?
82
100
  end
83
101
 
84
102
  private
85
103
 
86
- def append_sentence(str, str_to_append)
87
- if str_to_append && !str_to_append.empty?
88
- if str
89
- if !str.empty?
90
- "#{str}. #{str_to_append}."
91
- else
92
- "#{str_to_append}."
93
- end
94
- end
104
+ # Extract metadata from Pure
105
+ #
106
+ # @param id [String]
107
+ # @param uuid [String]
108
+ def extract(uuid: nil, id: nil)
109
+ if !uuid.nil?
110
+ @resource = @resource_extractor.find uuid: uuid
95
111
  else
96
- str
112
+ @resource = @resource_extractor.find id: id
97
113
  end
98
114
  end
99
115
 
@@ -105,30 +121,6 @@ module ResearchMetadataAnnouncement
105
121
  max_length && max_length > 0
106
122
  end
107
123
 
108
- def title_formats(format:, max_length:)
109
- return nil unless @resource
110
- uri = prepare_uri
111
- return nil unless uri
112
- title = @resource.title
113
- build_title_formats(format: format,
114
- uri: uri,
115
- title: title,
116
- max_length: max_length)
117
- end
118
-
119
- def descriptors_formats(format:, max_length:, max_descriptors:)
120
- return nil unless @resource
121
- uri = prepare_uri
122
- return nil unless uri
123
- keywords = @resource.keywords
124
- return nil if keywords.empty?
125
- build_descriptors_formats(format: format,
126
- keywords: keywords,
127
- uri: uri,
128
- max_length: max_length,
129
- max_descriptors: max_descriptors)
130
- end
131
-
132
124
  def validate_string_length(str, max_length)
133
125
  if length_constrained? max_length
134
126
  str if str.size <= max_length
@@ -137,61 +129,6 @@ module ResearchMetadataAnnouncement
137
129
  end
138
130
  end
139
131
 
140
- def build_descriptors_formats(format:, keywords:, uri:, max_length:, max_descriptors:)
141
- case format
142
- when :keywords_uri_format
143
- str = append_sentence(build_keywords(keywords, max_descriptors), uri)
144
- when :uri_keywords_format
145
- str = append_sentence(uri, build_keywords(keywords, max_descriptors))
146
- when :hashtags_uri_format
147
- str = append_sentence(build_hashtags(keywords, max_descriptors), uri)
148
- when :uri_hashtags_format
149
- str = append_sentence(uri, build_hashtags(keywords, max_descriptors))
150
- end
151
- validate_string_length str, max_length
152
- end
153
-
154
- def build_title_format(format:, title:, uri:)
155
- case format
156
- when :title_uri_format
157
- append_sentence title, uri
158
- when :uri_title_format
159
- append_sentence uri, title
160
- end
161
- end
162
-
163
- def build_title_format_truncated(format:, title:, uri:, available_chars:)
164
- truncated_title = title[0..available_chars-3].strip + '...'
165
- case format
166
- when :title_uri_format
167
- "#{truncated_title} #{uri}."
168
- when :uri_title_format
169
- "#{uri}. #{truncated_title}"
170
- end
171
- end
172
-
173
- def build_title_formats(format:, title:, uri:, max_length:)
174
- if length_constrained? max_length
175
- available_chars = max_length - (uri.size + 3)
176
- available_chars = 0 if available_chars < 0
177
- if title.size <= available_chars
178
- return build_title_format(format: format,
179
- title: title,
180
- uri: uri)
181
- end
182
- if available_chars - 3 > 0
183
- return build_title_format_truncated(format: format,
184
- title: title,
185
- uri: uri,
186
- available_chars: available_chars)
187
- end
188
- else
189
- build_title_format(format: format,
190
- title: title,
191
- uri: uri)
192
- end
193
- end
194
-
195
132
  def build_keywords(keywords, max)
196
133
  keywords[0..max - 1].join ', '
197
134
  end
@@ -1,5 +1,5 @@
1
1
  module ResearchMetadataAnnouncement
2
2
  # Semantic version number
3
3
  #
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: research_metadata_announcement
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Albin-Clark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-29 00:00:00.000000000 Z
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puree