citero 1.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,203 @@
1
+ module Citero
2
+ module Outputs
3
+ class EasyBib
4
+ require 'json'
5
+ def initialize(csf)
6
+ @csf = csf.csf
7
+ end
8
+
9
+ def to_easybib
10
+ JSON.generate(create_json)
11
+ end
12
+
13
+ private
14
+
15
+ def create_json
16
+ hashes =[
17
+ bib_source(@csf["itemType"]),
18
+ item_object(@csf["itemType"]),
19
+ pubtype(@csf["itemType"]),
20
+ main,
21
+ contributors
22
+ ]
23
+ hashes.reduce({},:merge)
24
+ end
25
+
26
+ def bib_source(type)
27
+ { source: get_type(type) }
28
+ end
29
+
30
+ def item_object(type)
31
+ { "#{get_type(type)}": title }
32
+ end
33
+
34
+ def title
35
+ @title ||= { title: @csf['title'] }
36
+ end
37
+
38
+ def pubtype(type)
39
+ { pubtype: { main: get_pub_type(type) } }
40
+ end
41
+
42
+ def main()
43
+ { "#{@pub_type}": send(@pub_type.to_sym) }
44
+ end
45
+
46
+ def pubnonperiodical
47
+ start = {}
48
+ start = title if @csf['title']
49
+ start.merge(construct_json({
50
+ publisher: "publisher",
51
+ city: "place",
52
+ vol: "volume",
53
+ edition: "edition"
54
+ })).merge(errata)
55
+ end
56
+
57
+ def pubmagazine
58
+ title.merge(construct_json({vol: "volume"})).merge(errata)
59
+ end
60
+
61
+ def pubnewspaper
62
+ title.merge(construct_json({
63
+ edition: "edition",
64
+ section: "section",
65
+ city: "place"
66
+ })).merge(errata)
67
+ end
68
+
69
+ def pubjournal
70
+ title.merge(construct_json({
71
+ issue: "issue",
72
+ vol: "volume",
73
+ series: "series"})).merge(errata)
74
+ end
75
+
76
+ def pubonline
77
+ title.merge(construct_json({
78
+ inst: "institution",
79
+ year:"date",
80
+ url:"url",
81
+ dayaccessed:"accessDate"
82
+ }))
83
+ end
84
+
85
+ def pages
86
+ pages_hash = {}
87
+ if @csf["numPages"]
88
+ if @csf["firstPage"]
89
+ pages_hash = { end: @csf["firstPage"].to_i + @csf["numPages"].to_i }
90
+ end
91
+ else
92
+ if @csf["numPages"]
93
+ pages_hash = { end: @csf["numPages"].to_i }
94
+ end
95
+ end
96
+ pages_hash.merge(construct_json({ start: "firstPage"}))
97
+ end
98
+
99
+ def errata
100
+ construct_json({
101
+ year: "date",
102
+ start: "firstPage"
103
+ }).merge(pages)
104
+ end
105
+
106
+ def construct_json(hash)
107
+ hashes = []
108
+ hash.each do |key, value|
109
+ hashes << { "#{key}": @csf[value] } if @csf[value]
110
+ end
111
+ hashes.reduce({},:merge)
112
+ end
113
+
114
+ def contributors
115
+ { contributors: get_contributor_array }
116
+ end
117
+
118
+
119
+ def get_type(type)
120
+ return type_map[type.to_sym] if type_map.include?(type.to_sym)
121
+ return 'book'
122
+ end
123
+
124
+ def get_pub_type(type)
125
+ @pub_type ||= pub_type_map.include?(type.to_sym) ? pub_type_map[type.to_sym] : 'pubnonperiodical'
126
+ end
127
+
128
+ def author_contributors
129
+ ['author', 'inventor', 'contributor']
130
+ end
131
+
132
+ def editor_contributors
133
+ ['editor', 'seriesEditor']
134
+ end
135
+
136
+ def translator_contributors
137
+ ['translator']
138
+ end
139
+
140
+ def all_contributors
141
+ [author_contributors, editor_contributors, translator_contributors].flatten
142
+ end
143
+
144
+ def get_csf_contributor_array(contributor_type)
145
+ [@csf[contributor_type]].compact.flatten
146
+ end
147
+
148
+ def get_contributor_symbol(param)
149
+ return :author if author_contributors.include?(param)
150
+ return :editor if editor_contributors.include?(param)
151
+ return :translator if translator_contributors.include?(param)
152
+ end
153
+
154
+ def get_contributor_array
155
+ contributor_array = []
156
+ all_contributors.each do |contrib|
157
+ get_csf_contributor_array(contrib).each do |cont|
158
+ contributor_array << add_contributor(get_contributor_symbol(contrib), cont)
159
+ end
160
+ end
161
+ contributor_array
162
+ end
163
+
164
+ def add_contributor(contributor_symbol,raw_name)
165
+ name = Citero::Utils::NameFormatter.new(raw_name)
166
+ hashes = []
167
+ hashes << { function: contributor_symbol }
168
+ hashes << { first: name.first_name } if name.first_name
169
+ hashes << { middle: name.middle_name } if name.middle_name
170
+ hashes << { last: name.last_name } if name.last_name
171
+ hashes.reduce({},:merge)
172
+ end
173
+
174
+ def pub_type_map
175
+ @pub_type_map ||= {
176
+ magazineArticle: 'pubmagazine',
177
+ newspaperArticle: 'pubnewspaper',
178
+ journalArticle: 'pubjournal',
179
+ journal: 'pubjournal',
180
+ webpage: 'pubonline'
181
+ }
182
+ end
183
+
184
+ def type_map
185
+ @type_map ||= {
186
+ journal: 'journal',
187
+ report: 'report',
188
+ blogPost: 'blog',
189
+ bookSection: 'chapter',
190
+ conferencePaper: 'conference',
191
+ thesis: 'dissertation',
192
+ videoRecording: 'film',
193
+ journalArticle: 'journal',
194
+ magazineArticle: 'magazine',
195
+ newspaperArticle: 'newspaper',
196
+ artwork: 'painting',
197
+ computerProgram: 'software',
198
+ webpage: 'website'
199
+ }
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,199 @@
1
+ module Citero
2
+ module Outputs
3
+ class OpenUrl
4
+ require 'open-uri'
5
+ require "erb"
6
+ require "cgi"
7
+ def initialize(csf)
8
+ @csf = csf.csf
9
+ end
10
+
11
+ def to_openurl
12
+ output = ""
13
+ output_methods.each do |method_sym|
14
+ output += send(method_sym)
15
+ end
16
+ output.chop if output[-1].eql? "&"
17
+ end
18
+
19
+ private
20
+
21
+ def openurl_param(key,value, with_prefix = true, encoded = true)
22
+ output = ""
23
+ return output if value.nil?
24
+ key = "rft.#{key}" if with_prefix
25
+ if value.is_a?(Array)
26
+ value.each do |val|
27
+ val = CGI::escape(val) if encoded
28
+ output += "#{key}=#{val}&"
29
+ end
30
+ else
31
+ value = CGI::escape(value) if encoded
32
+ output += "#{key}=#{value}&"
33
+ end
34
+ output
35
+ end
36
+
37
+ def output_start
38
+ output = ""
39
+ output += openurl_param("ulr_ver", "Z39.88-2004")
40
+ output += openurl_param("ctx_ver", "Z39.88-2004")
41
+ if @csf['pnxRecordId']
42
+ output += openurl_param("rfr_id", "info:sid/primo.exlibrisgroup.com:primo-#{@csf['pnxRecordId']}", false, false)
43
+ else
44
+ output += openurl_param("rfr_id", "info:sid/libraries.nyu.edu:citero", false, false)
45
+ end
46
+ end
47
+
48
+ def output_doi
49
+ openurl_param('rft_id=info:doi/', @csf['doi'])
50
+ end
51
+
52
+ def output_isbn
53
+ openurl_param('rft_id=info:isbn/', @csf['isbn'])
54
+ end
55
+
56
+ def output_type
57
+ if type_output_map[@csf['itemType']]
58
+ str = openurl_param('rft_val_fmlt', type_output_map[@csf['itemType']], false, false)
59
+ str += openurl_param('genre', genre_output_map[@csf['itemType']]) if genre_output_map[@csf['itemType']]
60
+ str
61
+ else
62
+ openurl_param('rft_val_fmlt', "info:ofi/fmt:kev:mtx:book", false, false) + openurl_param('genre', "book")
63
+ end
64
+ end
65
+
66
+ def output_date
67
+ openurl_param('date', @csf['date'])
68
+ end
69
+
70
+ def output_title
71
+ openurl_param('title', @csf['title'])
72
+ end
73
+
74
+ def output_author
75
+ openurl_param('au', @csf['author'])
76
+ end
77
+
78
+ def output_bookTitle
79
+ openurl_param('btitle', @csf['bookTitle'])
80
+ end
81
+
82
+ def output_publicationTitle
83
+ openurl_param('jtitle', @csf['publicationTitle'])
84
+ end
85
+
86
+ def output_edition
87
+ openurl_param('edition', @csf['edition'])
88
+ end
89
+
90
+ def output_contributor
91
+ openurl_param('contributor', @csf['contributor'])
92
+ end
93
+
94
+ def output_assignee
95
+ openurl_param('assignee', @csf['assignee'])
96
+ end
97
+
98
+ def output_volume
99
+ openurl_param('volume', @csf['volume'])
100
+ end
101
+
102
+ def output_reportNumber
103
+ openurl_param('volume', @csf['reportNumber'])
104
+ end
105
+
106
+ def output_issue
107
+ openurl_param('issue', @csf['issue'])
108
+ end
109
+
110
+ def output_patentNumber
111
+ openurl_param('number', @csf['patentNumber'])
112
+ end
113
+
114
+ def output_publisher
115
+ openurl_param('publisher', @csf['publisher'])
116
+ end
117
+
118
+ def output_place
119
+ openurl_param('place', @csf['place'])
120
+ end
121
+
122
+ def output_abstractNote
123
+ openurl_param('description', @csf['abstractNote'])
124
+ end
125
+
126
+ def output_startPage
127
+ openurl_param('spage', @csf['startPage'])
128
+ end
129
+
130
+ def output_endPage
131
+ openurl_param('epage', @csf['endPage'])
132
+ end
133
+
134
+ def output_numPages
135
+ openurl_param('tpages', @csf['numPages'])
136
+ end
137
+
138
+ def output_issn
139
+ openurl_param('issn', @csf['issn'])
140
+ end
141
+
142
+ def output_tags
143
+ openurl_param('subject', @csf['tags'])
144
+ end
145
+
146
+ def type_output_map
147
+ @type_output_map ||= {
148
+ "bookSection" => "info:ofi/fmt:kev:mtx:book",
149
+ "journalArticle" => "info:ofi/fmt:kev:mtx:journal",
150
+ "thesis" => "info:ofi/fmt:kev:mtx:dissertation",
151
+ "patent" => "info:ofi/fmt:kev:mtx:patent",
152
+ "webpage" => "info:ofi/fmt:kev:mtx:dc"
153
+ }
154
+ end
155
+
156
+ def genre_output_map
157
+ @genre_output_map ||= {
158
+ "bookSection" => "bookitem",
159
+ "conferencePaper" => "conference",
160
+ "report" => "report",
161
+ "document" => "document",
162
+ "journalArticle" => "article",
163
+ "thesis" => "dissertation",
164
+ "patent" => "patent"
165
+ }
166
+ end
167
+
168
+ def output_methods
169
+ @output_methods ||= [
170
+ :output_start,
171
+ :output_doi,
172
+ :output_isbn,
173
+ :output_type,
174
+ :output_date,
175
+ :output_title,
176
+ :output_author,
177
+ :output_bookTitle,
178
+ :output_publicationTitle,
179
+ :output_edition,
180
+ :output_contributor,
181
+ :output_assignee,
182
+ :output_volume,
183
+ :output_reportNumber,
184
+ :output_issue,
185
+ :output_patentNumber,
186
+ :output_publisher,
187
+ :output_place,
188
+ :output_abstractNote,
189
+ :output_startPage,
190
+ :output_endPage,
191
+ :output_numPages,
192
+ :output_isbn,
193
+ :output_issn,
194
+ :output_tags
195
+ ]
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,52 @@
1
+ module Citero
2
+ module Outputs
3
+ class RefworksTagged < Ris
4
+ def initialize(csf)
5
+ super(csf)
6
+ @csf = csf.csf
7
+ end
8
+
9
+ def to_refworks_tagged
10
+ ris = to_ris
11
+ output = ""
12
+ ris.each_line do |line|
13
+ output = "#{output}#{ris_to_refworks_tagged(line)}"
14
+ end
15
+ output
16
+ end
17
+
18
+ private
19
+
20
+ def ris_to_refworks_tagged(line)
21
+ key,val = line.split('-',2).collect {|x| x.strip }
22
+ key = tag_map[key] if tag_map[key]
23
+ val = type_map[val] if key.eql?('RT')
24
+ "#{key} #{val}\n"
25
+ end
26
+
27
+ def tag_map
28
+ @tag_map ||= {
29
+ 'TY' => 'RT',
30
+ 'CY' => 'PP',
31
+ 'DP' => 'DS',
32
+ 'ET' => 'ED',
33
+ 'KW' => 'K1',
34
+ 'N1' => 'NO',
35
+ 'PY' => 'YR',
36
+ 'TI' => 'T1'
37
+ }
38
+ end
39
+
40
+ def type_map
41
+ @type_map ||= {
42
+ 'JOUR' => 'Journal Article',
43
+ 'THES' => 'Dissertation',
44
+ 'BOOK' => 'Book, whole',
45
+ 'RPRT' => 'Report',
46
+ 'CHAP' => 'Book, section',
47
+ 'ELEC' => 'Webpage'
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end