wikipedia-client 1.15.0 → 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af50d4fda3d70e5bd837bde17b2ca86ec4c653c92891a5088246772da4ca45b5
4
- data.tar.gz: 6366ad0bf999ed51533fd49963b4fa6d36e7181d49fc9bab227407cc3f76300f
3
+ metadata.gz: 6bd75b9c5cb006f00a42ee85054e1cce3d61d942c6c2c8d55f01d46ce594ad0e
4
+ data.tar.gz: 594bdcb73afa6d02d3a6761bad0180df3b19896f1474bdfbcfed04382318afb4
5
5
  SHA512:
6
- metadata.gz: d0f6e70ee49591fb4fb1238cd35ede198ff14e6efd89ddfbc972e4c7b03ff3aeabc2770c80f7981af5ec30d820ffbd5ca4a4870da944686d612134d34dacde87
7
- data.tar.gz: 701089ac1c06f95ea5b67afba9b7d6754c967d0d4411f7b95b3f8a8f456b9adabc4ab67036be5024c70a1d21022144a5e88d4693c76d719ab3ad4bff0cd01e9d
6
+ metadata.gz: a9dd0e851b81c6f59769eae01e7439c11efba75684c321eb26589b572ead963235b2b8a322169b7171908ef930a4fddcb61766e605acee5ae1d4fa3e08bea6d4
7
+ data.tar.gz: b54cd43b57dafc5409a3e4524623bebbc3c275c26c97c6aeed7ccb821a075025d3dd93f41e557155e219e7346b4138c72c012f1bd175c34b49679c0d8334a4ee
data/README.md CHANGED
@@ -124,6 +124,29 @@ page.raw_data
124
124
  "lastrevid"=>348481810, "counter"=>0, "length"=>7891}}}}
