ugigi 0.3.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - 1.8.7
@@ -1,3 +1,10 @@
1
+ ## 0.4
2
+
3
+ * gem非依存になりました。
4
+ * Spec追加
5
+ * list_count APIに対応
6
+ * Mylist APIに完全対応
7
+
1
8
  ## 0.3.1 - 0.3.3
2
9
  * バグ修正
3
10
 
data/README.md CHANGED
@@ -1,11 +1,10 @@
1
1
  # Ugigi
2
2
 
3
- Ugigi Wrapper for Ruby 1.9.x
3
+ Ugigi Wrapper for Ruby
4
4
 
5
5
  ## Requirements
6
6
 
7
- * Ruby 1.9.x
8
- * mechanize gem
7
+ * Ruby
9
8
 
10
9
  ## Installation
11
10
 
@@ -28,6 +27,9 @@ Ugigi Wrapper for Ruby 1.9.x
28
27
  # "にとり"が文章中に含まれているSSの数を持ってくる
29
28
  puts Ugigi.total_count(:free => "にとり")
30
29
 
30
+ # 東方創想話に限定して検索
31
+ puts Ugigi.search(:title => "霊夢", :only => :sosowa)
32
+
31
33
  ## Contributing
32
34
 
33
35
  1. Fork it
data/Rakefile CHANGED
@@ -1,2 +1,5 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new('spec')
@@ -1,5 +1,7 @@
1
+ # coding: utf-8
2
+
1
3
  require "kconv"
2
- require "mechanize"
4
+ require "net/http"
3
5
  require "time"
4
6
  require "json"
5
7
  require "uri"
@@ -7,15 +9,13 @@ require "uri"
7
9
  $LOAD_PATH.unshift(File.expand_path("../", __FILE__))
8
10
  require "ugigi/version"
9
11
  require "ugigi/scheme"
10
- require "ugigi/parser"
11
12
 
12
13
  module Ugigi
13
- BASE_URL = "http://ugigi.dvrdns.org/api/search/list.json"
14
- BASE_URL_NOAPI = "http://ugigi.dvrdns.org/search"
14
+ BASE_URL = "http://ugigi.dvrdns.org/api/"
15
15
 
16
16
  protected
17
17
 
18
- def self.serialize_parameter parameter
18
+ def self.serialize_parameter(parameter={})
19
19
  return "" unless parameter.class == Hash
20
20
  ant = Hash.new
21
21
  parameter.each do |key, value|
@@ -24,16 +24,73 @@ module Ugigi
24
24
  param = ant.inject(""){|k,v|k+"&#{v[0]}=#{URI.escape(v[1])}"}.sub!(/^&/,"?")
25
25
  return param ? param : ""
26
26
  end
27
+
28
+ def self.send_req(path, params={})
29
+ uri = URI.join(BASE_URL, path, serialize_parameter(params))
30
+ response = Net::HTTP.get_response(uri)
31
+ return JSON.parse(response.body)
32
+ end
27
33
 
28
34
  public
29
-
35
+
30
36
  def self.search(args={})
31
- parser = Parser.new
32
- parser.fetch(args)
37
+ case args[:only]
38
+ when :sosowa
39
+ args = args.update(:sswp => 0, :compe => 0)
40
+ when :compe
41
+ args = args.update(:sswl => 0, :sswp => 0)
42
+ when :sswp
43
+ args = args.update(:sswl => 0, :compe => 0)
44
+ end
45
+ args.delete(:only) if args.has_key? :only
46
+ data = send_req("search/list.json", args)
47
+ indexes = []
48
+ data.each do |e|
49
+ index = Index.new(e)
50
+ case args[:style]
51
+ when :sosowa
52
+ index.element = index.to_sosowa_index
53
+ indexes << index
54
+ else
55
+ indexes << index
56
+ end
57
+ end
58
+ return indexes
33
59
  end
34
-
35
- def self.total_count(args)
36
- parser = Parser.new
37
- parser.total_count(args)
60
+
61
+ def self.total_count(args={})
62
+ data = send_req("search/list_count.json", args)
63
+ return data["count"]
64
+ end
65
+
66
+ def self.mylist(args={})
67
+ data = send_req("list/get", args)
68
+ indexes = []
69
+ data.each do |e|
70
+ index = Index.new(e)
71
+ case args[:style]
72
+ when :sosowa
73
+ index.element = index.to_sosowa_index
74
+ indexes << index
75
+ else
76
+ indexes << index
77
+ end
78
+ end
79
+ return indexes
80
+ end
81
+
82
+ def self.mylist_add(args={})
83
+ data = send_req("list/add", args)
84
+ return data
85
+ end
86
+
87
+ def seld.mylist_remove(args={})
88
+ data = send_req("list/rem", args)
89
+ return data
90
+ end
91
+
92
+ def seld.mylist_edit(args={})
93
+ data = send_req("list/edit", args)
94
+ return data
38
95
  end
39
96
  end
