jok 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14f5aab7e070be756a1ed011042507b4c1484a37
4
- data.tar.gz: 4d10e1be850ee77f6afb7003e0ed34aa40cbc5d5
3
+ metadata.gz: 8eb62706f8c5f1ff330fa23a150b91df64e7192a
4
+ data.tar.gz: e0471797d1efb07439209e48d3c5117f333941a4
5
5
  SHA512:
6
- metadata.gz: 17fde72b45b618fcac4c49223e3b8c10d7e0e2a7dcd0ffa23d966af00b58d084f849df67e873318fb7761a6897892c8d3692797c2fb2dccde1afcc3642dc3c30
7
- data.tar.gz: 6b4a213e41f3e90e9a957580248e350c33ce6eae94408663e6a259d2666f2fe0c9839a08860a8155feae8442d19478842b68d6a18b0df2e7c0345930cc9e805f
6
+ metadata.gz: 64385a60d184fb7d5dc5b224a7512468d846015ad1901142591649fa92864350ad0a80a0f89b5ae58708ff97e5eb1f5d327e2e4676e7904968825216cbc92804
7
+ data.tar.gz: 9ba83c4eeeffb8460119f6db5585a1dd46515e408174d407df90c8137121daea03ba54fc70e3b896e609e49647c45e222d499a78c84d25d974087a5244dd0a2d
@@ -4,7 +4,7 @@ module Jok
4
4
 
5
5
  def list
6
6
  @list ||= begin
7
- dom.xpath('//div[@class="left-nav"]/ul/li/a').map(&:text)
7
+ Nokogiri::HTML(HTTParty.get(BASE_URI)).xpath('//div[@class="left-nav"]/ul/li/a').map(&:text)
8
8
  end
9
9
  end
10
10
 
@@ -38,27 +38,33 @@ module Jok
38
38
 
39
39
  private
40
40
 
41
- def dom(method="")
42
- topic = "#{BASE_URI}/#{method.gsub("_", "-")}"
41
+ # Cautious: Randomness!!!
42
+ def parse_uri(method="")
43
+ topic = "#{BASE_URI}#{method.gsub("_", "-")}"
43
44
  response_body = HTTParty.get(topic)
44
- dom_tree = Nokogiri::HTML(response_body)
45
45
 
46
- return dom_tree if method == ""
46
+ return topic if method == ""
47
+
48
+ dom_tree = Nokogiri::HTML(response_body)
47
49
 
48
50
  pagination_items = dom_tree.xpath('//div[@class="pagination"]/ul/li').map(&:text).map(&:strip)
49
51
 
50
52
  if pagination_items.length > 0
51
53
  selected_item = pagination_items[srand % (pagination_items.length - 1)]
52
-
53
- response_body = HTTParty::get("#{topic}/#{selected_item}")
54
- Nokogiri::HTML(response_body)
54
+ "#{topic}/#{selected_item}"
55
55
  else
56
- dom_tree
56
+ topic
57
57
  end
58
58
  end
59
59
 
60
60
  def joke(topic="")
61
- dom(topic).xpath('//div[@class="joke3-pop-box"]/p[substring-before(@id, "_") = "joke"]').map(&:text).map(&:strip)
61
+ uri = parse_uri(topic)
62
+ response_body = HTTParty.get(uri)
63
+ dom = Nokogiri::HTML(response_body).xpath('//div[@class="joke3-pop-box"]/p[substring-before(@id, "_") = "joke"]')
64
+
65
+ dom.map do |child|
66
+ OpenStruct.new(content: child.text.strip, link: "#{uri}##{child.attributes["id"].value}")
67
+ end
62
68
  end
63
69
  end
64
70
  end
@@ -1,3 +1,3 @@
1
1
  module Jok
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -6,6 +6,9 @@ describe Jok, "Hello world" do
6
6
  end
7
7
 
8
8
  it "#da" do
9
- Jok.da.must_be_instance_of String
9
+ joke = Jok.da
10
+ joke.must_be_instance_of OpenStruct
11
+ joke.content.must_be_instance_of String
12
+ joke.link.must_be_instance_of String
10
13
  end
11
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jok
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jingkai He
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-30 00:00:00.000000000 Z
11
+ date: 2015-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  requirements: []
135
135
  rubyforge_project:
136
- rubygems_version: 2.2.2
136
+ rubygems_version: 2.4.5
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: Bring you a good laugh.