wikidata-client 0.0.12 → 0.1.0.pre.rc1

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
- SHA1:
3
- metadata.gz: 1a9211669fb0e0d562d0a0770e32a4b071e920ce
4
- data.tar.gz: 79d664ca9b0838b86a0cc17094f03638df349fcb
2
+ SHA256:
3
+ metadata.gz: 3a8b1ca5db1a94adb4890a6dfa8c6dfa7eaa2e8e802b097d16e48f2932049888
4
+ data.tar.gz: 59e71242e4542335fbb4e13ff3bada7db08ba48272be638ab26cf529edae23bb
5
5
  SHA512:
6
- metadata.gz: '0733939096f548e59bf55e90ac7eac9791426168df63d343f2261bed6f3f79c3e0ee9e379d7e436161a4dfd539f288e80e8189a41ad0ca2657f18290c7638622'
7
- data.tar.gz: 38849a4352cc774affdf599573b7f125505d9a755f1ea6013588b2f8ad04f7e879864b5b8d30ad6ffd6e6cc7ae9be82cab194409d8d6d7876a3902632c0508f3
6
+ metadata.gz: 8fd41ca92856865b23e73ff5b04c96af1cec255c7852b7b33b5e226db42ec4cc8a7fd5d31e4e66c00c675e956bda34fe7b6b5859ec9b18221f0ca609b373105c
7
+ data.tar.gz: 1ccf018cd1255ad71ecaa0cc125c2d51d3d1efd45c04a2e541581cbf772630f90537ff203257e0b727b44415aba5ddb720649c4e52f6b86f9aaabb0720130064
data/README.md CHANGED
@@ -1,10 +1,6 @@
1
1
  # Wikidata API Client
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/wikidata-client.svg)](http://badge.fury.io/rb/wikidata-client)
4
- [![Build Status](https://travis-ci.org/klacointe/wikidata-client.svg?branch=master)](https://travis-ci.org/klacointe/wikidata-client)
5
- [![Code Climate](https://codeclimate.com/github/klacointe/wikidata-client/badges/gpa.svg)](https://codeclimate.com/github/klacointe/wikidata-client)
6
- [![Test Coverage](https://codeclimate.com/github/klacointe/wikidata-client/badges/coverage.svg)](https://codeclimate.com/github/klacointe/wikidata-client)
7
- [![Dependency Status](https://gemnasium.com/klacointe/wikidata-client.svg)](https://gemnasium.com/klacointe/wikidata-client)
8
4
 
9
5
 
10
6
  [Wikidata](http://www.wikidata.org/) client library for Ruby.
@@ -33,7 +29,7 @@ Wikidata.configure do |config|
33
29
  open_timeout: 2
34
30
  }
35
31
  }
36
- config.faraday = -> (builder) { builder.adapter :patron }
32
+ config.adapter = :excon
37
33
  end
38
34
  ```
39
35
 
@@ -6,7 +6,7 @@ module Wikidata
6
6
 
7
7
  attr_accessor :query, :total_pages
8
8
 
9
- def initialize query = {}
9
+ def initialize(query = {})
10
10
  @query = DEFAULT_QUERY.merge(query)
11
11
  end
12
12
 
@@ -28,9 +28,9 @@ module Wikidata
28
28
  def api
29
29
  opts = Wikidata.options.merge url: Wikidata.settings.api.url
30
30
  Faraday.new(opts) do |faraday|
31
+ faraday.adapter Wikidata.adapter
31
32
  faraday.request :url_encoded
32
33
  faraday.response :json, content_type: /\bjson$/
33
- Wikidata.faraday.call faraday
34
34
  end
35
35
  end
36
36
  end
@@ -1,23 +1,23 @@
1
1
  module Wikidata
2
2
  class Config
3
3
  class << self
4
- attr_accessor :options, :faraday, :settings, :mapping
4
+ attr_accessor :options, :adapter, :settings, :mapping
5
5
 
6
- def configure &block
6
+ def configure
7
7
  yield self
8
8
  end
9
9
  end
10
10
 
11
11
  @options = {}
12
- @faraday = ->(builder) { builder.adapter :excon }
12
+ @adapter = :excon
13
13
  @settings = Hashie::Mash.new(
14
14
  YAML.load_file(
15
- File.expand_path('../../../config/settings.yml', __FILE__)
15
+ File.expand_path('../../config/settings.yml', __dir__)
16
16
  )
17
17
  )
18
18
  @mapping = Hashie::Mash.new(
19
19
  YAML.load_file(
20
- File.expand_path('../../../config/mapping.yml', __FILE__)
20
+ File.expand_path('../../config/mapping.yml', __dir__)
21
21
  )
22
22
  )
23
23
  end
@@ -1,24 +1,23 @@
1
1
  module Wikidata
2
- class Entity
2
+ class Entity < Hashie::Mash
3
3
  extend Forwardable
4
4
 
5
- attr_accessor :hash
6
- def_delegators :@hash, :id, :labels, :aliases, :descriptions, :sitelinks
5
+ disable_warnings
7
6
 
8
- def initialize hash
9
- @hash = Hashie::Mash.new hash
7
+ def initialize(hash, opts = {})
8
+ super(hash, opts)
10
9
  @_properties = {}
11
10
  end
12
11
 
13
12
  def id
14
- return hash['id'] if hash['id']
15
- return hash['title'] if hash['title']
13
+ self['id'] || self['title']
16
14
  end
17
15
 
18
16
  def title
19
17
  return labels['en'].value if labels && labels['en']
20
18
  return sitelinks['en'].value if sitelinks && sitelinks['en']
21
- hash['title'] if hash['title']
19
+
20
+ title
22
21
  end
23
22
 
24
23
  def url
@@ -31,35 +30,35 @@ module Wikidata
31
30
  define_method k do
32
31
  resource ? property(code) : properties(code)
33
32
  end
34
- define_method (resource ? "#{k}_id" : "#{k}_ids") do
33
+ define_method(resource ? "#{k}_id" : "#{k}_ids") do
35
34
  resource ? property_id(code) : property_ids(code)
36
35
  end
37
36
  end
38
37
  end
39
38
 
40
39
  def property_keys
41
- hash.claims.keys
40
+ claims.keys
42
41
  end
43
42
 
44
- def property_name code
43
+ def property_name(code)
45
44
  Wikidata::Item.find(code).title
46
45
  end
47
46
 
48
- def properties code
49
- @_properties[code] ||= Array(raw_property(code)).map {|a| Wikidata::Property.build a }
47
+ def properties(code)
48
+ @_properties[code] ||= Array(raw_property(code)).map { |a| Wikidata::Property.build a }
50
49
  end
51
50
 
52
- def property_ids code
51
+ def property_ids(code)
53
52
  Array(raw_property(code)).map do |attribute|
54
53
  self.class.entity_id attribute
55
54
  end.compact
56
55
  end
57
56
 
58
- def property code
57
+ def property(code)
59
58
  properties(code).first
60
59
  end
61
60
 
62
- def property_id code
61
+ def property_id(code)
63
62
  property_ids(code).first
64
63
  end
65
64
 
@@ -68,17 +67,17 @@ module Wikidata
68
67
  end
69
68
 
70
69
  class << self
71
-
72
- # TODO Handle other types
70
+ # TODO: Handle other types
73
71
  # http://www.wikidata.org/wiki/Wikidata:Glossary#Entities.2C_items.2C_properties_and_queries
74
- def entity_id attribute
72
+ def entity_id(attribute)
75
73
  return unless attribute.mainsnak.datavalue
74
+
76
75
  attribute.mainsnak.datavalue.value.tap do |h|
77
76
  case h['entity-type']
78
- when 'item'
79
- return "Q#{h['numeric-id']}"
80
- else
81
- return nil
77
+ when 'item'
78
+ return "Q#{h['numeric-id']}"
79
+ else
80
+ return nil
82
81
  end
83
82
  end
84
83
  end
@@ -86,9 +85,10 @@ module Wikidata
86
85
 
87
86
  private
88
87
 
89
- def raw_property code
90
- return unless hash.claims
91
- hash.claims[code]
88
+ def raw_property(code)
89
+ return unless claims
90
+
91
+ claims[code]
92
92
  end
93
93
  end
94
94
  end
@@ -1,11 +1,12 @@
1
+ require 'wikidata/property/hash'
2
+
1
3
  module Wikidata
2
4
  module Property
3
-
4
5
  class Base
5
6
  attr_reader :property
6
7
 
7
- def initialize property
8
- @property = Hashie::Mash.new(property)
8
+ def initialize(property)
9
+ @property = Wikidata::Property::Hash.new(property)
9
10
  end
10
11
 
11
12
  def value
@@ -0,0 +1,7 @@
1
+ module Wikidata
2
+ module Property
3
+ class Hash < Hashie::Mash
4
+ disable_warnings :hash
5
+ end
6
+ end
7
+ end
@@ -2,16 +2,16 @@ module Wikidata
2
2
  module Property
3
3
  class Time < Wikidata::Property::Base
4
4
  DATE_PRECISION = {
5
- 0 => { key: nil, value: 1_000_000_000 * 365 * 24 * 3600 },
6
- 1 => { key: nil, value: 100_000_000 * 365 * 24 * 3600 },
7
- 2 => { key: nil, value: 10_000_000 * 365 * 24 * 3600 },
8
- 3 => { key: nil, value: 1_000_000 * 365 * 24 * 3600 },
9
- 4 => { key: nil, value: 100_000 * 365 * 24 * 3600 },
10
- 5 => { key: nil, value: 10_000 * 365 * 24 * 3600 },
11
- 6 => { key: nil, value: 1000 * 365 * 24 * 3600 },
12
- 7 => { key: :century, value: 100 * 365 * 24 * 3600 },
13
- 8 => { key: :decade, value: 10 * 365 * 24 * 3600 },
14
- 9 => { key: :year, value: 365 * 24 * 3600 },
5
+ 0 => { key: nil, value: 1_000_000_000 * 365 * 24 * 3600 },
6
+ 1 => { key: nil, value: 100_000_000 * 365 * 24 * 3600 },
7
+ 2 => { key: nil, value: 10_000_000 * 365 * 24 * 3600 },
8
+ 3 => { key: nil, value: 1_000_000 * 365 * 24 * 3600 },
9
+ 4 => { key: nil, value: 100_000 * 365 * 24 * 3600 },
10
+ 5 => { key: nil, value: 10_000 * 365 * 24 * 3600 },
11
+ 6 => { key: nil, value: 1000 * 365 * 24 * 3600 },
12
+ 7 => { key: :century, value: 100 * 365 * 24 * 3600 },
13
+ 8 => { key: :decade, value: 10 * 365 * 24 * 3600 },
14
+ 9 => { key: :year, value: 365 * 24 * 3600 },
15
15
  10 => { key: :month, value: 30 * 24 * 3600 },
16
16
  11 => { key: :day, value: 24 * 3600 },
17
17
  12 => { key: nil, value: 3600 },
@@ -36,13 +36,14 @@ module Wikidata
36
36
 
37
37
  def date
38
38
  return @_date if @_date
39
- d = Hash[[:year, :month, :day, :hour, :min, :sec].zip(
39
+
40
+ d = ::Hash[%i[year month day hour min sec].zip(
40
41
  value.time.scan(/(-?\d+)-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/).first.map(&:to_i)
41
42
  )]
42
- [:month, :day].each do |k|
43
+ %i[month day].each do |k|
43
44
  d[k] = (d[k] == 0 ? 1 : d[k])
44
45
  end
45
- @_date ||= DateTime.new *d.values
46
+ @_date ||= DateTime.new(*d.values)
46
47
  end
47
48
 
48
49
  def timestamp
@@ -78,12 +79,18 @@ module Wikidata
78
79
  protected
79
80
 
80
81
  def generic_range
81
- from = before > 0 ?
82
- timestamp - (before.to_i * precision) : timestamp
83
- to = after > 0 ?
84
- timestamp + (after.to_i * precision) : timestamp
85
-
86
- @_range ||= (to_datetime ::Time.at(from).utc)..(to_datetime ::Time::at(to).utc)
82
+ from = if before > 0
83
+ timestamp - (before.to_i * precision)
84
+ else
85
+ timestamp
86
+ end
87
+ to = if after > 0
88
+ timestamp + (after.to_i * precision)
89
+ else
90
+ timestamp
91
+ end
92
+
93
+ @_range ||= (to_datetime ::Time.at(from).utc)..(to_datetime ::Time.at(to).utc)
87
94
  end
88
95
 
89
96
  def century_range
@@ -117,11 +124,11 @@ module Wikidata
117
124
  end
118
125
 
119
126
  def month_range
120
- if date.month == 2 && self.class.leap_year?(date.year)
121
- last_day = 29
122
- else
123
- last_day = DAYS_IN_MONTH[date.month]
124
- end
127
+ last_day = if date.month == 2 && self.class.leap_year?(date.year)
128
+ 29
129
+ else
130
+ DAYS_IN_MONTH[date.month]
131
+ end
125
132
  from = DateTime.new(date.year, date.month, 1, 0, 0, 0)
126
133
  to = DateTime.new(date.year, date.month, last_day, 23, 59, 59)
127
134
  @_range ||= (from..to)
@@ -137,7 +144,7 @@ module Wikidata
137
144
  DateTime.new t.year, t.month, t.day, t.hour, t.min, t.sec
138
145
  end
139
146
 
140
- def self.leap_year? year
147
+ def self.leap_year?(year)
141
148
  (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0)
142
149
  end
143
150
  end
@@ -1,7 +1,8 @@
1
1
  module Wikidata
2
2
  module Property
3
- def self.build attribute
4
- return if %w{somevalue novalue}.include? attribute.mainsnak.snaktype
3
+ def self.build(attribute)
4
+ return if %w[somevalue novalue].include? attribute.mainsnak.snaktype
5
+
5
6
  case attribute.mainsnak.datatype
6
7
  when 'string', 'external-id'
7
8
  Wikidata::Property::String.new attribute
@@ -17,8 +18,8 @@ module Wikidata
17
18
  Wikidata::Property::CommonsMedia.new attribute
18
19
  when 'monolingualtext'
19
20
  Wikidata::Property::MonolingualText.new attribute
20
- when 'quantity'
21
- Wikidata::Property::Quantity.new attribute
21
+ when 'quantity'
22
+ Wikidata::Property::Quantity.new attribute
22
23
  else
23
24
  puts "Unkown property type #{attribute.mainsnak.datatype}"
24
25
  end
@@ -1,3 +1,3 @@
1
1
  module Wikidata
2
- VERSION = "0.0.12".freeze
2
+ VERSION = "0.1.0-rc1".freeze
3
3
  end
data/lib/wikidata.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'faraday'
2
- require 'faraday_middleware'
2
+ require 'faraday/excon'
3
3
  require 'hashie'
4
4
  require 'yaml'
5
5
 
@@ -7,12 +7,12 @@ require 'wikidata/config'
7
7
 
8
8
  module Wikidata
9
9
  class << self
10
- def configure &block
11
- Config.configure &block
10
+ def configure(&block)
11
+ Config.configure(&block)
12
12
  end
13
13
 
14
- def faraday
15
- Config.faraday
14
+ def adapter
15
+ Config.adapter
16
16
  end
17
17
 
18
18
  def options
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,3 @@
1
- require "simplecov"
2
- SimpleCov.start
3
-
4
1
  require 'wikidata'
5
2
  require 'vcr'
6
3
  require 'pry'
@@ -1,18 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Wikidata do
4
- let(:options) {
4
+ let(:options) do
5
5
  { request: { timeout: 10 } }
6
- }
7
- let(:builder) {
8
- -> (builder) { builder.use :excon }
9
- }
6
+ end
10
7
  it 'should be configurable' do
11
8
  Wikidata.configure do |c|
12
9
  c.options = options
13
- c.faraday = builder
10
+ c.adapter = :excon
14
11
  end
15
12
  Wikidata.options.should be options
16
- Wikidata.faraday.should be builder
13
+ Wikidata.adapter.should be :excon
17
14
  end
18
15
  end
metadata CHANGED
@@ -1,142 +1,142 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikidata-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.1.0.pre.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kévin Lacointe
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-13 00:00:00.000000000 Z
11
+ date: 2022-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ name: excon
14
15
  requirement: !ruby/object:Gem::Requirement
15
16
  requirements:
16
17
  - - "~>"
17
18
  - !ruby/object:Gem::Version
18
- version: '0.9'
19
- name: faraday
20
- prerelease: false
19
+ version: '0.92'
21
20
  type: :runtime
21
+ prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.9'
26
+ version: '0.92'
27
27
  - !ruby/object:Gem::Dependency
28
+ name: faraday
28
29
  requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - "~>"
31
32
  - !ruby/object:Gem::Version
32
- version: '0.9'
33
- name: faraday_middleware
34
- prerelease: false
33
+ version: '2.2'
35
34
  type: :runtime
35
+ prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.9'
40
+ version: '2.2'
41
41
  - !ruby/object:Gem::Dependency
42
+ name: faraday-excon
42
43
  requirement: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - "~>"
45
46
  - !ruby/object:Gem::Version
46
- version: '3.3'
47
- name: hashie
48
- prerelease: false
47
+ version: '2.1'
49
48
  type: :runtime
49
+ prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.3'
54
+ version: '2.1'
55
55
  - !ruby/object:Gem::Dependency
56
+ name: hashie
56
57
  requirement: !ruby/object:Gem::Requirement
57
58
  requirements:
58
59
  - - "~>"
59
60
  - !ruby/object:Gem::Version
60
- version: '0.40'
61
- name: excon
62
- prerelease: false
61
+ version: '5.0'
63
62
  type: :runtime
63
+ prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.40'
68
+ version: '5.0'
69
69
  - !ruby/object:Gem::Dependency
70
+ name: pry
70
71
  requirement: !ruby/object:Gem::Requirement
71
72
  requirements:
72
73
  - - "~>"
73
74
  - !ruby/object:Gem::Version
74
- version: '3.1'
75
- name: rspec
76
- prerelease: false
75
+ version: '0.10'
77
76
  type: :development
77
+ prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.1'
82
+ version: '0.10'
83
83
  - !ruby/object:Gem::Dependency
84
+ name: rake
84
85
  requirement: !ruby/object:Gem::Requirement
85
86
  requirements:
86
87
  - - "~>"
87
88
  - !ruby/object:Gem::Version
88
- version: '1.18'
89
- name: webmock
90
- prerelease: false
89
+ version: '13.0'
91
90
  type: :development
91
+ prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '1.18'
96
+ version: '13.0'
97
97
  - !ruby/object:Gem::Dependency
98
+ name: rspec
98
99
  requirement: !ruby/object:Gem::Requirement
99
100
  requirements:
100
101
  - - "~>"
101
102
  - !ruby/object:Gem::Version
102
- version: '2.9'
103
- name: vcr
104
- prerelease: false
103
+ version: '3.1'
105
104
  type: :development
105
+ prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '2.9'
110
+ version: '3.1'
111
111
  - !ruby/object:Gem::Dependency
112
+ name: vcr
112
113
  requirement: !ruby/object:Gem::Requirement
113
114
  requirements:
114
115
  - - "~>"
115
116
  - !ruby/object:Gem::Version
116
- version: '0.10'
117
- name: pry
118
- prerelease: false
117
+ version: '6.1'
119
118
  type: :development
119
+ prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0.10'
124
+ version: '6.1'
125
125
  - !ruby/object:Gem::Dependency
126
+ name: webmock
126
127
  requirement: !ruby/object:Gem::Requirement
127
128
  requirements:
128
129
  - - "~>"
129
130
  - !ruby/object:Gem::Version
130
- version: '10.3'
131
- name: rake
132
- prerelease: false
131
+ version: '3.14'
133
132
  type: :development
133
+ prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: '10.3'
139
- description:
138
+ version: '3.14'
139
+ description:
140
140
  email: kevinlacointe@gmail.com
141
141
  executables: []
142
142
  extensions: []
@@ -156,6 +156,7 @@ files:
156
156
  - lib/wikidata/property/base.rb
157
157
  - lib/wikidata/property/commons_media.rb
158
158
  - lib/wikidata/property/globe_coordinate.rb
159
+ - lib/wikidata/property/hash.rb
159
160
  - lib/wikidata/property/monolingual_text.rb
160
161
  - lib/wikidata/property/quantity.rb
161
162
  - lib/wikidata/property/string.rb
@@ -211,7 +212,7 @@ homepage: https://github.com/klacointe/wikidata-client
211
212
  licenses:
212
213
  - MIT
213
214
  metadata: {}
214
- post_install_message:
215
+ post_install_message:
215
216
  rdoc_options: []
216
217
  require_paths:
217
218
  - lib
@@ -222,13 +223,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
223
  version: '0'
223
224
  required_rubygems_version: !ruby/object:Gem::Requirement
224
225
  requirements:
225
- - - ">="
226
+ - - ">"
226
227
  - !ruby/object:Gem::Version
227
- version: '0'
228
+ version: 1.3.1
228
229
  requirements: []
229
- rubyforge_project:
230
- rubygems_version: 2.6.14
231
- signing_key:
230
+ rubygems_version: 3.1.6
231
+ signing_key:
232
232
  specification_version: 4
233
233
  summary: Wikidata API client
234
234
  test_files: