sirius 0.0.1

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzNjODZjYzQyM2VhNzczYTUyYWEyMWQxMWEyNzI0ZTdkMTUwY2UxNw==
5
+ data.tar.gz: !binary |-
6
+ M2UyYWZhMjJlMDQ3ZTI0YmY4OTUyZTkxZTc5NGNmNWM1ZjAyOWRmYQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MTRkNGMwNmEwNmIyYzI0MDNkMTdmMDg3ODc3OTk4NzllYmU3NDI5MzhmNzBi
10
+ MWVmNGU2ZTkwMTYwMTcwYmU1MjlkZDQ4NjVmZWI1ODY3OTg4NjJmZjEwOGYw
11
+ NzkxYmMyYzg2YmM0NzY2ZWFlYWRlYzYxYjg5YTc4ZjhhODEyODQ=
12
+ data.tar.gz: !binary |-
13
+ ZDU4ZTFhOWI0Mjg5ZTU4OWIyZmJkM2NhZjliYWM5NTcyMzk2NDNiMmUwNzg3
14
+ ZTE2OTExM2Q3ZDlmYzBjMjBjNzIxZTc2NjJmZmY3ZDY3NDEwMzFjN2M3ZmUw
15
+ ZDAxZWIzYmZiNTU0ODNlNzM1NWZkZWY1OGI5MjYzZDg2OWRmMmI=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sirius.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 mod-pr <mod.pr[@]yandex.ru>
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.
@@ -0,0 +1,29 @@
1
+ # Sirius
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sirius'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sirius
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "run spec"
6
+ RSpec::Core::RakeTask.new :spec do |t|
7
+ end
8
+
9
+ task :default => :spec
10
+
11
+
12
+
13
+
14
+
@@ -0,0 +1,144 @@
1
+ require "sirius/version"
2
+ require "sirius/attribute"
3
+ require "sirius/boards"
4
+ require "sirius/post"
5
+ require "sirius/thread"
6
+
7
+ require "timeout"
8
+ require "json"
9
+ require "net/http"
10
+
11
+
12
+ ##
13
+ # Top level module for Sirius
14
+ #
15
+ # ==== Warning
16
+ # All methods throws Exception
17
+ # * when page not found
18
+ # * when timeout error
19
+ # * when json parse error
20
+ # * otherwise?
21
+ #
22
+ # === Wow!
23
+ #
24
+ # Possible call methods for each board
25
+ #
26
+ #
27
+ # === Example
28
+ #
29
+ # # Returns pages count
30
+ # Sirius::PR::pages # => 20
31
+ # # Returns all threads
32
+ # Sirius::PR::page # => [<Thread>...<Thread>]
33
+ # # Returns all posts from thread
34
+ # Sirius::PR::thread(1) # => [<Post>...<Posts>]
35
+ #
36
+
37
+ module Sirius
38
+ extend self
39
+
40
+ #TODO Exceptions api
41
+
42
+ ##
43
+ # Default URL for request
44
+ #
45
+ URL = "http://2ch.hk"
46
+
47
+ ##
48
+ # Default time for request
49
+ #
50
+ TIME = 4
51
+
52
+ #
53
+ # Get all threads from page.
54
+ # Return Array of threads for +board+ from +page+.
55
+ #
56
+ # ==== Attributes
57
+ #
58
+ # * +board+ - board for request
59
+ # * +page_num+ - page_num, default is `nil`[wakaba page or 0]
60
+ #
61
+ # ==== Example
62
+ # # Return all threads from 0 page for `pr`
63
+ # Sirius::page('pr') #=> [<Thread>...<Thread>]
64
+ # # For `b`, 1st page
65
+ # Sirius::page('b', 1) #=> [<Thread>...<Thread>]
66
+ #
67
+ #
68
+ def page(board, page_num = nil)
69
+ get(board, page_num)[:threads].inject([]){|m, z| m << Thread.new(z.merge({:board => board})) }
70
+ end
71
+
72
+ #TODO
73
+ #
74
+ # #page should add in CACHE page count for board.
75
+ #
76
+
77
+ #TODO
78
+ # add #pages! method for get all pages from board
79
+ # #pages get from CACHE
80
+ # #pages! http-request for return page count.
81
+
82
+ alias :threads :page
83
+
84
+ #
85
+ # Get all posts from thread
86
+ # Return Array of posts
87
+ #
88
+ # === Attributes
89
+ #
90
+ # * +board+ - board
91
+ # * +num+ - num of thread
92
+ #
93
+ # === Example
94
+ # # Return all posts for 1st thread
95
+ # Sirius::thread('pr', 1) #=> [<Post>...<Post>]
96
+ #
97
+ def thread(board, num)
98
+ get("#{board}/res", num)[:thread].inject([]){|m, z| m << Thread.new({:board => board, :posts => [z]}) }
99
+ end
100
+
101
+ #
102
+ # Get count pages for +board+
103
+ # Return Fixnum
104
+ #
105
+ # === Attributes
106
+ # * +board+ - board for request
107
+ #
108
+ # === Example
109
+ #
110
+ # # Return pages count from `pr`
111
+ # Sirius::pages('pr') #=> 20
112
+ #
113
+ def pages(board)
114
+ get(board, 1)[:pages].size
115
+ end
116
+
117
+ self::Boards::constants.each do |boards|
118
+ top = self
119
+ self::Boards::const_get(boards).each do |board|
120
+ self.const_set(board.upcase, Module.new {
121
+ %w(pages page thread).each do |method|
122
+ define_singleton_method(method) do |*args|
123
+ top.send(method, *(args << board).reverse)
124
+ end
125
+ end
126
+ })
127
+ end
128
+ end
129
+
130
+ private
131
+
132
+ def get(board, page_num='wakaba') #:nodoc:
133
+ begin
134
+ page_num = page_num == 'wakaba' || page_num.nil? ? 'wakaba' : page_num.to_i
135
+ url = URI("#{URL}/#{board}/#{page_num}.json")
136
+ response = Timeout.timeout(TIME) do
137
+ Net::HTTP.get_response(url)
138
+ end
139
+ JSON::parse(response.body, {:symbolize_names => true})
140
+ rescue Exception => e
141
+ e
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,52 @@
1
+ module Sirius
2
+ #
3
+ # === Macro
4
+ # Generate attributes with default value.
5
+ # And convert attribute to need type.
6
+ # Because json return some values as String but it must be Integer.
7
+ ##:method: +int+ as +attr_int+
8
+ ##:method: +str+ as +attr_str+
9
+ #
10
+ # Get Array attributes and default value
11
+ #
12
+ # === Example
13
+ # class A
14
+ # extend Attribute
15
+ #
16
+ # int [:a, :b, :c]
17
+ # int [:d], 10
18
+ # str [:k], "foo"
19
+ # end
20
+ #
21
+ # a = A.new
22
+ #
23
+ # a.a # => 0
24
+ # a.a = "10"
25
+ #
26
+ # a.a # => 10
27
+ # a.a.class # => Fixnum
28
+ #
29
+ # a.k # => "foo"
30
+ # a.k = "bar"
31
+ # a.k # => "bar"
32
+ #
33
+ #
34
+ module Attribute
35
+ {:int => 0, :str => ""}.each do |k, v|
36
+ method = "attr_#{k}"
37
+
38
+ define_method(method) do |attrs, default=v|
39
+ attrs.each do |a|
40
+ define_method("#{a}") do
41
+ instance_variable_get(:"@#{a}") || default
42
+ end
43
+ define_method("#{a}=") do |new_attr|
44
+ new_attr = new_attr.nil? ? default : new_attr.send("to_#{k[0]}")
45
+ instance_variable_set(:"@#{a}", new_attr)
46
+ end
47
+ end
48
+ end
49
+ alias_method k, method
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,51 @@
1
+ module Sirius
2
+ module Boards
3
+ #
4
+ # Only boards
5
+ #
6
+ # TODO
7
+ # Add hidden boards
8
+ THEMES = %w<
9
+ app au bi biz
10
+ bo br c em
11
+ ew fa fiz fl
12
+ fs ftb gd hh
13
+ hi hw me mg
14
+ mlp mo mov mu
15
+ ne pvc ph po
16
+ pr psy ra re
17
+ s sf sci sn
18
+ sp spc t tr
19
+ tv un w
20
+ web
21
+ wh
22
+ wm
23
+ >
24
+
25
+ CREATIVITY = %w<
26
+ di de diy dom
27
+ f izd mus o
28
+ pa p wp wrk
29
+ td
30
+ >
31
+
32
+ GAMES = %w<
33
+ bg cg gb mc
34
+ mmo tes vg wr
35
+ >
36
+
37
+ JAPAN = %w<
38
+ a aa fd ja ma rm to vn
39
+ >
40
+
41
+ OTHER = %w<
42
+ d b soc gif r abu int
43
+ >
44
+
45
+ ADULT = %w<
46
+ fg fur g ga
47
+ h ho hc e
48
+ fet sex fag
49
+ >
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ module Sirius
2
+ #
3
+ # All posts from threads maps on this class
4
+ #
5
+ #
6
+ class Post
7
+ extend Sirius::Attribute
8
+ int %w(width lasthit num banned size timestamp)
9
+ int %w(sticky tn_width tn_height closed)
10
+ int %w(parent height op)
11
+ str %w(thumbnail video image comment subject name date)
12
+
13
+ class << self
14
+ def parse(json) #:nodoc:
15
+ i = self.new
16
+ json.each do |key, value|
17
+ i.send("#{key}=", value)
18
+ end
19
+ i
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,54 @@
1
+ module Sirius
2
+ #
3
+ # All threads from request maps on this class
4
+ #
5
+ #
6
+ class Thread
7
+ include Enumerable
8
+
9
+ attr_accessor :image_count, :omitimages, :omit, :board, :reply_count, :posts
10
+ #
11
+ # Get Hash{:image_count => "10" ...} and generate attributes
12
+ #
13
+ def initialize(json)
14
+ json.each do |key, value|
15
+ self.send("#{key}=", value)
16
+ end
17
+ end
18
+
19
+ def posts=(json) #:nodoc:
20
+ @posts = []
21
+ json.map{|_| _.first}.each do |post|
22
+ @posts << Post.parse(post)
23
+ end
24
+ end
25
+
26
+
27
+ #
28
+ # Load thread
29
+ #
30
+ # === Summary
31
+ # When you get all threads from page, you take only limit posts from thread, but with this method you can load all posts for thread.
32
+ #
33
+ # === Example
34
+ # Api::PR::page.first.size # => 6
35
+ # # But with `load`
36
+ # Api::PR::page.first.load.size # => 100
37
+ #
38
+ def load
39
+ Api::thread(board, posts.first.num)
40
+ end
41
+
42
+ #TODO
43
+ # add +more+ method = for ajax loading posts from thread
44
+ # alias :moar :more
45
+
46
+ alias :load! :load
47
+
48
+ def each(&block) #:nodoc:
49
+ posts.each(&block)
50
+ end
51
+
52
+ alias :size :count
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module Sirius
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sirius/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sirius"
8
+ spec.version = Sirius::VERSION
9
+ spec.authors = ["mod-pr"]
10
+ spec.email = ["mod.pr@yandex.ru"]
11
+ spec.description = %q{api for 2ch.hk}
12
+ spec.summary = %q{api for 2ch.hk}
13
+ spec.homepage = "https://github.com/mod-pr/sirius"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.10"
24
+
25
+ spec.add_dependency "json", "~> 1.7.7"
26
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ class A
4
+ extend Sirius::Attribute
5
+
6
+ int [:a, :b, :c]
7
+ int [:d], 10
8
+ str [:k], "foo"
9
+ str [:z]
10
+ end
11
+
12
+ describe Sirius::Attribute do
13
+
14
+ it "generate correct attibutes" do
15
+ a = A.new
16
+ a.a.should eq 0
17
+ a.b.should eq 0
18
+ a.c.should eq 0
19
+
20
+ a.a = 10
21
+ a.a.should eq 10
22
+
23
+ a.k.should eq "foo"
24
+ a.z.should eq ""
25
+ end
26
+
27
+ end
28
+
29
+
30
+
31
+
32
+
33
+
34
+
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sirius::Post do
4
+ let(:json){{:width => "0", :closed => "0", :video => "" }}
5
+ it "parse create new post from json" do
6
+ z = Sirius::Post.parse(json)
7
+ z.class.should eq Sirius::Post
8
+ z.closed.should eq 0
9
+ z.width.should eq 0
10
+ z.video.should eq ""
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sirius do
4
+
5
+ pending "add specs for http:requests"
6
+
7
+ end
8
+
9
+
@@ -0,0 +1 @@
1
+ require "sirius"
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sirius::Thread do
4
+ let(:t){Sirius::Thread}
5
+
6
+ it "create new thread from json" do
7
+ a = t.new({:image_count => 1})
8
+ a.image_count.should eq 1
9
+ end
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sirius
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mod-pr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-23 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: '2.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.7
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.7.7
69
+ description: api for 2ch.hk
70
+ email:
71
+ - mod.pr@yandex.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/sirius.rb
83
+ - lib/sirius/attribute.rb
84
+ - lib/sirius/boards.rb
85
+ - lib/sirius/post.rb
86
+ - lib/sirius/thread.rb
87
+ - lib/sirius/version.rb
88
+ - sirius.gemspec
89
+ - spec/attribute_spec.rb
90
+ - spec/post_spec.rb
91
+ - spec/sirius_spec.rb
92
+ - spec/spec_helper.rb
93
+ - spec/thread_spec.rb
94
+ homepage: https://github.com/mod-pr/sirius
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: api for 2ch.hk
118
+ test_files:
119
+ - spec/attribute_spec.rb
120
+ - spec/post_spec.rb
121
+ - spec/sirius_spec.rb
122
+ - spec/spec_helper.rb
123
+ - spec/thread_spec.rb
124
+ has_rdoc: