alpinist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3dc5a49e339407762a5f9eb5ca0d650512114eb
4
+ data.tar.gz: d99948b25f222c03b43508ffc9740390210eb8a3
5
+ SHA512:
6
+ metadata.gz: 015740d84d3b30f7b5a88087e0c409d5ab87ccb1ca1f882fffdf69d04b695c2bdadc6fbc57bbb2b27d842fb20667cecc817a6dc243ef576e74dc2b66e3603c40
7
+ data.tar.gz: c5da1c544c7ea63433d143385c4d398cf4462e7fe3e7c75cb60e5fe9450e1ed6e10eddd1d083441403169855072d7b7239aca7b1dcfb1cc6bf269becae8aac76
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in alpinist.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toru KAWAMURA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,152 @@
1
+ # Alpinist
2
+
3
+ ALPS (Application-Level Profile Semantics) processor
4
+
5
+ This is currently a work in progress.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'alpinist'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install alpinist
22
+
23
+ ## Usage
24
+
25
+ See the example below.
26
+
27
+ ## Example
28
+
29
+ Suppose this ALPS profile were on http://example.com/Rubygem
30
+
31
+ ```json
32
+ {
33
+ "alps": {
34
+ "version": "1.0",
35
+ "doc": {
36
+ "format": "text",
37
+ "value": "RubyGem info"
38
+ },
39
+ "descriptor": [
40
+ {
41
+ "id": "RubyGem",
42
+ "type": "semantic",
43
+ "href": "http://alps.io/schema.org/SoftwareApplication#SoftwareApplication",
44
+ "descriptor": [
45
+ {
46
+ "id": "name",
47
+ "type": "semantic",
48
+ "href": "http://alps.io/schema.org/SoftwareApplication#name"
49
+ },
50
+ {
51
+ "id": "version",
52
+ "type": "semantic",
53
+ "href": "http://alps.io/schema.org/SoftwareApplication#version"
54
+ },
55
+ {
56
+ "id": "authors",
57
+ "type": "semantic",
58
+ "href": "http://alps.io/schema.org/SoftwareApplication#author"
59
+ },
60
+ {
61
+ "id": "info",
62
+ "type": "semantic",
63
+ "href": "http://alps.io/schema.org/SoftwareApplication#description"
64
+ },
65
+ {
66
+ "id": "licenses",
67
+ "type": "semantic",
68
+ "href": "http://alps.io/schema.org/SoftwareApplication#license"
69
+ },
70
+ {
71
+ "id": "project_uri",
72
+ "type": "safe",
73
+ "href": "http://alps.io/schema.org/SoftwareApplication#url"
74
+ },
75
+ {
76
+ "id": "gem_uri",
77
+ "type": "safe",
78
+ "href": "http://alps.io/schema.org/SoftwareApplication#downloadUrl"
79
+ },
80
+ {
81
+ "id": "homepage_uri",
82
+ "type": "safe",
83
+ "href": "http://alps.io/iana/relations#related"
84
+ },
85
+ {
86
+ "id": "source_code_uri",
87
+ "type": "safe"
88
+ },
89
+ {
90
+ "id": "dependencies",
91
+ "type": "semantic",
92
+ "href": "http://alps.io/schema.org/SoftwareApplication#requirements"
93
+ }
94
+ ]
95
+ }
96
+ ]
97
+ }
98
+ }
99
+ ```
100
+
101
+ ```ruby
102
+ fetcher = Alpinist::ProfileFetcher.new
103
+ profile_url = 'http://example.com/Rubygem'
104
+ doc = fetcher.fetch(profile_url)
105
+ profile = Alpinist::Profile.new(doc, profile_url)
106
+ semantics = Alpinist::Semantics.new(profile, fetcher)
107
+ semantics.build!
108
+ semantics.print_tree # for debug
109
+ ```
110
+ ```
111
+ * http://alps.io/schema.org/Thing
112
+ +---+ http://alps.io/schema.org/CreativeWork
113
+ +---+ http://alps.io/schema.org/SoftwareApplication#SoftwareApplication
114
+ +---> http://example.com/RubyGem#RubyGem
115
+ * http://alps.io/schema.org/Thing#name
116
+ +---+ http://alps.io/schema.org/SoftwareApplication#name
117
+ +---> http://example.com/RubyGem#name
118
+ * http://alps.io/schema.org/CreativeWork#version
119
+ +---+ http://alps.io/schema.org/SoftwareApplication#version
120
+ +---> http://example.com/RubyGem#version
121
+ * http://alps.io/schema.org/CreativeWork#author
122
+ +---+ http://alps.io/schema.org/SoftwareApplication#author
123
+ +---> http://example.com/RubyGem#authors
124
+ * http://alps.io/schema.org/Thing#description
125
+ +---+ http://alps.io/schema.org/SoftwareApplication#description
126
+ +---> http://example.com/RubyGem#info
127
+ * http://example.com/RubyGem#licenses
128
+ * http://alps.io/schema.org/Thing#url
129
+ +---+ http://alps.io/schema.org/SoftwareApplication#url
130
+ +---> http://example.com/RubyGem#project_uri
131
+ * http://alps.io/schema.org/SoftwareApplication#downloadUrl
132
+ +---> http://example.com/RubyGem#gem_uri
133
+ * http://alps.io/iana/relations#related
134
+ +---> http://example.com/RubyGem#homepage_uri
135
+ * http://example.com/RubyGem#source_code_uri
136
+ * http://alps.io/schema.org/SoftwareApplication#requirements
137
+ +---> http://example.com/RubyGem#dependencies
138
+ ```
139
+
140
+ ## Development
141
+
142
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
143
+
144
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
145
+
146
+ ## Contributing
147
+
148
+ 1. Fork it ( https://github.com/[my-github-username]/alpinist/fork )
149
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
150
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
151
+ 4. Push to the branch (`git push origin my-new-feature`)
152
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'alpinist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "alpinist"
8
+ spec.version = Alpinist::VERSION
9
+ spec.authors = ["Toru KAWAMURA"]
10
+ spec.email = ["tkawa@4bit.net"]
11
+
12
+ spec.summary = %q{ALPS (Application-Level Profile Semantics) processor}
13
+ spec.description = %q{ALPS (Application-Level Profile Semantics) processor}
14
+ spec.homepage = "https://github.com/tkawa/alpinist-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "multi_xml"
23
+ spec.add_dependency "rubytree"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "alpinist"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require 'tree'
2
+ require 'alpinist/descriptor'
3
+ require 'alpinist/profile'
4
+ require 'alpinist/profile_fetcher'
5
+ require 'alpinist/semantics'
6
+ require 'alpinist/version'
7
+
8
+ module Alpinist
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,23 @@
1
+ module Alpinist
2
+ class Descriptor
3
+ attr_reader :id, :name, :type, :rt, :href, :doc, :ext
4
+ attr_accessor :reference
5
+
6
+ def initialize(id: nil, name: nil, type: nil, rt: nil, href: nil, doc: nil, ext: nil, reference: nil)
7
+ @id = id
8
+ @name = name
9
+ @type = type
10
+ @rt = rt
11
+ @href = href
12
+ @doc = doc
13
+ @ext = ext
14
+ @reference = reference
15
+ end
16
+
17
+ def inspect
18
+ display_variable_name = %w(@id @name @type @rt @href @doc @ext)
19
+ variables_inspect = display_variable_name.map{ |n| "#{n}=#{instance_variable_get(n).inspect}" }.join(' ')
20
+ "#<#{self.class}:#{__id__} #{variables_inspect}>"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,82 @@
1
+ require 'json'
2
+ require 'multi_xml'
3
+
4
+ module Alpinist
5
+ class Profile
6
+ attr_reader :root_nodes, :url
7
+
8
+ def initialize(document, url)
9
+ @document = document
10
+ @url = url
11
+ @root_nodes = []
12
+ @descriptors = {}
13
+ @insubstantial_descriptors = []
14
+ parse!
15
+ end
16
+
17
+ def find_node(id)
18
+ @root_nodes.inject(nil) do |result, root_node|
19
+ result || root_node.find { |node| node.name == id && node.content.reference.nil? }
20
+ end
21
+ end
22
+
23
+ def find_descriptor(id)
24
+ id = id.sub(/^#/, '')
25
+ @descriptors[id] || @insubstantial_descriptors.find { |descriptor| descriptor.id == id }
26
+ end
27
+
28
+ def root_descriptors
29
+ @root_nodes.map(&:content)
30
+ end
31
+
32
+ def all_descriptors
33
+ @descriptors.values
34
+ end
35
+
36
+ private
37
+
38
+ def parse!
39
+ doc =
40
+ begin
41
+ JSON.parse(@document)
42
+ rescue JSON::ParserError # TODO: improve
43
+ MultiXml.parse(@document)
44
+ end
45
+ alps = doc['alps']
46
+ raise unless alps
47
+
48
+ @root_nodes = Array.wrap(alps['descriptor']).map do |data|
49
+ define_descriptor(data)
50
+ end
51
+ end
52
+
53
+ def define_descriptor(data)
54
+ if data['id'] # substantial
55
+ id = data['id']
56
+ descriptor = Descriptor.new(id: id, name: data['name'], type: data['type'], rt: data['rt'], href: data['href'], doc: data['doc'], ext: data['ext'])
57
+ @descriptors[id] = descriptor
58
+ set_reference(id, descriptor)
59
+ else # insubstantial
60
+ url, id = data['href'].split('#')
61
+ reference = @descriptors[id] if url.empty?
62
+ descriptor = Descriptor.new(id: id, name: data['name'], type: data['type'], rt: data['rt'], href: data['href'], doc: data['doc'], ext: data['ext'], reference: reference)
63
+ @insubstantial_descriptors << descriptor
64
+ end
65
+ node = Tree::TreeNode.new(id, descriptor) # 適切なTreeに取替可能
66
+ if data['descriptor']
67
+ Array.wrap(data['descriptor']).each do |data_child|
68
+ node << define_descriptor(data_child) # FIXME: idの重複
69
+ end
70
+ end
71
+ node
72
+ end
73
+
74
+ def set_reference(id, descriptor) # 必要?
75
+ @insubstantial_descriptors.each do |insubstantial_descriptor|
76
+ if insubstantial_descriptor.reference.nil? && insubstantial_descriptor.id == id
77
+ insubstantial_descriptor.reference = descriptor
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,18 @@
1
+ require 'open-uri'
2
+ module Alpinist
3
+ class ProfileFetcher
4
+ def fetch(url, as: nil, &block)
5
+ if url.match(%r|http://alps\.io/|)
6
+ as ||= url
7
+ url = "#{url}.xml"
8
+ end
9
+ if as
10
+ puts "Fetch: #{url} as #{as}"
11
+ else
12
+ puts "Fetch: #{url}"
13
+ end
14
+ block ||= ->(sio) { sio.read }
15
+ open(url, &block)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ module Alpinist
2
+ class Semantics
3
+ def initialize(initial_profile, fetcher = ProfileFetcher.new)
4
+ @initial_profile = initial_profile
5
+ @fetcher = fetcher
6
+ @profiles = {}
7
+ @profiles[initial_profile.url] = initial_profile
8
+ @nodes = {}
9
+ end
10
+
11
+ def build!
12
+ @initial_profile.all_descriptors.each do |descriptor|
13
+ descriptor_url = "#{@initial_profile.url}##{descriptor.id}"
14
+ connect_to_parent(descriptor_url, descriptor)
15
+ end
16
+ end
17
+
18
+ def find_node(descriptor_url)
19
+ @nodes[descriptor_url]
20
+ end
21
+
22
+ def print_tree
23
+ root_nodes = @nodes.values.select { |node| node.is_root? }
24
+ root_nodes.each(&:print_tree)
25
+ end
26
+
27
+ private
28
+
29
+ def connect_to_parent(descriptor_url, descriptor)
30
+ node = (@nodes[descriptor_url] ||= Tree::TreeNode.new(descriptor_url, descriptor)) # 適切なTreeに取替可能
31
+ if parent_descriptor_url = descriptor.href
32
+ parent_descriptor = find_descriptor(parent_descriptor_url)
33
+ if parent_descriptor
34
+ parent_node = connect_to_parent(parent_descriptor_url, parent_descriptor)
35
+ parent_node << node
36
+ else
37
+ puts "Descriptor #{parent_descriptor_url} not found."
38
+ end
39
+ end
40
+ node
41
+ end
42
+
43
+ def find_descriptor(descriptor_url)
44
+ profile_url, descriptor_id = descriptor_url.split('#')
45
+ profile = (@profiles[profile_url] ||= begin
46
+ document = @fetcher.fetch(profile_url)
47
+ Profile.new(document, profile_url)
48
+ end)
49
+ if descriptor_id
50
+ profile.find_descriptor(descriptor_id)
51
+ else
52
+ profile.root_descriptors.first # idなしの参照は先頭のdescriptorを指すことにする
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Alpinist
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alpinist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Toru KAWAMURA
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_xml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubytree
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: ALPS (Application-Level Profile Semantics) processor
70
+ email:
71
+ - tkawa@4bit.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - alpinist.gemspec
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/alpinist.rb
86
+ - lib/alpinist/descriptor.rb
87
+ - lib/alpinist/profile.rb
88
+ - lib/alpinist/profile_fetcher.rb
89
+ - lib/alpinist/semantics.rb
90
+ - lib/alpinist/version.rb
91
+ homepage: https://github.com/tkawa/alpinist-ruby
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: ALPS (Application-Level Profile Semantics) processor
115
+ test_files: []