cite_mapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/respond_with'
3
+ require 'cite_mapper'
4
+
5
+ class Api < Sinatra::Base
6
+ register Sinatra::RespondWith
7
+
8
+ before do
9
+ headers 'Access-Control-Allow-Origin' => '*',
10
+ 'Access-Control-Allow-Methods' => %w{ GET },
11
+ 'Access-Control-Allow-Headers' => %w{ Content-Type }
12
+ end
13
+
14
+ get '/find_cite' do
15
+ cts_urn = params[:cite]
16
+ res = mapper.find_by_cite(cts_urn)
17
+
18
+ respond_to do |f|
19
+ f.json { res.to_json }
20
+ end
21
+ end
22
+
23
+ def mapper
24
+ @mapper ||= CiteMapper.new
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ class CiteMapper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,126 @@
1
+ require "cite_mapper/version"
2
+
3
+ class CiteMapper
4
+ def initialize
5
+ @authors = {}
6
+ parse_abbr_file
7
+ end
8
+
9
+ def find_by_cite(urn_string)
10
+ cts = CtsUrn.new(urn_string)
11
+ author = @authors[cts.author]
12
+ work = author[cts.work]
13
+ Result.new(author, work, cts.section)
14
+ end
15
+
16
+ private
17
+
18
+ DATA_PATH = File.expand_path('../../data', __FILE__)
19
+
20
+ def parse_abbr_file
21
+ file = File.read(File.join(DATA_PATH, 'perseus.abb'))
22
+ file.lines.each do |line|
23
+ _, abbr, cts = line.strip.split("\t")
24
+ author, work = parse_abbr(abbr)
25
+ author_id, work_id = parse_cts(cts)
26
+
27
+ add_entry(author, work, author_id, work_id)
28
+ end
29
+ end
30
+
31
+ def parse_abbr(abbr)
32
+ parts = abbr.split
33
+ author = parts.shift
34
+ work = parts.join(' ')
35
+ [author, work]
36
+ end
37
+
38
+ def parse_cts(cts)
39
+ # they all start with abo:
40
+ namespace, author_id, work_id = cts[4..-1].split(',')
41
+ ["#{namespace}#{author_id}", "#{namespace}#{work_id}"]
42
+ end
43
+
44
+ def add_author(id, name)
45
+ @authors[id] ||= Author.new(id, name)
46
+ end
47
+
48
+ def add_entry(author, work, author_id, work_id)
49
+ author = add_author(author_id, author)
50
+ author.add_work(work_id, work)
51
+ end
52
+ end
53
+
54
+ class Author
55
+ attr_reader :id, :name
56
+
57
+ def initialize(id, name)
58
+ @id = id
59
+ @name = name
60
+ @works = {}
61
+ end
62
+
63
+ def [](id)
64
+ @works[id]
65
+ end
66
+
67
+ def add_work(id, name)
68
+ @works[id] = Work.new(id, name)
69
+ end
70
+ end
71
+
72
+ class Work
73
+ attr_reader :id, :name
74
+
75
+ def initialize(id, name)
76
+ @id = id
77
+ @name = name
78
+ end
79
+ end
80
+
81
+ class CtsUrn
82
+ attr_reader :prefix, :category, :author, :work, :edition, :section
83
+
84
+ def initialize(cts_urn)
85
+ @urn = cts_urn
86
+ parse
87
+ end
88
+
89
+ private
90
+
91
+ def parse
92
+ m = @urn.match(/(?<prefix>urn:cts):(?<category>.*?):(?<author>.*?)\.(?<work>.*?)\.(?<edition>.*?):(?<section>.*)/)
93
+ m.names.each do |name|
94
+ instance_variable_set("@#{name}", m[name])
95
+ end
96
+ end
97
+ end
98
+
99
+ class Result
100
+ attr_reader :section
101
+
102
+ def initialize(author, work, section)
103
+ @author_obj = author
104
+ @work_obj = work
105
+ @section = section
106
+ end
107
+
108
+ def author
109
+ @author_obj.name
110
+ end
111
+
112
+ def work
113
+ @work_obj.name
114
+ end
115
+
116
+ def to_json
117
+ content = %i{ author work section }.map { |category| to_property(category) }
118
+ %{{ #{content.join(', ')} }}
119
+ end
120
+
121
+ private
122
+
123
+ def to_property(name)
124
+ %{"#{name}" : "#{send(name)}"}
125
+ end
126
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe CiteMapper do
4
+ it 'has a version number' do
5
+ expect(CiteMapper::VERSION).not_to be nil
6
+ end
7
+
8
+ before(:all) do
9
+ @mapper = CiteMapper.new
10
+ end
11
+
12
+ describe "#find_by_cite" do
13
+ it "returns an object with info about the standard citation" do
14
+ cite = 'urn:cts:latinLit:phi0448.phi001.perseus-grc1:6'
15
+ res = @mapper.find_by_cite(cite)
16
+ res.author.should == 'Caes.'
17
+ res.work.should == 'Gall.'
18
+ res.section.should == '6'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'cite_mapper'
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cite_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - LFDM
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra-contrib
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple mapper of cite urns to standard abbreviations
84
+ email:
85
+ - 1986gh@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - cite_mapper.gemspec
99
+ - config.ru
100
+ - data/perseus.abb
101
+ - lib/cite_mapper.rb
102
+ - lib/cite_mapper/api.rb
103
+ - lib/cite_mapper/version.rb
104
+ - spec/lib/cite_mapper_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.0
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Simple mapper of cite urns to standard abbreviations
130
+ test_files:
131
+ - spec/lib/cite_mapper_spec.rb
132
+ - spec/spec_helper.rb