wikidata-client 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.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +69 -0
- data/LICENCE +21 -0
- data/README.md +180 -0
- data/Rakefile +5 -0
- data/config/mapping.yml +19 -0
- data/config/settings.yml +4 -0
- data/lib/wikidata.rb +39 -0
- data/lib/wikidata/client.rb +36 -0
- data/lib/wikidata/config.rb +23 -0
- data/lib/wikidata/entity.rb +83 -0
- data/lib/wikidata/item.rb +41 -0
- data/lib/wikidata/property.rb +29 -0
- data/lib/wikidata/property/base.rb +16 -0
- data/lib/wikidata/property/globe_coordinate.rb +13 -0
- data/lib/wikidata/property/string.rb +6 -0
- data/lib/wikidata/property/time.rb +66 -0
- data/lib/wikidata/property/url.rb +6 -0
- data/lib/wikidata/resource.rb +69 -0
- data/lib/wikidata/response.rb +31 -0
- data/lib/wikidata/search_response.rb +35 -0
- data/lib/wikidata/version.rb +3 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/entities/should_allow_to_return_only_entities_ids_from_a_key.yml +160 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/entities/should_allow_to_return_only_entities_ids_from_an_id.yml +160 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/entities/should_return_entities_property_from_a_key_children_here_.yml +518 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/entities/should_return_entities_property_from_an_id_P40_for_children_here_.yml +516 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_date/should_return_Time_from_a_a_key_date_of_birth_here_.yml +162 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_date/should_return_Time_from_a_property_id_P569_for_date_of_birth_here_.yml +162 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_globe_coordinate/should_return_GlobeCoordinate_from_a_key_coordinate_location_here_.yml +58 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_globe_coordinate/should_return_GlobeCoordinate_from_a_property_id_P625_for_coordinate_location_here_.yml +58 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_string/should_return_String_from_a_key_freebase_identifier_here_.yml +160 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_string/should_return_String_from_a_property_id_P646_for_freebase_identifier_here_.yml +160 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_url/should_return_Url_from_a_key_official_website_here_.yml +78 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/properties/of_type_url/should_return_Url_from_a_property_id_P856_for_official_website_here_.yml +78 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/should_return_aliases.yml +176 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/should_return_descriptions.yml +178 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/should_return_id.yml +176 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/should_return_labels.yml +176 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/should_return_sitelinks.yml +176 -0
- data/spec/fixtures/vcr/Wikidata_Entity/properties/should_return_url.yml +178 -0
- data/spec/fixtures/vcr/Wikidata_Item/_find_/should_find_by_id.yml +319 -0
- data/spec/fixtures/vcr/Wikidata_Item/_find_/should_find_by_ids.yml +441 -0
- data/spec/fixtures/vcr/Wikidata_Item/_find_/should_find_by_title.yml +319 -0
- data/spec/fixtures/vcr/Wikidata_Item/_find_/should_find_by_titles.yml +441 -0
- data/spec/fixtures/vcr/Wikidata_Item/_find_/should_return_nil_if_item_not_found.yml +58 -0
- data/spec/fixtures/vcr/Wikidata_Item/_search/should_fetch_all_all_items_of_the_collection.yml +656 -0
- data/spec/fixtures/vcr/Wikidata_Item/_search/should_return_a_response_with_an_empty_array_of_results_if_search_missing.yml +57 -0
- data/spec/fixtures/vcr/Wikidata_Item/_search/should_return_an_array_of_Wikidata_Item.yml +654 -0
- data/spec/fixtures/vcr/Wikidata_Item/_search/should_return_the_next_page_offset.yml +123 -0
- data/spec/fixtures/vcr/Wikidata_Item/_search/should_return_the_total_number_of_hits.yml +123 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/wikidata/client_spec.rb +4 -0
- data/spec/wikidata/entity_spec.rb +139 -0
- data/spec/wikidata/item_spec.rb +64 -0
- data/spec/wikidata/property/time_spec.rb +61 -0
- data/spec/wikidata_spec.rb +20 -0
- data/wikidata-client.gemspec +27 -0
- metadata +262 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 176a5af4ed352f7b9647a8ee712c5dcc98314abe
|
4
|
+
data.tar.gz: 4a1bbc872493aaeb43f0c374de9aac2244e445fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e88633911b243de244e13a694cef8ad33dcd3fc3442f78e4e3c9bd1b61b2a6e10b1edbfb656c5d87f09abfc1e4468640d9e0b070c1fccabbb64fb1363e612c1
|
7
|
+
data.tar.gz: 318598d5c79876629b2bf334a1c60a3d5a16ff544e669335908ad61d7e26cee1594899dfe2cce27d8aff1d2014dee5f24fc46f6d636ff5ae762c4631adfcbea1
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
wikidata-client (0.0.1)
|
5
|
+
faraday (~> 0.9)
|
6
|
+
faraday_middleware (~> 0.9)
|
7
|
+
hashie (~> 3.3)
|
8
|
+
patron (~> 0.4)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
addressable (2.3.6)
|
14
|
+
codeclimate-test-reporter (0.4.0)
|
15
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
16
|
+
coderay (1.1.0)
|
17
|
+
crack (0.4.2)
|
18
|
+
safe_yaml (~> 1.0.0)
|
19
|
+
diff-lcs (1.2.5)
|
20
|
+
docile (1.1.5)
|
21
|
+
faraday (0.9.0)
|
22
|
+
multipart-post (>= 1.2, < 3)
|
23
|
+
faraday_middleware (0.9.1)
|
24
|
+
faraday (>= 0.7.4, < 0.10)
|
25
|
+
hashie (3.3.1)
|
26
|
+
method_source (0.8.2)
|
27
|
+
multi_json (1.10.1)
|
28
|
+
multipart-post (2.0.0)
|
29
|
+
patron (0.4.18)
|
30
|
+
pry (0.10.1)
|
31
|
+
coderay (~> 1.1.0)
|
32
|
+
method_source (~> 0.8.1)
|
33
|
+
slop (~> 3.4)
|
34
|
+
rake (10.3.2)
|
35
|
+
rspec (3.1.0)
|
36
|
+
rspec-core (~> 3.1.0)
|
37
|
+
rspec-expectations (~> 3.1.0)
|
38
|
+
rspec-mocks (~> 3.1.0)
|
39
|
+
rspec-core (3.1.4)
|
40
|
+
rspec-support (~> 3.1.0)
|
41
|
+
rspec-expectations (3.1.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.1.0)
|
44
|
+
rspec-mocks (3.1.1)
|
45
|
+
rspec-support (~> 3.1.0)
|
46
|
+
rspec-support (3.1.0)
|
47
|
+
safe_yaml (1.0.3)
|
48
|
+
simplecov (0.9.1)
|
49
|
+
docile (~> 1.1.0)
|
50
|
+
multi_json (~> 1.0)
|
51
|
+
simplecov-html (~> 0.8.0)
|
52
|
+
simplecov-html (0.8.0)
|
53
|
+
slop (3.6.0)
|
54
|
+
vcr (2.9.3)
|
55
|
+
webmock (1.18.0)
|
56
|
+
addressable (>= 2.3.6)
|
57
|
+
crack (>= 0.3.2)
|
58
|
+
|
59
|
+
PLATFORMS
|
60
|
+
ruby
|
61
|
+
|
62
|
+
DEPENDENCIES
|
63
|
+
codeclimate-test-reporter
|
64
|
+
pry (~> 0.10)
|
65
|
+
rake (~> 10.3)
|
66
|
+
rspec (~> 3.1)
|
67
|
+
vcr (~> 2.9)
|
68
|
+
webmock (~> 1.18)
|
69
|
+
wikidata-client!
|
data/LICENCE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT LICENSE
|
2
|
+
|
3
|
+
Copyright (c) klacointe <kevinlacointe@gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# Wikidata API Client
|
2
|
+
|
3
|
+
[](https://travis-ci.org/klacointe/wikidata-client)
|
4
|
+
[](https://codeclimate.com/github/klacointe/wikidata-client)
|
5
|
+
[](https://codeclimate.com/github/klacointe/wikidata-client)
|
6
|
+
[](https://gemnasium.com/klacointe/wikidata-client)
|
7
|
+
|
8
|
+
|
9
|
+
[Wikidata](http://www.wikidata.org/) client library for Ruby.
|
10
|
+
|
11
|
+
It provide an easy way to search for wikidata pages and read their contents.
|
12
|
+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
In bunler: `gem 'wikidata-client', '~> 0.0.1', require: 'wikidata'`
|
17
|
+
|
18
|
+
Otherwise: `gem install wikidata-client`
|
19
|
+
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
|
23
|
+
Only one thing is configurable for now, the HTTP client [patron](https://github.com/toland/patron)
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Wikidata.configure do |c|
|
27
|
+
c.client_options = {
|
28
|
+
request: {
|
29
|
+
open_timeout: 2,
|
30
|
+
timeout: 9
|
31
|
+
}
|
32
|
+
}
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
|
39
|
+
### Search
|
40
|
+
|
41
|
+
```irb
|
42
|
+
> search = Wikidata::Item.search 'Homer Simpson'
|
43
|
+
=> <Wikidata::SearchResponse results: [
|
44
|
+
<Wikidata::Item id: Q7810, title: "Homer Simpson">,
|
45
|
+
<Wikidata::Item id: Q3577370, title: "Homer Simpson, This Is Your Wife">,
|
46
|
+
<Wikidata::Item id: Q2082128, title: "Homer Simpson in: "Kidney Trouble">,
|
47
|
+
<Wikidata::Item id: Q2715733, title: "Politically Inept, with Homer Simpson">,
|
48
|
+
<Wikidata::Item id: Q1626047, title: "The City of New York vs. Homer Simpson">,
|
49
|
+
<Wikidata::Item id: Q5966549, title: "Q5966549">,
|
50
|
+
<Wikidata::Item id: Q15162647, title: "Being Homer Simpson">,
|
51
|
+
<Wikidata::Item id: Q17653635, title: "'Homer Simpson' contributes to U.S. presidential candidate Buddy Roemer">,
|
52
|
+
<Wikidata::Item id: Q17713638, title: "Q17713638">,
|
53
|
+
<Wikidata::Item id: Q2751406, title: "Q2751406">
|
54
|
+
]>
|
55
|
+
|
56
|
+
> search.empty?
|
57
|
+
=> false
|
58
|
+
|
59
|
+
> search.results.first.labels['en'].value
|
60
|
+
=> "Homer Simpson"
|
61
|
+
|
62
|
+
> search.total_hits
|
63
|
+
=> 119
|
64
|
+
|
65
|
+
> search.next_page_offset
|
66
|
+
=> 10
|
67
|
+
```
|
68
|
+
|
69
|
+
### Find by ids or titles
|
70
|
+
|
71
|
+
```irb
|
72
|
+
> homer = Wikidata::Item.find_by_title 'Homer Simpson' # Same as homer = Wikidata::Item.find 'Q7810'
|
73
|
+
=> <Wikidata::Item id: Q7810, title: "Homer Simpson">
|
74
|
+
|
75
|
+
> homer.children
|
76
|
+
=> [<Wikidata::Item id: Q5480, title: "Bart Simpson">, <Wikidata::Item id: Q5846, title: "Lisa Simpson">, <Wikidata::Item id: Q7834, title: "Maggie Simpson">]
|
77
|
+
|
78
|
+
> homer.date_of_birth.date
|
79
|
+
=> #<DateTime: 1956-06-18T00:00:00+00:00 ((2435643j,0s,0n),+0s,2299161j)>
|
80
|
+
```
|
81
|
+
|
82
|
+
|
83
|
+
If you want several pages at the same time, just give an array to `find` or `find_by_title` methods:
|
84
|
+
|
85
|
+
```irb
|
86
|
+
> Wikidata::Item.find_by_title ['Sid Vicious', 'John Lydon']
|
87
|
+
=> <Wikidata::Response results: [
|
88
|
+
<Wikidata::Item id: Q47878, title: "Sid Vicious">,
|
89
|
+
<Wikidata::Item id: Q106662, title: "John Lydon">
|
90
|
+
]>
|
91
|
+
|
92
|
+
> Wikidata::Item.find ['Q47878', 'Q106662']
|
93
|
+
=> <Wikidata::Response results: [
|
94
|
+
<Wikidata::Item id: Q47878, title: "Sid Vicious">,
|
95
|
+
<Wikidata::Item id: Q106662, title: "John Lydon">
|
96
|
+
]>
|
97
|
+
```
|
98
|
+
|
99
|
+
## Properties
|
100
|
+
|
101
|
+
To access an item property, use the Wikidata property code and one of these
|
102
|
+
methods:
|
103
|
+
|
104
|
+
- `properties`: to get all properties
|
105
|
+
- `property`: to get the first property
|
106
|
+
|
107
|
+
```irb
|
108
|
+
> homer = Wikidata::Item.find_by_title 'Homer Simpson'
|
109
|
+
|
110
|
+
=> <Wikidata::Item id: Q7810, title: "Homer Simpson">
|
111
|
+
|
112
|
+
> homer.properties('P40')
|
113
|
+
=> [
|
114
|
+
<Wikidata::Item id: Q5480, title: "Bart Simpson">,
|
115
|
+
<Wikidata::Item id: Q5846, title: "Lisa Simpson">,
|
116
|
+
<Wikidata::Item id: Q7834, title: "Maggie Simpson">
|
117
|
+
]
|
118
|
+
|
119
|
+
> homer.property('P22')
|
120
|
+
=> <Wikidata::Item id: Q842104, title: "Grampa Simpson">
|
121
|
+
```
|
122
|
+
|
123
|
+
Alternatively, you can map wikidata property codes to method names, see
|
124
|
+
https://github.com/klacointe/wikidata-client/blob/master/config/mapping.yml to
|
125
|
+
get the list of mapped properties.
|
126
|
+
|
127
|
+
If you define the mapping under the `resources` section, accessor will be on the
|
128
|
+
first property only. If under the `collections` sections, all properties will be
|
129
|
+
returned:
|
130
|
+
|
131
|
+
```irb
|
132
|
+
> homer = Wikidata::Item.find_by_title 'Homer Simpson'
|
133
|
+
|
134
|
+
=> <Wikidata::Item id: Q7810, title: "Homer Simpson">
|
135
|
+
|
136
|
+
> homer.children
|
137
|
+
=> [
|
138
|
+
<Wikidata::Item id: Q5480, title: "Bart Simpson">,
|
139
|
+
<Wikidata::Item id: Q5846, title: "Lisa Simpson">,
|
140
|
+
<Wikidata::Item id: Q7834, title: "Maggie Simpson">
|
141
|
+
]
|
142
|
+
|
143
|
+
> homer.father
|
144
|
+
=> <Wikidata::Item id: Q842104, title: "Grampa Simpson">
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
Feel free to add yours in a topic branch and seed me a pull request.
|
149
|
+
|
150
|
+
|
151
|
+
Finally of you want to get only ids of properties to avoid making several
|
152
|
+
unecessary requests, you can use these methods: `property_id` and `property_ids`:
|
153
|
+
|
154
|
+
```irb
|
155
|
+
> homer = Wikidata::Item.find_by_title 'Homer Simpson'
|
156
|
+
|
157
|
+
=> <Wikidata::Item id: Q7810, title: "Homer Simpson">
|
158
|
+
|
159
|
+
> homer.property_ids('P40')
|
160
|
+
=> ["Q5480", "Q5846", "Q7834"]
|
161
|
+
|
162
|
+
> homer.property_id('P22')
|
163
|
+
=> "Q842104"
|
164
|
+
```
|
165
|
+
|
166
|
+
## Note on Patches/Pull Requests
|
167
|
+
|
168
|
+
* Fork the project.
|
169
|
+
* Make your feature addition or bug fix.
|
170
|
+
* Add tests for it. This is important so I don't break it in a future version
|
171
|
+
unintentionally.
|
172
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have
|
173
|
+
your own version, that is fine but bump version in a commit by itself in another
|
174
|
+
branch so I can ignore when I pull)
|
175
|
+
* Send me a pull request. Bonus points for topic branches.
|
176
|
+
|
177
|
+
|
178
|
+
## Copyright
|
179
|
+
|
180
|
+
MIT. See LICENSE for further details.
|
data/Rakefile
ADDED
data/config/mapping.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
resources:
|
2
|
+
creator: P170
|
3
|
+
place_of_birth: P19
|
4
|
+
place_of_death: P20
|
5
|
+
country_of_citizenship: P27
|
6
|
+
date_of_birth: P569
|
7
|
+
date_of_death: P570
|
8
|
+
date_of_foundation_or_creation: P571
|
9
|
+
official_website: P856
|
10
|
+
gender: P21
|
11
|
+
is_in_the_administrative_territorial_entity: P131
|
12
|
+
coordinate_location: P625
|
13
|
+
freebase_identifier: P646
|
14
|
+
father: P22
|
15
|
+
collections:
|
16
|
+
official_websites: P856
|
17
|
+
instance_of: P31
|
18
|
+
subclass_of: P279
|
19
|
+
children: P40
|
data/config/settings.yml
ADDED
data/lib/wikidata.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'patron'
|
4
|
+
require 'hashie'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
require 'wikidata/config'
|
8
|
+
|
9
|
+
module Wikidata
|
10
|
+
class << self
|
11
|
+
def configure &block
|
12
|
+
Config.configure &block
|
13
|
+
end
|
14
|
+
|
15
|
+
def client_options
|
16
|
+
Config.client_options
|
17
|
+
end
|
18
|
+
|
19
|
+
def settings
|
20
|
+
Config.settings
|
21
|
+
end
|
22
|
+
|
23
|
+
def mapping
|
24
|
+
Config.mapping
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'wikidata/client'
|
30
|
+
require 'wikidata/property'
|
31
|
+
require 'wikidata/property/base'
|
32
|
+
require 'wikidata/property/string'
|
33
|
+
require 'wikidata/property/time'
|
34
|
+
require 'wikidata/property/url'
|
35
|
+
require 'wikidata/property/globe_coordinate'
|
36
|
+
require 'wikidata/entity'
|
37
|
+
require 'wikidata/item'
|
38
|
+
require 'wikidata/response'
|
39
|
+
require 'wikidata/search_response'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Wikidata
|
2
|
+
class Client
|
3
|
+
DEFAULT_QUERY = {
|
4
|
+
format: 'json'
|
5
|
+
}
|
6
|
+
|
7
|
+
attr_accessor :query, :total_pages
|
8
|
+
|
9
|
+
def initialize query = {}
|
10
|
+
@query = DEFAULT_QUERY.merge(query)
|
11
|
+
end
|
12
|
+
|
13
|
+
def response
|
14
|
+
response_class.new api.get('', query)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def response_class
|
20
|
+
case @query[:action]
|
21
|
+
when 'query'
|
22
|
+
Wikidata::SearchResponse
|
23
|
+
else
|
24
|
+
Wikidata::Response
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def api
|
29
|
+
Faraday.new({url: Wikidata.settings.api.url}.merge(Wikidata.client_options)) do |faraday|
|
30
|
+
faraday.request :url_encoded
|
31
|
+
faraday.response :json, content_type: /\bjson$/
|
32
|
+
faraday.adapter :patron
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Wikidata
|
2
|
+
class Config
|
3
|
+
class << self
|
4
|
+
attr_accessor :client_options, :settings, :mapping
|
5
|
+
|
6
|
+
def configure &block
|
7
|
+
yield self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
@client_options = {}
|
12
|
+
@settings = Hashie::Mash.new(
|
13
|
+
YAML.load_file(
|
14
|
+
File.expand_path('../../../config/settings.yml', __FILE__)
|
15
|
+
)
|
16
|
+
)
|
17
|
+
@mapping = Hashie::Mash.new(
|
18
|
+
YAML.load_file(
|
19
|
+
File.expand_path('../../../config/mapping.yml', __FILE__)
|
20
|
+
)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Wikidata
|
2
|
+
class Entity
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
attr_accessor :hash
|
6
|
+
def_delegators :@hash, :id, :labels, :aliases, :descriptions, :sitelinks
|
7
|
+
|
8
|
+
def initialize hash
|
9
|
+
@hash = Hashie::Mash.new hash
|
10
|
+
@_properties = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def id
|
14
|
+
return hash['id'] if hash['id']
|
15
|
+
return hash['title'] if hash['title']
|
16
|
+
end
|
17
|
+
|
18
|
+
def title
|
19
|
+
return labels['en'].value if labels && labels['en']
|
20
|
+
return sitelinks['en'].value if sitelinks && sitelinks['en']
|
21
|
+
hash['title'] if hash['title']
|
22
|
+
end
|
23
|
+
|
24
|
+
def url
|
25
|
+
Wikidata.settings.item.url.gsub(':id', id)
|
26
|
+
end
|
27
|
+
|
28
|
+
Wikidata.mapping.resources.each do |k, code|
|
29
|
+
define_method k do
|
30
|
+
property code
|
31
|
+
end
|
32
|
+
define_method "#{k}_id" do
|
33
|
+
property_id code
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Wikidata.mapping.collections.each do |k, code|
|
38
|
+
define_method k do
|
39
|
+
properties code
|
40
|
+
end
|
41
|
+
define_method "#{k}_ids" do
|
42
|
+
property_ids code
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def properties code
|
47
|
+
@_properties[code] ||= Array(raw_property(code)).map {|a| Wikidata::Property.build a }
|
48
|
+
end
|
49
|
+
|
50
|
+
def property_ids code
|
51
|
+
Array(raw_property(code)).map do |attribute|
|
52
|
+
# TODO Handle other types
|
53
|
+
# http://www.wikidata.org/wiki/Wikidata:Glossary#Entities.2C_items.2C_properties_and_queries
|
54
|
+
case attribute.mainsnak.datavalue.value['entity-type']
|
55
|
+
when 'item'
|
56
|
+
prefix = 'Q'
|
57
|
+
else
|
58
|
+
raise "Unkown wikibase-item entity-type #{attribute.mainsnak.datatype.value['entity-type']}"
|
59
|
+
end
|
60
|
+
"#{prefix}#{attribute.mainsnak.datavalue.value['numeric-id']}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def property code
|
65
|
+
properties(code).first
|
66
|
+
end
|
67
|
+
|
68
|
+
def property_id code
|
69
|
+
property_ids(code).first
|
70
|
+
end
|
71
|
+
|
72
|
+
def inspect
|
73
|
+
"<#{self.class} id: #{id}, title: \"#{title}\">"
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def raw_property code
|
79
|
+
return unless hash.claims
|
80
|
+
hash.claims[code]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|