pathtraq 001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/pathtraq.rb +144 -0
  2. data/spec/spec_pathtraq.rb +99 -0
  3. metadata +63 -0
data/lib/pathtraq.rb ADDED
@@ -0,0 +1,144 @@
1
+ require "open-uri"
2
+ require "simple-rss"
3
+
4
+ module Pathtraq
5
+ VERSION = "000"
6
+
7
+ class Item
8
+ attr_reader :title
9
+ attr_reader :link
10
+ attr_reader :description
11
+ attr_reader :hits
12
+
13
+ def initialize(data)
14
+ @title = data.title
15
+ @link = data.link
16
+ @description = data.description
17
+ @hits = data.pathtraq_hits.to_i
18
+ end
19
+ end
20
+
21
+ class Feed < DelegateClass(Array)
22
+ def self.request(params = {})
23
+ params ||= {}
24
+ new(SimpleRSS.new(Request.new(self::URL, params).send))
25
+ end
26
+
27
+ attr_reader :title
28
+ attr_reader :link
29
+
30
+ def initialize(data)
31
+ __setobj__([])
32
+ data.channel.items.each do |i|
33
+ self << Item.new(i)
34
+ end
35
+ @title = data.title
36
+ @link = data.link
37
+ end
38
+ end
39
+
40
+ class NewsRanking < Feed
41
+ URL = "http://api.pathtraq.com/news_ja"
42
+ PARAMS = [:genre, :m]
43
+ GENRES = [:national, :sports, :business, :politics, :international,
44
+ :academic, :culture ]
45
+ class << self
46
+ GENRES.each do |genre|
47
+ define_method(genre) do |*params|
48
+ params = (params.first || Hash.new)
49
+ params[:genre] = genre
50
+ request(params)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ class CategoryRanking < Feed
57
+ URL = "http://api.pathtraq.com/popular"
58
+ PARAMS = [:category, :m]
59
+ CATEGORIES = [:politics, :business, :society, :showbiz, :music, :movie,
60
+ :anime, :game, :sports, :motor, :education, :reading,
61
+ :science, :art, :foods, :travel, :mobile, :computer, :web ]
62
+ class << self
63
+ CATEGORIES.each do |cat|
64
+ define_method(cat) do |*params|
65
+ params = (params.first || Hash.new)
66
+ params[:category] = cat
67
+ request(params)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ class KeywordSearch < Feed
74
+ URL = "http://api.pathtraq.com/pages"
75
+ PARAMS = [:m, :url]
76
+ end
77
+
78
+ module PageCounter
79
+ URL = "http://api.pathtraq.com/page_counter"
80
+ PARAMS = [:m, :url]
81
+
82
+ def self.request(params)
83
+ params[:api] = "json"
84
+ res = Request.new(URL, params).send
85
+ if md = /count:\s*(\d+)/.match(res)
86
+ md[1].to_i
87
+ else
88
+ raise Error.new(res, params)
89
+ end
90
+ end
91
+ end
92
+
93
+ module PageChart
94
+ URL = "http://api.pathtraq.com/page_chart"
95
+ PARAMS = [:url, :scale]
96
+
97
+ def self.request(params)
98
+ params[:api] = "json"
99
+ res = Request.new(URL, params).send
100
+ if md = /plots:\s*\[\s*([\d\s,]+)\s*\]/.match(res)
101
+ return md[1].gsub!(" ", "").split(",").map{|s| s.to_i }
102
+ else
103
+ raise Error.new(res, params)
104
+ end
105
+ end
106
+
107
+ def self.url(url, scale=:"24h")
108
+ request(:url => url, :scale => scale)
109
+ end
110
+ end
111
+
112
+ class Error < StandardError
113
+ def initialize(res, params)
114
+ @result = res
115
+ @params = params
116
+ end
117
+
118
+ def message
119
+ "Maybe caused by invalid query: #{@params}"
120
+ end
121
+ end
122
+
123
+ class Request
124
+ def initialize(url, params)
125
+ @uri = URI(url)
126
+ @params = params.map {|key, val| "#{key}=#{val}" }.join("&")
127
+ @uri.query = URI.escape(@params) if @params.size > 0
128
+ end
129
+
130
+ def send
131
+ STDERR.puts "Pathtraq: request to #{@uri}" if $DEBUG
132
+
133
+ begin
134
+ res = @uri.read
135
+ STDERR.puts "#{res.status.join(" ")} #{res.content_type}" if $DEBUG
136
+ return res
137
+ rescue OpenURI::HTTPError
138
+ raise Error.new(res, @params)
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ SimpleRSS.item_tags << :'pathtraq:hits'
@@ -0,0 +1,99 @@
1
+ # This spec is written in bacon DSL.
2
+ # Please execute the folloing command if you don't know bacon:
3
+ #
4
+ # % sudo gem install bacon
5
+
6
+ require "bacon"
7
+
8
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
9
+
10
+ $KCODE = "UTF-8"
11
+
12
+ URL = "http://d.hatena.ne.jp/keita_yamaguchi/"
13
+
14
+ require "pathtraq"
15
+ include Pathtraq
16
+
17
+ shared "feed" do
18
+ it 'should send a request' do
19
+ proc { @feedclass.request(@default_params) }.should.not.raise
20
+ end
21
+
22
+ it 'should send a request with parameters' do
23
+ proc do
24
+ @feedclass.request(@parameters)
25
+ end.should.not.raise
26
+ end
27
+
28
+ it 'should be an array of items' do
29
+ res = @feedclass.request(@default_params)
30
+ res.should.kind_of Array
31
+ res.each {|item| item.should.kind_of Item }
32
+ end
33
+
34
+ it 'should have title and link' do
35
+ feed = @feedclass.request(@default_params)
36
+ feed.title.should.not.nil
37
+ feed.link.should.not.nil
38
+ end
39
+ end
40
+
41
+ describe "Pathtraq::NewsRanking" do
42
+ before do
43
+ @feedclass = NewsRanking
44
+ @parameters = { :m => :hot, :genre => :sports }
45
+ end
46
+
47
+ behaves_like "feed"
48
+
49
+ it 'should send a request with parameters' do
50
+ proc do
51
+ NewsRanking.request(:m => :hot, :genre => :sports)
52
+ end.should.not.raise
53
+ end
54
+
55
+ it 'should be callable by genre' do
56
+ NewsRanking::GENRES.each do |genre|
57
+ proc { NewsRanking.send(genre) }.should.not.raise
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "Pathtraq::CategoryRanking" do
63
+ before do
64
+ @feedclass = CategoryRanking
65
+ @parameters = { :m => :hot, :genre => :politics }
66
+ end
67
+
68
+ behaves_like "feed"
69
+
70
+ it 'should be callable by category' do
71
+ CategoryRanking::CATEGORIES.each do |cat|
72
+ proc { CategoryRanking.send(cat) }.should.not.raise
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "Pathtraq::KeywordSearch" do
78
+ before do
79
+ @feedclass = KeywordSearch
80
+ @parameters = { :m => :hot, :url => URL }
81
+ @default_params = { :url => "ruby"}
82
+ end
83
+
84
+ behaves_like "feed"
85
+ end
86
+
87
+ describe "Pathtraq::PageCounter" do
88
+ it 'should return counter' do
89
+ PageCounter.request(:url => URL).should >= 0
90
+ end
91
+ end
92
+
93
+ describe "Pathtraq::PageChart" do
94
+ it 'should return access numbers' do
95
+ res = PageChart.request(:url => URL)
96
+ res.size.should > 0
97
+ res.each {|i| i.should >=0 }
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pathtraq
3
+ version: !ruby/object:Gem::Version
4
+ version: "001"
5
+ platform: ruby
6
+ authors:
7
+ - Keita Yamaguchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-27 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: simple-rss
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: keita.yamaguchi@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/pathtraq.rb
35
+ - spec/spec_pathtraq.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/keita/ruby-pathtraq/tree/master
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: pathtraq
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: ruby-pathtraq is a wrappter library for Pathtraq API
62
+ test_files: []
63
+