board-game-gem 0.3.9 → 0.4.0

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: 89306ee68b3eebd59353277a88c8439157031e0d
4
- data.tar.gz: fc1411005ad46e6fbd440a837779d88fb3beaf2b
3
+ metadata.gz: fc068b33bb86956486f67fd7361578490a65ccb8
4
+ data.tar.gz: 630af575a044c82480b02fe8cfa8e3d0ca9d889d
5
5
  SHA512:
6
- metadata.gz: a41c69dc211c9529bede5d61f8338005b2460607fcd2dbc40cb2105316ded9739c6b384a38d4d1716b3a868373a44d8c9746aadd94a35a15097292f586b99144
7
- data.tar.gz: 0db46eaa50493497dc44e314db7ee388a26a835e6ac22d51fae5b26c09b1d4295a82c6d20640db47cd1dfd5598cfc3c2f449083bfc0d7fb7356948217338da33
6
+ metadata.gz: dfb84f9b20fe3ec8326402e5374f6cd0861efb887ddc6effe1ce4b3de664a9160216613f1cf266bfc947b223680dccebfe82a0767cf471100c22b9bb47dab609
7
+ data.tar.gz: 1a3abbffb70a69dcfd56de1792f121657abe45cf2635d7b38a5f5e60ec48145caba4d7fc02180505318e18fb0141b131d0ad7fa1bad27f6615989a149b07c4af
@@ -53,5 +53,13 @@ module BoardGameGem
53
53
  def get_datetime(xml, path, key)
54
54
  return DateTime.strptime(get_string(xml, path, key), '%F %T')
55
55
  end
56
+
57
+ def key_for_api(api1, api2)
58
+ @api == 2 ? api2 : api1
59
+ end
60
+
61
+ def api_key_value
62
+ key_for_api(nil, "value")
63
+ end
56
64
  end
57
65
  end
@@ -4,27 +4,40 @@ module BoardGameGem
4
4
  attr_reader :id, :type, :thumbnail, :image, :name, :description, :year_published, :min_players, :max_players,
5
5
  :playing_time, :min_playing_time, :max_playing_time, :statistics
6
6
 
7
- def initialize(xml)
7
+ def initialize(xml, api = 2)
8
+ @api = api
8
9
  if !xml.nil?
9
- @id = get_integer(xml, "item", "id")
10
- @type = get_string(xml, "item", "type")
10
+ @id = get_integer(xml, key_for_api("boardgame", "item"), key_for_api("objectid", "id"))
11
+ if api == 2
12
+ @type = get_string(xml, "item", "type")
13
+ else
14
+ if !get_value(xml, "boardgame", "subtypemismatch").nil?
15
+ if !get_value(xml, "videogameplatform").nil?
16
+ @type = "videogame"
17
+ elsif !get_value(xml, "boardgameexpansion").nil?
18
+ @type = "boardgameexpansion"
19
+ end
20
+ else
21
+ @type = "boardgame"
22
+ end
23
+ end
11
24
  @image = get_string(xml, "image")
12
25
  @thumbnail = get_string(xml, "thumbnail")
13
- @name = get_string(xml, "name[type='primary']", "value")
26
+ @name = get_string(xml, key_for_api("name", "name[type='primary']"), api_key_value)
14
27
  @alternate_names = get_strings(xml, "name[type='alternate']", "value")
15
28
  @description = get_string(xml, "description")
16
- @year_published = get_integer(xml, "yearpublished", "value")
17
- @min_players = get_integer(xml, "minplayers", "value")
18
- @max_players = get_integer(xml, "maxplayers", "value")
19
- @playing_time = get_integer(xml, "playingtime", "value")
20
- @min_playing_time = get_integer(xml, "minplaytime", "value")
21
- @max_playing_time = get_integer(xml, "maxplaytime", "value")
29
+ @year_published = get_integer(xml, "yearpublished", api_key_value)
30
+ @min_players = get_integer(xml, "minplayers", api_key_value)
31
+ @max_players = get_integer(xml, "maxplayers", api_key_value)
32
+ @playing_time = get_integer(xml, "playingtime", api_key_value)
33
+ @min_playing_time = get_integer(xml, "minplaytime", api_key_value)
34
+ @max_playing_time = get_integer(xml, "maxplaytime", api_key_value)
22
35
  @statistics = nil
23
36
  if !xml.at_css("statistics").nil?
24
37
  @statistics = {}
25
- @statistics[:user_ratings] = get_integer(xml, "usersrated", "value")
26
- @statistics[:average] = get_float(xml, "average", "value")
27
- @statistics[:bayes] = get_float(xml, "bayesaverage", "value")
38
+ @statistics[:user_ratings] = get_integer(xml, "usersrated", api_key_value)
39
+ @statistics[:average] = get_float(xml, "average", api_key_value)
40
+ @statistics[:bayes] = get_float(xml, "bayesaverage", api_key_value)
28
41
  @statistics[:ranks] = []
29
42
  xml.css("rank").each do |rank|
30
43
  rank_data = {}
@@ -35,15 +48,15 @@ module BoardGameGem
35
48
  rank_data[:bayes] = rank["bayesaverage"].to_f
36
49
  @statistics[:ranks].push(rank_data)
37
50
  end