@@ -1,6 +1,10 @@
1
1
  module Ugigi
2
2
  class Scheme
3
3
  attr_accessor :element
4
+
5
+ def initialize(element)
6
+ @element = element
7
+ end
4
8
 
5
9
  def method_missing(action, *args)
6
10
  return @element[action.to_s] rescue nil
@@ -10,11 +14,7 @@ module Ugigi
10
14
  alias_method :available_methods, :params
11
15
  end
12
16
 
13
- class Index < Scheme
14
- def initialize(element)
15
- @element = element
16
- end
17
-
17
+ class Index < Scheme
18
18
  def to_sosowa_index
19
19
  log = @element["link"].scan(/log=(\d+)/)[0][0].to_i
20
20
  key = @element["link"].scan(/key=(\d+)/)[0][0].to_i
@@ -1,3 +1,3 @@
1
1
  module Ugigi
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4"
3
3
  end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH << File.expand_path("../../../lib", __FILE__)
2
+ require "ugigi"
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = 'random'
14
+ end
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "spec_helper"
4
+ require "pp"
5
+
6
+ describe Ugigi, "が #search を呼ぶ時は" do
7
+ before do
8
+ @free_search_result = Ugigi.search(:free => "霊夢")
9
+ end
10
+
11
+ it "Array を返すこと" do
12
+ @free_search_result.class.should == Array
13
+ end
14
+
15
+ it "配列の中身が全て Ugigi::Index であること" do
16
+ @free_search_result.each do |i|
17
+ i.class.should == Ugigi::Index
18
+ end
19
+ end
20
+ end
21
+
22
+ describe Ugigi, "が #total_count を呼ぶ時は" do
23
+ before do
24
+ @total_count_result = Ugigi.total_count(:free => "霊夢")
25
+ end
26
+
27
+ it "Fixnum を返すこと" do
28
+ @total_count_result.class.should == Fixnum
29
+ end
30
+ end
@@ -14,5 +14,4 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "ugigi"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Ugigi::VERSION
17
- gem.add_dependency "mechanize"
18
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ugigi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: mechanize
16
- requirement: &70150130472260 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *70150130472260
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
+ dependencies: []
25
14
  description: Ugigi Wrapper for Ruby
26
15
  email:
27
16
  - oame@oameya.com
@@ -30,15 +19,18 @@ extensions: []
30
19
  extra_rdoc_files: []
31
20
  files:
32
21
  - .gitignore
22
+ - .rspec
23
+ - .travis.yml
33
24
  - CHANGELOG.md
34
25
  - Gemfile
35
26
  - LICENSE
36
27
  - README.md
37
28
  - Rakefile
38
29
  - lib/ugigi.rb
39
- - lib/ugigi/parser.rb
40
30
  - lib/ugigi/scheme.rb
41
31
  - lib/ugigi/version.rb
32
+ - spec/spec_helper.rb
33
+ - spec/ugigi_spec.rb
42
34
  - ugigi.gemspec
43
35
  homepage: ''
44
36
  licenses: []
@@ -60,9 +52,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
52
  version: '0'
61
53
  requirements: []
62
54
  rubyforge_project:
63
- rubygems_version: 1.8.10
55
+ rubygems_version: 1.8.24
64
56
  signing_key:
65
57
  specification_version: 3
66
58
  summary: Ugigi Wrapper for Ruby 1.9.x
67
- test_files: []
68
- has_rdoc:
59
+ test_files:
60
+ - spec/spec_helper.rb
61
+ - spec/ugigi_spec.rb
@@ -1,43 +0,0 @@
1
- # coding: utf-8
2
-
3
- module Ugigi
4
- class Parser
5
- def initialize
6
- @agent = Mechanize.new
7
- @agent.user_agent = "Ugigi Ruby #{Ugigi::VERSION}"
8
- end
9
-
10
- def fetch(args)
11
- case args[:only]
12
- when :sosowa
13
- args = args.update(:sswp => 0, :compe => 0)
14
- when :compe
15
- args = args.update(:sswl => 0, :sswp => 0)
16
- when :sswp
17
- args = args.update(:sswl => 0, :compe => 0)
18
- end
19
- args.delete(:only) if args.has_key? :only
20
- params = Ugigi.serialize_parameter(args)
21
- page = @agent.get(URI.join(BASE_URL, params))
22
- data = JSON.parse(page.body)
23
- indexes = []
24
- data.each do |e|
25
- index = Index.new(e)
26
- case args[:style]
27
- when :sosowa
28
- index.element = index.to_sosowa_index
29
- indexes << index
30
- else
31
- indexes << index
32
- end
33
- end
34
- return indexes
35
- end
36
-
37
- def total_count(args)
38
- params = Ugigi.serialize_parameter(args)
39
- page = @agent.get(URI.join(BASE_URL_NOAPI, params))
40
- return page.search(%{div[@class="info"]}).children[0].text.strip.split("/")[1].to_i
41
- end
42
- end
43
- end