miby 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,68 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'yaml'
3
+ require 'base'
4
+
5
+
6
+ module Miby
7
+ class PageStructNotFoundError < StandardError
8
+ end
9
+ # ページ整形情報データベース。取得可能なページの生成方法を管理します。
10
+ #
11
+ # == ページ整形情報
12
+ # Miby::Pageでページを整形し必要な値を取得するための設定が定義されています。
13
+ # 以下のパラメータがあります(YAML形式)。
14
+ #
15
+ # name: # 定義名。Miby::Pageなどからはこの名前でアクセスする
16
+ # path: hoge.html # Miby::HTTP の http_address 以下のパス。
17
+ # params: # URLの必須パラメータ
18
+ # - id
19
+ # - owner_id
20
+ # params_opt: # 必須でないURLパラメータ
21
+ # - page
22
+ # css_selectors: # HTMLパーサ Hpricot を使ってブロックを絞り込むタグ
23
+ # - div.viewDiary
24
+ # - du.diary
25
+ # regexp: dt>(.*?)<.*dd>(.*?)<\/dd>.*?>(.*?)<\/ # 値を取得する為の正規表現。後方参照(.*?)を取得する。
26
+ # variable: # regexpで取得した値を順番に代入する
27
+ # - title
28
+ # - date
29
+ # - body
30
+ class PageStruct
31
+ def initialize(struct_file = PAGESTRUCT_FILE)
32
+ @struct_file = struct_file
33
+ @struct = load
34
+ end
35
+ # ページ情報を定義しているファイル。通常はYAMLファイル。
36
+ # 定数 PAGESTRUCT_FILE で指定されている。
37
+ attr_reader :struct_file
38
+
39
+ # 読み込んだページ情報定義ファイルの内容。
40
+ attr_reader :struct
41
+
42
+ # name で指定されたページ整形情報を返します。取得できない場合は例外が発生します。
43
+ def PageStruct.find(name)
44
+ self.new.find(name)
45
+ end
46
+ # name で指定されたページ整形情報を返します。取得できない場合は例外が発生します。
47
+ def find_by_name(name)
48
+ begin
49
+ @struct.fetch name.to_s
50
+ rescue IndexError
51
+ raise PageStructNotFoundError, "Not found struct name: #{name}."
52
+ end
53
+ end
54
+ alias find find_by_name
55
+
56
+ # ページ情報定義ファイルの内容を返します。
57
+ def inspect
58
+ @struct
59
+ end
60
+
61
+ private
62
+ def load
63
+ File.open(@struct_file, "r") do |file|
64
+ YAML::load(file.readlines.to_s)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,66 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'base'
3
+
4
+ module Miby
5
+ # mixiは一般的なWEBサイトと同様にcookieを使ってユーザーを識別しています。
6
+ # Miby::Userは認証されたユーザーのcookie情報を管理します。
7
+ # 最初にログインした時にユーザー情報設定ファイルに保存され、次の生成時からは自動的に読み込みます。
8
+ #
9
+ # == 生成
10
+ # 新しいユーザーを生成するにはnewで呼び出します。user_fileを指定するとそのファイルにユーザー情報が保存されている
11
+ # と仮定して読み込みます。
12
+ #
13
+ # デフォルトでは定数USER_FILEに指定されているファイルを読み込みます。
14
+ # user_file が読み込めない場合は未ログイン状態になります。
15
+ #
16
+ # user = User.new(user_file)
17
+ #
18
+ class User
19
+ def initialize(user_file = USER_FILE)
20
+ @user_file = user_file
21
+ load
22
+ end
23
+ # ユーザー情報設定ファイル
24
+ attr_reader :user_file
25
+
26
+ # cookiesからユーザーIDとログインに必要な情報を抽出してユーザー情報設定をファイルに保存し、設定を再読み込みします。
27
+ # cookies:: mixiから返されたcookie(Net::HTTPResponse#get_fields('Set-Cookie')の値)。
28
+ def set(cookies)
29
+ cookie = ""
30
+ cookies.each do |c|
31
+ cookie += c.gsub(" path=/", "")
32
+ end
33
+ id = cookie.match(/BF_SESSION=(\d+)/).to_a.values_at(1).to_s
34
+ save(id, cookie)
35
+ load
36
+ end
37
+ # ユーザーのログインID
38
+ attr_reader :id
39
+ attr_reader :cookie
40
+
41
+ def cookie?
42
+ @cookie.nil? ? false : true
43
+ end
44
+
45
+ private
46
+ # ユーザー情報を @user_file に保存します。
47
+ def save(id, cookie)
48
+ File.open(@user_file,"w") do |file|
49
+ file.print({ :id => id, :cookie => cookie}.to_yaml)
50
+ end
51
+ end
52
+
53
+ # ユーザー情報を @user_file から読み込みます。
54
+ def load
55
+ begin
56
+ File.open(@user_file,"r") do |file|
57
+ yaml = YAML::load(file.readlines.to_s)
58
+ @id = yaml[:id]
59
+ @cookie = yaml[:cookie]
60
+ end
61
+ rescue
62
+ # not load file - ignore
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'config'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+
7
+ class ConfigTest < Test::Unit::TestCase
8
+ def setup
9
+ @config = Miby::Config.new
10
+ end
11
+
12
+ def test_instance_variable
13
+ @config.instance_variables.each do |var|
14
+ assert_nothing_raised{ @config.__send__(var.gsub("@","")) }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'http'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+ class HTTPTest < Test::Unit::TestCase
7
+ def setup
8
+ @http = Miby::HTTP.new
9
+ end
10
+
11
+ def test_check_variables
12
+ assert_equal "127.0.0.1", @http.instance_variable_get("@http_address")
13
+ assert_equal "4000", @http.instance_variable_get("@http_port")
14
+ end
15
+
16
+ def test_connection
17
+ assert_nothing_raised do
18
+ @http.connection do |http|
19
+ response = http.get "/"
20
+ assert_equal "200", response.code
21
+ end
22
+ end # "http connection refuse. Not started http server?"
23
+ end
24
+
25
+ end
@@ -0,0 +1,27 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'miby'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+ class MibyTest < Test::Unit::TestCase
7
+ def setup
8
+ end
9
+
10
+ def test_get_page
11
+ miby = Miby::Miby.new
12
+ items = miby.friend_diary
13
+ assert_equal "香辛料。", items[0]["title"]
14
+
15
+ # page_no 2 and 3 (exist)
16
+ items = miby.friend_diary(2)
17
+ assert_equal "もも", items[0]["title"]
18
+ items = miby.friend_diary(3)
19
+ assert_equal "【日常】綴", items[0]["title"]
20
+
21
+ # page_no not exist
22
+ assert_raise(RuntimeError, "読み込めないページではエラーを発生させます。") { miby.friend_diary(100) }
23
+
24
+ # page_no not exist
25
+ assert_raise(RuntimeError, "読み込めないページではエラーを発生させます。") { miby.friend_diary("ないぺーじ") }
26
+ end
27
+ end
@@ -0,0 +1,53 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'mixi'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+
7
+ class MixiTest < Test::Unit::TestCase
8
+ def setup
9
+ @mixi = Miby::Mixi.new
10
+ @email = "hoge@hoge.com"
11
+ @password = "hogehoge"
12
+ end
13
+
14
+ def test_login
15
+ assert @mixi.login(@email, @password)
16
+ end
17
+
18
+ def test_login_refuse
19
+ assert_raise (RuntimeError) { @mixi.login(@email + "xxx", @password + "xxx") }
20
+ end
21
+
22
+ def test_login_no_email_and_password
23
+ puts "email: #{@email}"
24
+ puts "password: #{@password}"
25
+ assert @mixi.login
26
+ end
27
+
28
+ def test_get_page
29
+ check_page @mixi.friend_diary, "name", "title", "date", "id", "owner_id"
30
+ end
31
+
32
+ def test_next_page
33
+ puts "next page"
34
+ page = @mixi.friend_diary_next
35
+ puts page.class
36
+ p page.format_items[0]
37
+ puts page.format_items[0].class
38
+
39
+ page = @mixi.friend_diary(page.format_items.first)
40
+ page.to_a[0..1].each do |item|
41
+ puts "#{item["title"]} #{item["name"]} #{item["date"]}"
42
+ end
43
+ end
44
+
45
+ private
46
+ def check_page(page, *params)
47
+ assert_equal Miby::Page, page.class
48
+ assert ! page.empty?
49
+ params.each do |param|
50
+ assert_not_nil page.format_items[0][param], "#{param} is nil"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'page'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+
7
+ class PageTest < Test::Unit::TestCase
8
+ def setup
9
+ end
10
+
11
+ def test_pgae_get
12
+ page = Miby::Page.new("view_diary_diary", { "id" => 11111, "owner_id" => 12345 })
13
+ assert_equal "200", page.response.code
14
+
15
+ page.each do |item|
16
+ assert_kind_of String, item["title"]
17
+ assert_kind_of Time, item["date"]
18
+ assert_kind_of String, item["body"]
19
+ assert_nil item["body"].match(/<.*>/)
20
+ end
21
+ end
22
+
23
+ def test_page_get_not_found
24
+ assert_raise(Miby::PageStructNotFoundError) { Miby::Page.new("not_found_page_get") }
25
+ end
26
+
27
+ #def test_page_post
28
+ # page = Miby::Page.new("test_add_comment", { "diary_id" => "12345", "owner_id" => "11111", "comment_body" => "test" })
29
+ # assert "200", page.response.code
30
+ # assert ! page.empty?, "コメントの投稿が完了しませんでした。"
31
+ #end
32
+ end
@@ -0,0 +1,44 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'pagestruct'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+
7
+ class PageStructTest < Test::Unit::TestCase
8
+ def setup
9
+ @pagestruct = Miby::PageStruct.new
10
+
11
+ @found_page_name = "login" # 取得可能なページ名
12
+ @not_found_page_name = "not_found_page_name" # 取得不可能なページ名
13
+ end
14
+
15
+ def test_instance_variables
16
+ assert_not_nil @pagestruct.struct_file
17
+ assert_not_nil @pagestruct.struct
18
+ end
19
+
20
+ def test_find_by_name
21
+ assert_nothing_raised { @pagestruct.find_by_name @found_page_name }
22
+ assert_nothing_raised { @pagestruct.find @found_page_name }
23
+ assert_nothing_raised { Miby::PageStruct.find @found_page_name }
24
+ end
25
+
26
+ def test_find_by_name_not_found
27
+ assert_raise(Miby::PageStructNotFoundError) { @pagestruct.find_by_name @not_found_page_name }
28
+ assert_raise(Miby::PageStructNotFoundError) { @pagestruct.find @not_found_page_name }
29
+ assert_raise(Miby::PageStructNotFoundError) { Miby::PageStruct.find @not_found_page_name }
30
+ end
31
+
32
+ def test_struct_data
33
+ struct = @pagestruct.find_by_name @found_page_name
34
+ assert_equal "login.pl", struct["path"]
35
+ assert_equal "post", struct["method"]
36
+ assert_kind_of Array, struct["params"]
37
+ assert_kind_of String, struct["css_selectors"]
38
+ assert_kind_of String, struct["regexp"]
39
+ end
40
+
41
+ def test_inspect
42
+ assert_equal @pagestruct.struct, @pagestruct.inspect
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ module Miby
2
+ VERSION = "test"
3
+ CONFIG_FILE = File.dirname(__FILE__) + '/../etc/test_miby.conf'
4
+ USER_FILE = File.dirname(__FILE__) + '/../etc/test_user.yaml'
5
+ PAGESTRUCT_FILE = File.dirname(__FILE__) + '/../etc/test_pagestruct.yaml'
6
+ end
@@ -0,0 +1,44 @@
1
+ $:.push File.dirname(__FILE__) + "/../lib"
2
+ require 'test/unit'
3
+ require 'user'
4
+ require File.dirname(__FILE__) + "/test_setup"
5
+
6
+
7
+ class UserTest < Test::Unit::TestCase
8
+ def setup
9
+ @user = Miby::User.new
10
+ @id = "12345"
11
+ @set_cookie = ["BF_SESSION=#{@id}_ses; path=/","BF_STAMP=stamp;"]
12
+ end
13
+
14
+ def test_user_file_check
15
+ assert_not_nil @user.user_file
16
+ end
17
+
18
+ def test_set_data
19
+ assert_nothing_raised{ @user.set(@set_cookie) }
20
+ assert_equal @id, @user.id
21
+ assert_equal "BF_SESSION=12345_ses;BF_STAMP=stamp;", @user.cookie
22
+ end
23
+
24
+ def test_set_and_load_date
25
+ new_id = "9999"
26
+ set_cookie = ["BF_SESSION=#{new_id}_d471670f96fajef844122d76681f0ac7d_0; path=/","BF_STAMP=2dfc42685189712a82a7ef5d60dd6eb5;"]
27
+ assert_nothing_raised{ @user.set(set_cookie) }
28
+
29
+ # load new user
30
+ user = Miby::User.new
31
+ assert_equal new_id, user.id
32
+ end
33
+
34
+ # def test_login
35
+ # # true
36
+ # assert @user.logined?
37
+
38
+ #false
39
+ # user = Miby::User.new("/dev/null")
40
+ # assert ! user.logined?
41
+ # end
42
+
43
+
44
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: miby
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2008-01-25 00:00:00 +09:00
8
+ summary: mixi navigator for ruby
9
+ require_paths:
10
+ - lib
11
+ email: garin54@gmail.com
12
+ homepage: rubyforge.org/projects/miby
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - garin
31
+ files:
32
+ - bin/miby
33
+ - lib/user.rb
34
+ - lib/config.rb
35
+ - lib/pagestruct.rb
36
+ - lib/base.rb
37
+ - lib/mixi.rb
38
+ - lib/http.rb
39
+ - lib/page.rb
40
+ - lib/miby.rb
41
+ - etc/test_pagestruct.yaml
42
+ - etc/pagestruct.yaml
43
+ - test/test_setup.rb
44
+ - test/test_pagestruct.rb
45
+ - test/test_page.rb
46
+ - test/test_user.rb
47
+ - test/test_http.rb
48
+ - test/test_miby.rb
49
+ - test/test_config.rb
50
+ - test/test_mixi.rb
51
+ - Rakefile
52
+ - Readme.rd
53
+ test_files: []
54
+
55
+ rdoc_options: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ requirements: []
64
+
65
+ dependencies: []
66
+