38
- @statistics[:stddev] = get_float(xml, "stddev", "value")
39
- @statistics[:median] = get_float(xml, "median", "value")
40
- @statistics[:owned] = get_integer(xml, "owned", "value")
41
- @statistics[:trading] = get_integer(xml, "trading", "value")
42
- @statistics[:wanting] = get_integer(xml, "wanting", "value")
43
- @statistics[:wishing] = get_integer(xml, "wishing", "value")
44
- @statistics[:num_comments] = get_integer(xml, "numcomments", "value")
45
- @statistics[:num_weights] = get_integer(xml, "numweights", "value")
46
- @statistics[:average_weight] = get_integer(xml, "averageweight", "value")
51
+ @statistics[:stddev] = get_float(xml, "stddev", api_key_value)
52
+ @statistics[:median] = get_float(xml, "median", api_key_value)
53
+ @statistics[:owned] = get_integer(xml, "owned", api_key_value)
54
+ @statistics[:trading] = get_integer(xml, "trading", api_key_value)
55
+ @statistics[:wanting] = get_integer(xml, "wanting", api_key_value)
56
+ @statistics[:wishing] = get_integer(xml, "wishing", api_key_value)
57
+ @statistics[:num_comments] = get_integer(xml, "numcomments", api_key_value)
58
+ @statistics[:num_weights] = get_integer(xml, "numweights", api_key_value)
59
+ @statistics[:average_weight] = get_integer(xml, "averageweight", api_key_value)
47
60
  end
48
61
  else
49
62
  @id = 0
@@ -2,25 +2,35 @@ require 'nokogiri'
2
2
  require 'open-uri'
3
3
 
4
4
  module BoardGameGem
5
- API_ROOT = "https://www.boardgamegeek.com/xmlapi2"
5
+ API_1_ROOT = "https://www.boardgamegeek.com/xmlapi"
6
+ API_2_ROOT = "https://www.boardgamegeek.com/xmlapi2"
6
7
  MAX_ATTEMPTS = 10
7
8
 
8
- def self.get_item(id, statistics = false, options = {})
9
+ def self.get_item(id, statistics = false, api = 2, options = {})
9
10
  options[:id] = id
10
11
  options[:stats] = statistics ? 1 : 0
11
- item = BGGItem.new(BoardGameGem.request_xml("thing", options))
12
+ item = BGGItem.new(BoardGameGem.request_xml(api == 2 ? "thing" : "boardgame", options, api), api)
12
13
  item.id == 0 ? nil : item
13
14
  end
14
15
 
15
- def self.get_items(ids, statistics = false, options = {})
16
+ def self.get_items(ids, statistics = false, api = 2, options = {})
16
17
  options[:id] = ids.join(",")
17
18
  options[:stats] = statistics ? 1 : 0
18
- item_xml = BoardGameGem.request_xml("thing", options)
19
19
  item_list = []
20
- item_xml.css("item").wrap("<item_data></item_data>")
20
+ if api == 2
21
+ path = "thing"
22
+ element = "item"
23
+ else
24
+ path = "boardgame"
25
+ element = "boardgame"
26
+ end
27
+
28
+ item_xml = BoardGameGem.request_xml(path, options, api)
29
+ item_xml.css(element).wrap("<item_data></item_data>")
21
30
  item_xml.css("item_data").each do |item_data|
22
- item_list.push(BGGItem.new(item_data))
31
+ item_list.push(BGGItem.new(item_data, api))
23
32
  end
33
+
24
34
  item_list
25
35
  end
26
36
 
@@ -57,11 +67,16 @@ module BoardGameGem
57
67
 
58
68
  private
59
69
 
60
- def self.request_xml(method, hash, attempt = 0)
70
+ def self.request_xml(method, hash, api = 2)
61
71
  params = BoardGameGem.hash_to_uri(hash)
62
72
  value = BoardGameGem.retryable(tries: MAX_ATTEMPTS, on: OpenURI::HTTPError) do
63
- #p "#{API_ROOT}/#{method}?#{params}"
64
- open("#{API_ROOT}/#{method}?#{params}") do |file|
73
+ if api == 2
74
+ api_path = "#{API_2_ROOT}/#{method}?#{params}"
75
+ else
76
+ api_path = "#{API_1_ROOT}/#{method}/#{hash[:id]}?stats=#{hash[:stats] ? 1 : 0}"
77
+ end
78
+ p api_path
79
+ open(api_path) do |file|
65
80
  if file.status[0] != "200"
66
81
  sleep 0.05
67
82
  throw OpenURI::HTTPError
@@ -1,3 +1,3 @@
1
1
  module BoardGameGem
2
- VERSION = "0.3.9"
2
+ VERSION = "0.4.0"
3
3
  end
data/test.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  require_relative 'lib/board-game-gem'
2
- collection = BoardGameGem.get_collection("AcceptableIce")
2
+ #p BoardGameGem.get_items([15944, 15955, 15956]).to_s
3
+
4
+ p BoardGameGem.get_items([123260, 123265, 61001], true).map { |x| x.type }
5
+
6
+
7
+ p BoardGameGem.get_item(123260, true)
3
8
 
4
- p collection.status_of(68448)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: board-game-gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Roussel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-18 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake