hatena-keyword 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,20 @@
1
+ Copyright (c) 2010 [maiha@wota.jp]
2
+
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:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
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 ADDED
@@ -0,0 +1,17 @@
1
+ hatena-keyword
2
+ ==============
3
+
4
+ hatena keyword api
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+
11
+
12
+
13
+ Author
14
+ ======
15
+
16
+ maiha@wota.jp
17
+
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "hatena-keyword"
5
+ AUTHOR = "maiha"
6
+ EMAIL = "maiha@wota.jp"
7
+ HOMEPAGE = "http://github.com/maiha/hatena-keyword"
8
+ SUMMARY = "hatena keyword api"
9
+ GEM_VERSION = "0.0.1"
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.rubyforge_project = 'asakusarb'
13
+ s.executables = []
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.require_path = 'lib'
25
+ s.add_dependency('dsl_accessor', '>= 0.3.3')
26
+ s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ end
32
+
33
+ desc "Install the gem"
34
+ task :install do
35
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
36
+ end
37
+
38
+ desc "Uninstall the gem"
39
+ task :uninstall do
40
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
41
+ end
42
+
43
+ desc "Create a gemspec file"
44
+ task :gemspec do
45
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
46
+ file.puts spec.to_ruby
47
+ end
48
+ end
49
+
50
+ require 'spec/rake/spectask'
51
+ desc 'Default: run spec examples'
52
+ task :default => 'spec'
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/hatena'
@@ -0,0 +1,13 @@
1
+
2
+ require "rubygems"
3
+ require "xmlrpc/client"
4
+ require "nkf"
5
+ require "dsl_accessor"
6
+
7
+ module Hatena
8
+ end
9
+
10
+ __DIR__ = File.dirname(__FILE__)
11
+
12
+ require __DIR__ + "/hatena/null"
13
+ require __DIR__ + "/hatena/keyword"
@@ -0,0 +1,82 @@
1
+
2
+ module Hatena
3
+ Keyword = Struct.new(:word, :score, :cname, :refcount)
4
+
5
+ class Keyword
6
+ def to_s
7
+ word
8
+ end
9
+
10
+ class << self
11
+ def search(body)
12
+ Hatena::Keyword::Retrieve.new(body).execute
13
+ end
14
+
15
+ def link(body)
16
+ Hatena::Keyword::Link.new(body).execute
17
+ end
18
+
19
+ def [](body)
20
+ search(body).map(&:to_s).uniq
21
+ end
22
+ end
23
+
24
+ ######################################################################
25
+ ### Service
26
+
27
+ class Service
28
+ dsl_accessor :logger, Null.new, :instance => true
29
+ dsl_accessor :default_options, proc{{}}
30
+
31
+ def initialize(string, options = {})
32
+ @string = NKF.nkf('-w', string.to_s)
33
+ @options = options
34
+ @result = nil
35
+ end
36
+
37
+ def execute
38
+ server = XMLRPC::Client.new2('http://d.hatena.ne.jp/xmlrpc')
39
+ method = 'hatena.setKeywordLink'
40
+
41
+ hash = default_options
42
+ hash.delete(:body)
43
+ logger.debug "#{self.class.name}(%s, %s)" % [method, hash.inspect]
44
+ @result = server.call(method, default_options)
45
+ end
46
+
47
+ private
48
+ def default_options
49
+ self.class.default_options.merge(@options).merge(:body => @string)
50
+ end
51
+
52
+ def option(key)
53
+ default_options[key]
54
+ end
55
+
56
+ def result
57
+ @result || execute
58
+ end
59
+ end
60
+
61
+ class Link < Service
62
+ default_options({:cname=>"idol", :a_class=>"hatena-keyword", :score=>20})
63
+
64
+ def replace(&block)
65
+ result.gsub(%r{<a class="#{option(:a_class)}".*?>(.*?)</a>}i) {block.call($1)}
66
+ end
67
+ end
68
+
69
+ class Retrieve < Service
70
+ default_options({:cname=>"idol", :a_class=>"hatena-keyword", :score=>20, :mode=>'lite'})
71
+
72
+ def execute
73
+ super
74
+ # result: {"wordlist"=>[{"score"=>78, "cname"=>"idol", "word"=>"石村舞波", "refcount"=>4}]}
75
+ result["wordlist"].map do |h|
76
+ Hatena::Keyword.new(h["word"], h["score"], h["cname"], h["refcount"])
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
@@ -0,0 +1,7 @@
1
+ module Hatena
2
+ class Null
3
+ def method_missing(*)
4
+ self
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module Spec
2
+ module Example
3
+ module Subject
4
+ module ExampleGroupMethods
5
+ def its(*args, &block)
6
+ describe(args.first) do
7
+ define_method(:subject) { super().send(*args) }
8
+ it(&block)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe Hatena::Keyword do
4
+ subject { Hatena::Keyword }
5
+
6
+ it { should respond_to(:[]) }
7
+ it { should respond_to(:search) }
8
+ it { should respond_to(:link) }
9
+ end
@@ -0,0 +1,36 @@
1
+ ######################################################################
2
+ ### provide matcher
3
+ Spec::Matchers.define :provide do |expected|
4
+ match do |obj|
5
+ (obj.public_methods + obj.protected_methods + obj.private_methods).include?(expected.to_s)
6
+ end
7
+ end
8
+
9
+ module Spec
10
+ module Example
11
+ module Subject
12
+ module ExampleGroupMethods
13
+ # == Examples
14
+ #
15
+ # describe User do
16
+ # subject { User.new }
17
+ # provide :name
18
+ #
19
+ # [intead of]
20
+ #
21
+ # it "should provide #name" do
22
+ # methods = subject.public_methods + subject.protected_methods + subject.private_methods
23
+ # methods.should include("name")
24
+ # end
25
+ # end
26
+ #
27
+ def provide(name)
28
+ it "should provide ##{name}" do
29
+ subject.should provide(name)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,16 @@
1
+
2
+ require 'spec'
3
+ require 'rr'
4
+
5
+ require File.join(File.dirname(__FILE__), '/../lib/hatena')
6
+ require File.join(File.dirname(__FILE__), '/its_helper')
7
+ require File.join(File.dirname(__FILE__), '/provide_helper')
8
+
9
+
10
+ def path(key)
11
+ Pathname(File.join(File.dirname(__FILE__) + "/fixtures/#{key}"))
12
+ end
13
+
14
+ def data(key)
15
+ path(key).read{}
16
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hatena-keyword
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - maiha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-14 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dsl_accessor
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.3
24
+ version:
25
+ description: hatena keyword api
26
+ email: maiha@wota.jp
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - MIT-LICENSE
34
+ files:
35
+ - MIT-LICENSE
36
+ - README
37
+ - Rakefile
38
+ - lib/hatena.rb
39
+ - lib/hatena-keyword.rb
40
+ - lib/hatena/null.rb
41
+ - lib/hatena/keyword.rb
42
+ - spec/provide_helper.rb
43
+ - spec/keyword_spec.rb
44
+ - spec/its_helper.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/maiha/hatena-keyword
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: asakusarb
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: hatena keyword api
74
+ test_files: []
75
+