125
125
  ```
126
126
 
127
+ ### Additional HTTP headers
128
+
129
+ Some API features require tweaking HTTP headers. You can add additional headers via configuration.
130
+
131
+ For example, to retrieve the same page in different language variants:
132
+
133
+ ```ruby
134
+ Wikipedia.configure do
135
+ domain 'zh.wikipedia.org'
136
+ headers({ 'Accept-Language' => 'zh-tw' })
137
+ end
138
+
139
+ Wikipedia.find('牛肉').summary #=> "牛肉是指從牛身上得出的肉,為常見的肉品之一。肌肉部分可以切成牛排、牛肉塊或牛仔骨,也可以與其他的肉混合做成香腸或血腸。"
140
+
141
+ Wikipedia.configure do
142
+ domain 'zh.wikipedia.org'
143
+ headers({ 'Accept-Language' => 'zh-cn' })
144
+ end
145
+
146
+ Wikipedia.find('牛肉').summary #=> "牛肉是指从牛身上得出的肉,为常见的肉品之一。肌肉部分可以切成牛排、牛肉块或牛仔骨,也可以与其他的肉混合做成香肠或血肠。"
147
+ ```
148
+
149
+
127
150
  ## Contributing
128
151
 
129
152
  ### Getting the code, and running the tests
@@ -73,7 +73,7 @@ module Wikipedia
73
73
  end
74
74
 
75
75
  def request( options )
76
- URI.parse( url_for( options ) ).read( 'User-Agent' => @configuration[:user_agent] )
76
+ URI.parse( url_for( options ) ).read( headers )
77
77
  end
78
78
 
79
79
  protected
@@ -114,5 +114,11 @@ module Wikipedia
114
114
  end
115
115
  [h1, h2]
116
116
  end
117
+
118
+ def headers
119
+ {
120
+ 'User-Agent' => @configuration[:user_agent]
121
+ }.merge( @configuration[:headers] )
122
+ end
117
123
  end
118
124
  end
@@ -4,7 +4,8 @@ module Wikipedia
4
4
  protocol: 'https',
5
5
  domain: 'en.wikipedia.org',
6
6
  path: 'w/api.php',
7
- user_agent: 'wikipedia-client/1.7 (https://github.com/kenpratt/wikipedia-client)'
7
+ user_agent: 'wikipedia-client/1.7 (https://github.com/kenpratt/wikipedia-client)',
8
+ headers: {}
8
9
  }.freeze
9
10
 
10
11
  def initialize(configuration = DEFAULT)
@@ -25,6 +26,6 @@ module Wikipedia
25
26
  end
26
27
  end
27
28
 
28
- directives :protocol, :domain, :path, :user_agent
29
+ directives :protocol, :domain, :path, :user_agent, :headers
29
30
  end
30
31
  end
data/lib/wikipedia/url.rb CHANGED
@@ -11,7 +11,12 @@ module Wikipedia
11
11
  return @title if @title
12
12
 
13
13
  uri = URI.parse( @wiki_url )
14
- @title = Addressable::URI.unencode( uri.path.sub(/\/wiki\//, '') )
14
+ @title =
15
+ if uri.path.empty?
16
+ @wiki_url
17
+ else
18
+ Addressable::URI.unencode( uri.path.sub(/\/wiki\//, '') )
19
+ end
15
20
  end
16
21
  end
17
22
  end
@@ -1,3 +1,3 @@
1
1
  module Wikipedia
2
- VERSION = '1.15.0'.freeze
2
+ VERSION = '1.16.0'.freeze
3
3
  end
@@ -0,0 +1,2 @@
1
+ # rubocop:disable Style/FileName
2
+ require 'wikipedia'
data/lib/wikipedia.rb CHANGED
@@ -24,7 +24,7 @@ module Wikipedia
24
24
  end
25
25
 
26
26
  def self.configure(&block)
27
- @configuration.instance_eval(&block)
27
+ configuration.instance_eval(&block)
28
28
  end
29
29
 
30
30
  # rubocop:disable Style/MethodName
@@ -35,9 +35,12 @@ module Wikipedia
35
35
  class << self
36
36
  private
37
37
 
38
- def client
38
+ def configuration
39
39
  @configuration ||= Wikipedia::Configuration.new
40
- @client ||= Wikipedia::Client.new @configuration
40
+ end
41
+
42
+ def client
43
+ @client ||= Wikipedia::Client.new configuration
41
44
  end
42
45
  end
43
46
  end
data/spec/lib/url_spec.rb CHANGED
@@ -6,3 +6,10 @@ describe Wikipedia::Url, 'like http://en.wikipedia.org/wiki/Getting_Things_Done'
6
6
  expect(url.title).to eq('Getting_Things_Done')
7
7
  end
8
8
  end
9
+
10
+ describe Wikipedia::Url, 'like "? (Lost)"' do
11
+ it 'should return input as title' do
12
+ url = Wikipedia::Url.new('? (Lost)')
13
+ expect(url.title).to eq('? (Lost)')
14
+ end
15
+ end
@@ -18,3 +18,15 @@ describe Wikipedia, '.find' do
18
18
  expect(page1.title).to eq(page2.title)
19
19
  end
20
20
  end
21
+
22
+ describe Wikipedia, '.configure' do
23
+ it 'should set configuration' do
24
+ Wikipedia.configure do
25
+ protocol 'https'
26
+ domain 'zh.wikipedia.org'
27
+ end
28
+
29
+ page = Wikipedia.find('Getting_Things_Done')
30
+ expect(page.fullurl).to start_with('https://zh.wikipedia.org')
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikipedia-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.15.0
4
+ version: 1.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril David
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2021-07-06 00:00:00.000000000 Z
16
+ date: 2021-09-08 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: addressable
@@ -46,6 +46,7 @@ files:
46
46
  - Rakefile
47
47
  - init.rb
48
48
  - install.rb
49
+ - lib/wikipedia-client.rb
49
50
  - lib/wikipedia.rb
50
51
  - lib/wikipedia/client.rb
51
52
  - lib/wikipedia/configuration.rb