mona 0.2.3 → 0.4.0
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.
Potentially problematic release.
This version of mona might be problematic. Click here for more details.
- data/.document +5 -0
- data/.rspec +0 -2
- data/.travis.yml +3 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +17 -18
- data/README.md +2 -192
- data/Rakefile +46 -5
- data/VERSION +1 -0
- data/lib/mona/board.rb +50 -0
- data/lib/mona/client.rb +10 -0
- data/lib/mona/response.rb +27 -0
- data/lib/mona/thread.rb +64 -0
- data/lib/mona.rb +22 -105
- data/mona.gemspec +66 -47
- data/spec/board_spec.rb +27 -0
- data/spec/client_spec.rb +15 -0
- data/spec/mona_spec.rb +5 -0
- data/spec/response_spec.rb +39 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/test_data/dat.txt +119 -0
- data/spec/test_data/subject.txt +2 -0
- data/spec/thread_spec.rb +68 -0
- metadata +75 -219
- checksums.yaml +0 -7
- data/.ruby-version +0 -1
- data/.standard.yml +0 -3
- data/CHANGELOG.md +0 -5
- data/CODE_OF_CONDUCT.md +0 -84
- data/example/Gemfile +0 -1
- data/example/Gemfile.lock +0 -51
- data/example/Monafile.rb +0 -16
- data/example/bounded_contexts/cart/cart_app/Package.rb +0 -11
- data/example/bounded_contexts/cart/cart_app/bin/console +0 -11
- data/example/bounded_contexts/cart/cart_app/package/cart_app.rb +0 -2
- data/example/bounded_contexts/cart/cart_core/Package.rb +0 -12
- data/example/bounded_contexts/cart/cart_core/bin/console +0 -9
- data/example/bounded_contexts/cart/cart_core/package/cart_core.rb +0 -2
- data/example/bounded_contexts/cart/cart_datasets/Package.rb +0 -10
- data/example/bounded_contexts/cart/cart_datasets/bin/console +0 -9
- data/example/bounded_contexts/cart/cart_datasets/package/cart_datasets.rb +0 -2
- data/example/bounded_contexts/orders/orders_app/Package.rb +0 -12
- data/example/bounded_contexts/orders/orders_app/bin/console +0 -9
- data/example/bounded_contexts/orders/orders_app/package/orders_app.rb +0 -2
- data/example/bounded_contexts/orders/orders_core/Package.rb +0 -13
- data/example/bounded_contexts/orders/orders_core/bin/console +0 -9
- data/example/bounded_contexts/orders/orders_core/package/orders_core.rb +0 -2
- data/example/bounded_contexts/orders/orders_datasets/Package.rb +0 -11
- data/example/bounded_contexts/orders/orders_datasets/bin/console +0 -9
- data/example/bounded_contexts/orders/orders_datasets/package/orders_datasets.rb +0 -2
- data/example/bounded_contexts/orders/orders_query_api/Package.rb +0 -13
- data/example/bounded_contexts/orders/orders_query_api/bin/console +0 -9
- data/example/bounded_contexts/orders/orders_query_api/package/cart_query_api.rb +0 -2
- data/example/bounded_contexts/orders/paypal_client/Package.rb +0 -9
- data/example/bounded_contexts/orders/paypal_client/bin/console +0 -9
- data/example/bounded_contexts/orders/paypal_client/package/paypal_client.rb +0 -2
- data/example/bounded_contexts/shared/db_connection/Package.rb +0 -10
- data/example/bounded_contexts/shared/db_connection/bin/console +0 -9
- data/example/bounded_contexts/shared/db_connection/package/db_connection.rb +0 -2
- data/example/bounded_contexts/shared/test_utils/Package.rb +0 -9
- data/example/bounded_contexts/shared/test_utils/bin/console +0 -9
- data/example/bounded_contexts/shared/test_utils/package/test_utils.rb +0 -2
- data/example/bounded_contexts/storefront/storefront_app/Package.rb +0 -11
- data/example/bounded_contexts/storefront/storefront_app/bin/console +0 -9
- data/example/bounded_contexts/storefront/storefront_app/package/storefront_app.rb +0 -2
- data/example/bounded_contexts/storefront/storefront_core/Package.rb +0 -11
- data/example/bounded_contexts/storefront/storefront_core/bin/console +0 -9
- data/example/bounded_contexts/storefront/storefront_core/package/storefront_core.rb +0 -2
- data/example/bounded_contexts/storefront/storefront_datasets/Package.rb +0 -11
- data/example/bounded_contexts/storefront/storefront_datasets/bin/console +0 -9
- data/example/bounded_contexts/storefront/storefront_datasets/package/storefront_datasets.rb +0 -2
- data/example/configs/cart_app/default.yml +0 -4
- data/example/configs/cart_app/test.yml +0 -3
- data/example/configs/orders_app/default.yml +0 -4
- data/example/configs/orders_app/test.yml +0 -2
- data/exe/mona +0 -15
- data/lib/mona/config/loader.rb +0 -74
- data/lib/mona/config/manager.rb +0 -20
- data/lib/mona/config.rb +0 -23
- data/lib/mona/loaders/base.rb +0 -63
- data/lib/mona/loaders/zeitwerk.rb +0 -77
- data/lib/mona/package/builder.rb +0 -27
- data/lib/mona/package/dependency_bypasser.rb +0 -43
- data/lib/mona/package/factory.rb +0 -95
- data/lib/mona/package/find_root.rb +0 -33
- data/lib/mona/package/indexer.rb +0 -30
- data/lib/mona/package/initializer.rb +0 -46
- data/lib/mona/package/list.rb +0 -10
- data/lib/mona/package/repo.rb +0 -63
- data/lib/mona/package.rb +0 -51
- data/lib/mona/project/builder.rb +0 -20
- data/lib/mona/project/factory.rb +0 -115
- data/lib/mona/project/find_root.rb +0 -31
- data/lib/mona/project/initializer.rb +0 -26
- data/lib/mona/project.rb +0 -43
- data/lib/mona/task/manager.rb +0 -36
- data/lib/mona/task.rb +0 -21
- data/lib/mona/version.rb +0 -5
- data/sig/mona.rbs +0 -4
data/.document
ADDED
data/.rspec
CHANGED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
CHANGED
@@ -1,21 +1,20 @@
|
|
1
|
-
|
1
|
+
Copyright (c) 2011 Yamada Masaki
|
2
2
|
|
3
|
-
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
4
10
|
|
5
|
-
|
6
|
-
|
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
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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.
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,193 +1,3 @@
|
|
1
|
-
# Mona
|
1
|
+
# Mona [](https://travis-ci.org/masarakki/mona) [](https://gemnasium.com/masarakki/mona)
|
2
2
|
|
3
|
-
|
4
|
-
Next version of [Rdm gem](https://github.com/droidlabs/rdm)
|
5
|
-
|
6
|
-
## Idea
|
7
|
-
Main goal to implement the gem was to give developers an ability to keep a large codebase in the single git repository (monorepository) with easy access to:
|
8
|
-
1) require only needed dependencies
|
9
|
-
2) set up loading policies and inflection rules
|
10
|
-
3) manage configuration for different environments
|
11
|
-
4) run specific tasks on selected packages
|
12
|
-
|
13
|
-
## Example Project File Structure
|
14
|
-
```bash
|
15
|
-
├── Monafile.rb
|
16
|
-
├── Gemfile
|
17
|
-
├── Gemfile.lock
|
18
|
-
├── bounded_contexts
|
19
|
-
│ ├── orders
|
20
|
-
│ │ ├── bin
|
21
|
-
│ │ │ └── run
|
22
|
-
│ │ ├── package
|
23
|
-
│ │ │ ├── orders
|
24
|
-
│ │ │ │ └── export_orders.rb
|
25
|
-
│ │ │ │
|
26
|
-
│ │ │ └── orders.rb
|
27
|
-
│ │ │
|
28
|
-
│ │ └── Package.rb
|
29
|
-
│ │
|
30
|
-
│ ├── orders_db
|
31
|
-
│ │ ├── package
|
32
|
-
│ │ │ └── orders_db.rb
|
33
|
-
│ │ │
|
34
|
-
│ │ └── Package.rb
|
35
|
-
│ │
|
36
|
-
│ └── cart
|
37
|
-
│ ├── package
|
38
|
-
│ │ └── orders.rb
|
39
|
-
│ │
|
40
|
-
│ └── Package.rb
|
41
|
-
│
|
42
|
-
└── configs
|
43
|
-
├── orders_config
|
44
|
-
│ ├── default.yml
|
45
|
-
│ └── test.yml
|
46
|
-
│
|
47
|
-
└── api_client_config
|
48
|
-
├── default.yml
|
49
|
-
└── test.yml
|
50
|
-
```
|
51
|
-
|
52
|
-
## Package
|
53
|
-
The main Mona entity is the "package".
|
54
|
-
Package configuration is defined in Package.rb file.
|
55
|
-
Each package also have an 'entrypoint' file, which can be defined as package namespace module.
|
56
|
-
|
57
|
-
For example for orders package from project structure above entrypoint file could look the following way:
|
58
|
-
```ruby
|
59
|
-
module Orders
|
60
|
-
end
|
61
|
-
|
62
|
-
require 'some_gem'
|
63
|
-
|
64
|
-
SomeGem.example_initialization_for(__dir__)
|
65
|
-
```
|
66
|
-
|
67
|
-
You can declare dependencies rules which will determine what packages should be imported before you will initialize current one
|
68
|
-
```ruby
|
69
|
-
# ./bounded_contexts/orders/Package.rb
|
70
|
-
|
71
|
-
package do
|
72
|
-
name 'marketplace'
|
73
|
-
end
|
74
|
-
|
75
|
-
dependency do
|
76
|
-
import 'orders_db'
|
77
|
-
end
|
78
|
-
|
79
|
-
dependency :test do
|
80
|
-
import 'cart'
|
81
|
-
end
|
82
|
-
```
|
83
|
-
|
84
|
-
Having that configuration you can call the following code in your bin/run file
|
85
|
-
```bash
|
86
|
-
#!/usr/bin/env ruby
|
87
|
-
require 'bundler/setup'
|
88
|
-
require 'mona'
|
89
|
-
Mona.init_package
|
90
|
-
|
91
|
-
Orders::ExportOrders.new.call
|
92
|
-
```
|
93
|
-
|
94
|
-
Let's take a look at ```Mona.init_package``` command. That is the core feature of Mona and it does the following actions
|
95
|
-
1. Detect which package the caller file belongs to
|
96
|
-
You can also provide that file directly using 'from:' parameter
|
97
|
-
```Mona.init_package(from: __FILE__)```
|
98
|
-
or providing package name
|
99
|
-
```Mona.init_package('orders')```
|
100
|
-
2. Build dependencies tree: inside each project we have ```dependency do; end``` block which is provide us information which packages should be loaded before the current one
|
101
|
-
3. For each file in dependency tree:
|
102
|
-
* Using Project Code Loader to autoload/preload package classes and modules
|
103
|
-
* Call ```require``` for entrypoint file
|
104
|
-
|
105
|
-
## Configs
|
106
|
-
Mona Configs provides interface to declare and access key-value storage can be specified for certain environment
|
107
|
-
|
108
|
-
Instead of using ENV['SOME_VARIABLE_NAME'] in your ruby files you can set up Mona Configs and use env variables in your code like this:
|
109
|
-
```ruby
|
110
|
-
Mona.configs.api_client.some_variable_name
|
111
|
-
```
|
112
|
-
Let's define Mona Config for ```api_client```
|
113
|
-
```yml
|
114
|
-
# ./configs/api_client/default.yml
|
115
|
-
api_client:
|
116
|
-
some_variable_name: ENV['SOME_VARIABLE_NAME']
|
117
|
-
|
118
|
-
# ./configs/api_client/test.yml
|
119
|
-
api_client:
|
120
|
-
some_variable_name: ENV['SOME_VARIABLE_NAME'] + ENV['TEST_ENV_NUMBER']
|
121
|
-
```
|
122
|
-
|
123
|
-
Rdm looks up for configs in directory have been set in Monafile.rb file with ```set_configs_dir``` method
|
124
|
-
Each config has at least default.yml configuration which can be complemented with :env.yml files
|
125
|
-
Running ```Mona.init_package``` with specified :env will check if corresponding :env.yml file exists and merge it's values to the default configuration
|
126
|
-
|
127
|
-
## Project
|
128
|
-
The 2 main entities within Mona project are 'project' and 'package'
|
129
|
-
Project declaration stored in Monafile.rb and a parent directory of the file is considered as the project root
|
130
|
-
|
131
|
-
### Project Configuration File Example
|
132
|
-
|
133
|
-
```ruby
|
134
|
-
setup do |project|
|
135
|
-
# set up project.env
|
136
|
-
set_env(ENV['RUBY_ENV'] || raise('RUBY_ENV environment variable is not set'))
|
137
|
-
|
138
|
-
# set up directory to look up for project configs
|
139
|
-
set_configs_dir 'configs'
|
140
|
-
|
141
|
-
# set up directory to look up for packages
|
142
|
-
set_package_dirname 'package'
|
143
|
-
set_packages_lookup_subdir 'bounded_contexts'
|
144
|
-
|
145
|
-
# code loader configuration
|
146
|
-
set_loader :zeitwerk do
|
147
|
-
# file patterns to exclude in ruby autoloading
|
148
|
-
ignore "**/schema_migrations"
|
149
|
-
ignore "**/data_migrations"
|
150
|
-
|
151
|
-
# rules to overwrite default filename-to-class strategy
|
152
|
-
overwrite ->(kname) { kname.gsub(/Foo/) { _1.upcase } }
|
153
|
-
overwrite ->(kname) { kname.gsub('Baz', 'BazBar') }
|
154
|
-
|
155
|
-
# another way to define inflection rules
|
156
|
-
inflect "uuid_generator" => "UUIDGenerator"
|
157
|
-
end
|
158
|
-
|
159
|
-
# definition of the task to run rspec on the specific package
|
160
|
-
register_task :rspec_run, on: :package do |package, args|
|
161
|
-
Dir.chdir(package.root_path) do
|
162
|
-
require 'rspec'
|
163
|
-
|
164
|
-
status = RSpec::Core::Runner.run(args, $stderr, $stdout).to_i
|
165
|
-
|
166
|
-
exit(status)
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
```
|
171
|
-
|
172
|
-
## Tasks
|
173
|
-
Mona tasks are an easy way to declare some procedures can be called on you packages/package
|
174
|
-
|
175
|
-
### Examples
|
176
|
-
```ruby
|
177
|
-
# Monafile.rb
|
178
|
-
setup do |project|
|
179
|
-
# ...
|
180
|
-
register_task :puts_package_name, on: :package do |package, args|
|
181
|
-
puts [package.name, args].inspect
|
182
|
-
end
|
183
|
-
# ...
|
184
|
-
end
|
185
|
-
```
|
186
|
-
|
187
|
-
You can call defined task that way:
|
188
|
-
```ruby
|
189
|
-
Mona.tasks.puts_package_name(
|
190
|
-
packages: Mona.packages.filter(name: "orders"),
|
191
|
-
args: [1, 2, 3]
|
192
|
-
)
|
193
|
-
```
|
3
|
+
2ch
|
data/Rakefile
CHANGED
@@ -1,8 +1,49 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
5
13
|
|
6
|
-
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "mona"
|
18
|
+
gem.homepage = "http://github.com/masarakki/mona"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{talk 2ch}
|
21
|
+
gem.description = %Q{talk to 2ch library}
|
22
|
+
gem.email = "masaki@hisme.net"
|
23
|
+
gem.authors = ["masarakki"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
7
27
|
|
8
|
-
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "mona #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.0
|
data/lib/mona/board.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'kconv'
|
2
|
+
|
3
|
+
#
|
4
|
+
#Board of 2ch.net
|
5
|
+
#
|
6
|
+
class Mona::Board
|
7
|
+
attr_reader :host, :board
|
8
|
+
|
9
|
+
#
|
10
|
+
#=== initialize method
|
11
|
+
#
|
12
|
+
#_host_:: subdomain
|
13
|
+
#_board_:: board name
|
14
|
+
#
|
15
|
+
#==== example
|
16
|
+
#if you want to access http://hayabusa2.2ch.net/news4vip/
|
17
|
+
# Mona::Board.new('hayabusa2', 'news4vip')
|
18
|
+
def initialize(host, board)
|
19
|
+
@host = host
|
20
|
+
@board = board
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
#=== get threads
|
25
|
+
#
|
26
|
+
#array of Thread
|
27
|
+
#
|
28
|
+
def threads
|
29
|
+
@threads ||= connect.toutf8.lines.map do |line|
|
30
|
+
_, id, title, res_num = line.split(/^(\d+)\.dat<>(.+)\((\d+\))$/)
|
31
|
+
Mona::Thread.new(board: self, title: title.strip, res_num: res_num.to_i, id: id.to_i)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
#
|
38
|
+
#=== url for subject.txt
|
39
|
+
#
|
40
|
+
def dat_url
|
41
|
+
"http://#{@host}/#{@board}/subject.txt"
|
42
|
+
end
|
43
|
+
|
44
|
+
def connect
|
45
|
+
res = Mona::Client.new.get(dat_url)
|
46
|
+
if res.status == 200
|
47
|
+
res.content.toutf8
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/mona/client.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
class Mona::Client < ::HTTPClient
|
3
|
+
attr_reader :last_accessed_at
|
4
|
+
def initialize(args = {})
|
5
|
+
agent_name = args.delete(:agent_name) || 'Monazilla'
|
6
|
+
agent_name += "/#{Mona.version} (ruby-mona)"
|
7
|
+
super(agent_name: agent_name)
|
8
|
+
self.transparent_gzip_decompression = true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
class Mona::Response
|
5
|
+
attr_accessor :name, :trip, :written_at, :body
|
6
|
+
def initialize(options)
|
7
|
+
options.each do |key, value|
|
8
|
+
self.send("#{key}=", value)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def parse_line(line)
|
14
|
+
name, _, time_str, body, _ = line.to_s.split(/<>/)
|
15
|
+
name, trip = parse_name(name)
|
16
|
+
time = Time.parse(time_str + " +09:00")
|
17
|
+
body = body.strip.split(' <br> ').join("\n")
|
18
|
+
Mona::Response.new(name: name, trip: trip, written_at: time, body: body)
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_name(name)
|
22
|
+
name, trip = name.split(/ <\/b>/)
|
23
|
+
trip = trip.match(/\w+/)[0] if trip
|
24
|
+
[name, trip]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/mona/thread.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
#
|
4
|
+
#Thread of 2ch.net
|
5
|
+
#
|
6
|
+
#
|
7
|
+
#
|
8
|
+
class Mona::Thread
|
9
|
+
attr_reader :title, :id, :res_num, :board, :last_accessed_at, :dat_size
|
10
|
+
|
11
|
+
#=== initialize method
|
12
|
+
#
|
13
|
+
#==== required options
|
14
|
+
#_:board_:: instance of Board
|
15
|
+
#_:id_:: thread id
|
16
|
+
#==== optionals
|
17
|
+
#_:title_:: title of thread
|
18
|
+
#_:res_num_:: number of res
|
19
|
+
#_:last_accessed_at_:: timestamp of last accessed time
|
20
|
+
#_:dat_size_:: size of known dat file
|
21
|
+
#
|
22
|
+
def initialize(args = {})
|
23
|
+
@board = args[:board]
|
24
|
+
@title = args[:title]
|
25
|
+
@id = args[:id]
|
26
|
+
@res_num = args[:res_num]
|
27
|
+
@last_accessed_at = Time.at(args[:last_accessed_at] || 0)
|
28
|
+
@dat_size = args[:dat_size] || 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.from_url(url)
|
32
|
+
matched = url.match %r{http://(.+)/test/read.cgi/(.+)/(.+)/}
|
33
|
+
raise "Invalid Url" unless matched
|
34
|
+
board = Mona::Board.new(matched[1], matched[2])
|
35
|
+
new(:board => board, :id => matched[3].to_i)
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_body(body)
|
39
|
+
first = body.lines.first.strip.split(/<>/)
|
40
|
+
@title = first[4] if first[4]
|
41
|
+
body.lines.map{ |line| Mona::Response.parse_line(line) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def reload
|
45
|
+
headers = {'If-Modified-Since' => @last_accessed_at.rfc2822, 'Range' => @dat_size}
|
46
|
+
res = Mona::Client.new.get(dat_url, :header => headers)
|
47
|
+
|
48
|
+
case res.status
|
49
|
+
when 200
|
50
|
+
@dat_size += res.body.bytesize
|
51
|
+
@last_accessed_at = Time.rfc2822(res.header["Last-Modified"].first)
|
52
|
+
body = res.body.toutf8
|
53
|
+
parse_body(body)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
#
|
59
|
+
# === url for thread dat
|
60
|
+
#
|
61
|
+
def dat_url
|
62
|
+
"http://#{board.host}/#{board.board}/dat/#{id}.dat"
|
63
|
+
end
|
64
|
+
end
|
data/lib/mona.rb
CHANGED
@@ -1,110 +1,27 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
#Mona - 2ch library.
|
2
|
+
#Copyright (C) 2011- masarakki <masaki@hisme.net>
|
3
|
+
#
|
4
|
+
#== How to access 2ch.net
|
5
|
+
#
|
6
|
+
#first, create instance of Board.
|
7
|
+
#if you want to access to hayabusa2.2ch.net/news4vip,
|
8
|
+
# board = Mona::Board.new('hayabusa2', 'news4vip')
|
9
|
+
#then you can get list of Thread by
|
10
|
+
# threads = board.threads
|
11
|
+
#or if you know thread info, you can create Thread instance
|
12
|
+
# thread = Mona::Thread.new(:board => Mona::Board.new('hayabusa2', 'news4vip'), :id => 101010101)
|
13
|
+
#
|
9
14
|
module Mona
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
module Mixins
|
16
|
-
def self.included(base)
|
17
|
-
require 'sorbet-runtime'
|
18
|
-
|
19
|
-
base.extend T::Sig
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class Container < Dry::System::Container
|
24
|
-
use :env, inferrer: -> { ENV.fetch("RUBY_ENV", :production).to_sym }
|
25
|
-
# use :logging
|
26
|
-
|
27
|
-
configure do |config|
|
28
|
-
config.root = __dir__
|
29
|
-
|
30
|
-
config.component_dirs.add 'mona' do |dir|
|
31
|
-
dir.namespaces.add nil, key: nil, const: "mona"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
Deps = Container.injector
|
37
|
-
|
38
|
-
Container.finalize! unless Container.env == :test
|
39
|
-
|
40
|
-
Zeitwerk::Loader
|
41
|
-
.for_gem
|
42
|
-
.tap(&:setup)
|
43
|
-
.tap(&:eager_load)
|
44
|
-
|
45
|
-
class << self
|
46
|
-
attr_accessor :current_project
|
47
|
-
|
48
|
-
def init_package(package_name = nil, from: caller_locations.first.path, env: nil, prevent_eager_load: false)
|
49
|
-
sync_current_project!(from) do
|
50
|
-
package_name ||= Container["package.builder"].call(from, current_project.root_path, current_project.package_dirname).name
|
51
|
-
env ||= current_project.env
|
52
|
-
|
53
|
-
Container["package.initializer"].call(package_name, env: env, prevent_eager_load: prevent_eager_load)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def root(from = caller_locations.first.path)
|
58
|
-
sync_current_project!(from) do
|
59
|
-
current_project.root_path
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def tasks(from = caller_locations.first.path)
|
64
|
-
sync_current_project!(from) do
|
65
|
-
Container["task.manager"]
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def loader(from = caller_locations.first.path)
|
70
|
-
sync_current_project!(from) do
|
71
|
-
current_project.loader
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def configs(from = caller_locations.first.path)
|
76
|
-
sync_current_project!(from) do
|
77
|
-
Container["config.manager"]
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def packages(from = caller_locations.first.path)
|
82
|
-
sync_current_project!(from) do
|
83
|
-
Container["package.repo"]
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def sync_current_project!(path, &block)
|
88
|
-
path = File.expand_path(path)
|
89
|
-
path = File.dirname(path) unless File.directory?(path)
|
90
|
-
|
91
|
-
root = Container['project.find_root'].call(path)
|
92
|
-
|
93
|
-
if @current_project && @current_project.root_path == root
|
94
|
-
return block.call
|
95
|
-
end
|
96
|
-
|
97
|
-
synchronize do
|
98
|
-
@current_project = Container["project.initializer"].call(path)
|
99
|
-
|
100
|
-
block.call
|
101
|
-
end
|
102
|
-
end
|
15
|
+
autoload :Client, 'mona/client'
|
16
|
+
autoload :Board, 'mona/board'
|
17
|
+
autoload :Thread, 'mona/thread'
|
18
|
+
autoload :Response, 'mona/response'
|
103
19
|
|
104
|
-
|
105
|
-
|
20
|
+
VERSION_MAJOR = 0
|
21
|
+
VERSION_MINOR = 1
|
22
|
+
VERSION_PATCH = 0
|
106
23
|
|
107
|
-
|
108
|
-
|
24
|
+
def self.version
|
25
|
+
[VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH].join('.')
|
109
26
|
end
|
110
27
|
end
|