megalopolis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/lib/megalopolis/essentials.rb +29 -0
- data/lib/megalopolis/scheme.rb +145 -0
- data/lib/megalopolis/version.rb +3 -0
- data/lib/megalopolis.rb +33 -0
- data/megalopolis.gemspec +19 -0
- data/spec/megalopolis_spec.rb +51 -0
- data/spec/spec_helper.rb +17 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Oame
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# megalopolis-ruby
|
2
|
+
|
3
|
+
Megalopolis API wrapper for Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'megalopolis'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install megalopolis
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
#!/usr/bin/env ruby
|
22
|
+
# coding: utf-8
|
23
|
+
|
24
|
+
require "megalopolis"
|
25
|
+
|
26
|
+
m = Megalopolis.new("http://megalopolis-provider.com")
|
27
|
+
subject = m.get :log => 150
|
28
|
+
puts subject.first.title
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Megalopolis
|
2
|
+
module Essentials
|
3
|
+
def param_serialize(parameter, add_prefix=true)
|
4
|
+
return "" unless parameter.class == Hash
|
5
|
+
ant = Hash.new
|
6
|
+
parameter.each do |key, value|
|
7
|
+
ant[key.to_sym] = value.to_s
|
8
|
+
end
|
9
|
+
param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}
|
10
|
+
if add_prefix
|
11
|
+
param.sub!(/^&/,"?")
|
12
|
+
else
|
13
|
+
param.sub!(/^&/,"")
|
14
|
+
end
|
15
|
+
return param ? param : ""
|
16
|
+
end
|
17
|
+
|
18
|
+
def send_req(url)
|
19
|
+
uri = URI.parse(url)
|
20
|
+
|
21
|
+
Net::HTTP.version_1_2
|
22
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
23
|
+
response = http.get(uri.path, 'User-Agent' => USER_AGENT)
|
24
|
+
return response.body
|
25
|
+
end
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
class Hash2 < Hash
|
4
|
+
attr_accessor :hash
|
5
|
+
|
6
|
+
def initialize(hash)
|
7
|
+
super(hash)
|
8
|
+
@hash = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(action, *args)
|
12
|
+
return @hash[action.to_s] rescue nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def params() @hash.keys.map{|k|k.to_sym} ; end
|
16
|
+
|
17
|
+
def to_hash
|
18
|
+
@hash
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Megalopolis
|
23
|
+
class Novel
|
24
|
+
include Essentials
|
25
|
+
attr_reader :novel
|
26
|
+
attr_reader :base_url
|
27
|
+
attr_reader :log
|
28
|
+
attr_reader :id
|
29
|
+
|
30
|
+
def initialize(base_url, log, id)
|
31
|
+
@novel = fetch_novel(base_url, log, id)
|
32
|
+
@base_url = base_url
|
33
|
+
@log = log
|
34
|
+
@id = id
|
35
|
+
end
|
36
|
+
|
37
|
+
def fetch_novel(base_url, log, id)
|
38
|
+
page = send_req(File.join(base_url, log.to_s, "#{id}.json"))
|
39
|
+
novel = JSON.parse(page)
|
40
|
+
novel["url"] = URI.join(base_url, "#{log}/#{id}").to_s
|
41
|
+
return novel
|
42
|
+
end
|
43
|
+
|
44
|
+
def simple_rating(point)
|
45
|
+
get_params = param_serialize({:point => point})
|
46
|
+
uri = URI.parse(@base_url)
|
47
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
48
|
+
header = { "User-Agent" => USER_AGENT }
|
49
|
+
response = http.post(uri.path, get_param, header)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def comment(text, params={})
|
54
|
+
get_params = param_serialize(params)
|
55
|
+
uri = URI.parse(@base_url)
|
56
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
57
|
+
header = { "User-Agent" => USER_AGENT }
|
58
|
+
response = http.post(uri.path, get_param, header)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def plain
|
63
|
+
return self.body.gsub(/(<br>|\r?\n)/, "")
|
64
|
+
end
|
65
|
+
|
66
|
+
def method_missing(action, *args)
|
67
|
+
return Hash2.new(@novel[action.to_s]) rescue nil
|
68
|
+
end
|
69
|
+
|
70
|
+
def params() @novel.keys.map{|k|k.to_sym} ; end
|
71
|
+
alias_method :available_methods, :params
|
72
|
+
|
73
|
+
def to_hash
|
74
|
+
@novel
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class Index
|
79
|
+
attr_reader :base_url
|
80
|
+
attr_reader :index
|
81
|
+
|
82
|
+
def initialize(base_url, index)
|
83
|
+
@base_url = base_url
|
84
|
+
@index = index
|
85
|
+
end
|
86
|
+
|
87
|
+
def method_missing(action, *args)
|
88
|
+
return @index[action.to_s] rescue nil
|
89
|
+
end
|
90
|
+
|
91
|
+
def params() @index.keys.map{|k|k.to_sym} ; end
|
92
|
+
|
93
|
+
def to_hash
|
94
|
+
@index
|
95
|
+
end
|
96
|
+
|
97
|
+
def fetch
|
98
|
+
Novel.new(@base_url, self.log, self.id)
|
99
|
+
end
|
100
|
+
alias_method :get, :fetch
|
101
|
+
end
|
102
|
+
|
103
|
+
class Subject < Array
|
104
|
+
include Essentials
|
105
|
+
attr_reader :log
|
106
|
+
attr_reader :subject
|
107
|
+
attr_reader :base_url
|
108
|
+
|
109
|
+
def initialize(base_url, log)
|
110
|
+
@subject = fetch_subject(base_url, log)
|
111
|
+
super(subject)
|
112
|
+
@base_url = base_url
|
113
|
+
@log = log
|
114
|
+
end
|
115
|
+
|
116
|
+
def fetch_subject(base_url, log)
|
117
|
+
page = send_req(File.join(base_url, "#{log}.json"))
|
118
|
+
obj = JSON.parse(page)["entries"]
|
119
|
+
|
120
|
+
indexes = []
|
121
|
+
obj.each do |index|
|
122
|
+
index["url"] = URI.join(base_url, "#{log}/#{index["id"]}").to_s
|
123
|
+
index["log"] = index["subject"]
|
124
|
+
index.delete("subject")
|
125
|
+
indexes << Index.new(base_url, index)
|
126
|
+
end
|
127
|
+
return indexes.reverse
|
128
|
+
end
|
129
|
+
|
130
|
+
def next_page
|
131
|
+
Subject.new(@base_url, @log-1)
|
132
|
+
end
|
133
|
+
alias_method :next, :next_page
|
134
|
+
|
135
|
+
def prev_page
|
136
|
+
Subject.new(@base_url, @log+1)
|
137
|
+
end
|
138
|
+
alias_method :prev, :prev_page
|
139
|
+
|
140
|
+
def latest_log
|
141
|
+
page = send_req(File.join(base_url, "0.json"))
|
142
|
+
return JSON.parse(page)["entries"][0]["subject"]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
data/lib/megalopolis.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "uri"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.expand_path("../", __FILE__))
|
8
|
+
require "megalopolis/version"
|
9
|
+
require "megalopolis/essentials"
|
10
|
+
require "megalopolis/scheme"
|
11
|
+
|
12
|
+
class Megalopolis
|
13
|
+
attr_accessor :base_url
|
14
|
+
USER_AGENT = "Megalopolis Ruby Wrapper #{Megalopolis::VERSION}"
|
15
|
+
|
16
|
+
def initialize(base_url)
|
17
|
+
@base_url = base_url
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(args={})
|
21
|
+
args[:log] ||= 0
|
22
|
+
if args.has_key?(:key)
|
23
|
+
Novel.new(@base_url, args[:log], args[:key])
|
24
|
+
else
|
25
|
+
Subject.new(@base_url, args[:log])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
#def search(query, args={})
|
30
|
+
# page = send_req(File.join(@base_url, "search.json?query=#{query}"))
|
31
|
+
# parse_index(page)
|
32
|
+
#end
|
33
|
+
end
|
data/megalopolis.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'megalopolis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "megalopolis"
|
8
|
+
gem.version = Megalopolis::VERSION
|
9
|
+
gem.authors = ["o_ame"]
|
10
|
+
gem.email = ["oame@oameya.com"]
|
11
|
+
gem.description = %q{Megalopolis API wrapper for Ruby}
|
12
|
+
gem.summary = %q{Megalopolis API wrapper for Ruby}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "pp"
|
4
|
+
require "spec_helper"
|
5
|
+
|
6
|
+
describe Megalopolis, "が #get, :log => 100 を呼ぶ時は" do
|
7
|
+
before do
|
8
|
+
m = Megalopolis.new("http://coolier.sytes.net/sosowa/ssw_l/")
|
9
|
+
@s = m.get :log => 100
|
10
|
+
end
|
11
|
+
|
12
|
+
it "Megalopolis::Subjectを返すこと" do
|
13
|
+
@s.class.should == Megalopolis::Subject
|
14
|
+
end
|
15
|
+
|
16
|
+
it "最初のノベルはMegalopolis::Indexであること" do
|
17
|
+
@s.first.class.should == Megalopolis::Index
|
18
|
+
end
|
19
|
+
|
20
|
+
it "最初のタイトルがStringであること" do
|
21
|
+
@s.first.title.class.should == String
|
22
|
+
end
|
23
|
+
|
24
|
+
it "#next_pageがMegalopolis::Subjectを返すこと" do
|
25
|
+
@s.next_page.class.should == Megalopolis::Subject
|
26
|
+
end
|
27
|
+
|
28
|
+
it "#prev_pageがMegalopolis::Subjectを返すこと" do
|
29
|
+
@s.next_page.prev_page.class.should == Megalopolis::Subject
|
30
|
+
end
|
31
|
+
|
32
|
+
it "#latest_logがFixnumを返すこと" do
|
33
|
+
@s.latest_log.class.should == Fixnum
|
34
|
+
end
|
35
|
+
|
36
|
+
it "最初を#fetchしたらMegalopolis::Novelを返すこと" do
|
37
|
+
@s.first.fetch.class.should == Megalopolis::Novel
|
38
|
+
end
|
39
|
+
|
40
|
+
it "最初を#fetchしたMegalopolis::Novel#entry#titleがStringなこと" do
|
41
|
+
@s.first.fetch.entry.title.class.should == String
|
42
|
+
end
|
43
|
+
|
44
|
+
it "直接Novelを取得出来ること" do
|
45
|
+
log = @s.first.log
|
46
|
+
key = @s.first.key
|
47
|
+
m = Megalopolis.new("http://coolier.sytes.net/sosowa/ssw_l/")
|
48
|
+
novel = m.get :log => log, :key => key
|
49
|
+
novel.class.should == Megalopolis::Novel
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
#require "rspec"
|
4
|
+
require "megalopolis"
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
8
|
+
config.run_all_when_everything_filtered = true
|
9
|
+
config.filter_run :focus
|
10
|
+
config.color_enabled = true
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: megalopolis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- o_ame
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Megalopolis API wrapper for Ruby
|
15
|
+
email:
|
16
|
+
- oame@oameya.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/megalopolis.rb
|
28
|
+
- lib/megalopolis/essentials.rb
|
29
|
+
- lib/megalopolis/scheme.rb
|
30
|
+
- lib/megalopolis/version.rb
|
31
|
+
- megalopolis.gemspec
|
32
|
+
- spec/megalopolis_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Megalopolis API wrapper for Ruby
|
58
|
+
test_files:
|
59
|
+
- spec/megalopolis_spec.rb
|
60
|
+
- spec/spec_helper.rb
|