board-game-gem 0.3.6 → 0.3.7

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: 176f0892877f5d69ed9f39d40b45f7629f4f894b
4
- data.tar.gz: 319eb3f37f20f0c2e1edd1f72120a181459b2229
3
+ metadata.gz: 2c3e0fa45e3189984d4d424a96ce0456fff79e6d
4
+ data.tar.gz: ad86d9f0acb69d0868e0832faf22825ac2285bd8
5
5
  SHA512:
6
- metadata.gz: 7706f8ca646f2219609fe99721d64d2dafd09bade34d7e4135512b7528912be95d0ff3bba8ad6be4972b9a12aeb3e20e5a3b4c04a1ea75335b1d7227342bdcd7
7
- data.tar.gz: 99826cab898a1ab99c90e3dcbdfb5709f66ec6d0f07af0af51bdfaf81b05052069d6373bec7aded21dcad2bd4100b95b2ea3d5f63f8f77e521403aad9d3f5700
6
+ metadata.gz: 7186b16e34ab46758272f8f52f3e4ddc0a05ad8b8d12ae34b4b5c0d86e30f73887644802142edd1f175b9aa9d7eb657a2c546e02da4050fbf01796e9e94d2fff
7
+ data.tar.gz: 0dd412ee4a6e553d0b245ac51c6a4891113d6e93c579cd20aaa7c5736f40b5cf6f134ef94000cc34d76d7c14347e4d9d4dac636630c733c1472792966981cb9d
@@ -2,42 +2,54 @@ require 'nokogiri'
2
2
  require 'open-uri'
3
3
 
4
4
  module BoardGameGem
5
- API_ROOT = "http://www.boardgamegeek.com/xmlapi2"
5
+ API_ROOT = "https://www.boardgamegeek.com/xmlapi2"
6
6
  MAX_ATTEMPTS = 10
7
7
 
8
- def BoardGameGem.get_item(id, statistics = false, options = {})
8
+ def self.get_item(id, statistics = false, options = {})
9
9
  options[:id] = id
10
10
  options[:stats] = statistics ? 1 : 0
11
11
  item = BGGItem.new(BoardGameGem.request_xml("thing", options))
12
- return item.id == 0 ? nil : item
12
+ item.id == 0 ? nil : item
13
13
  end
14
14
 
15
- def BoardGameGem.get_family(id, options = {})
15
+ def self.get_items(ids, statistics = false, options = {})
16
+ options[:id] = ids.join(",")
17
+ options[:stats] = statistics ? 1 : 0
18
+ item_xml = BoardGameGem.request_xml("thing", options)
19
+ item_list = []
20
+ item_xml.css("item").wrap("<item_data></item_data>")
21
+ item_xml.css("item_data").each do |item_data|
22
+ item_list.push(BGGItem.new(item_data))
23
+ end
24
+ item_list
25
+ end
26
+
27
+ def self.get_family(id, options = {})
16
28
  options[:id] = id
17
29
  family = BGGFamily.new(BoardGameGem.request_xml("family", options))
18
- return family.id == 0 ? nil : family
30
+ family.id == 0 ? nil : family
19
31
  end
20
32
 
21
- def BoardGameGem.get_user(username, options = {})
33
+ def self.get_user(username, options = {})
22
34
  options[:name] = username
23
35
  user = BGGUser.new(BoardGameGem.request_xml("user", options))
24
- return user.id == 0 ? nil : user
36
+ user.id == 0 ? nil : user
25
37
  end
26
38
 
27
- def BoardGameGem.get_collection(username, options = {})
39
+ def self.get_collection(username, options = {})
28
40
  options[:username] = username
29
41
  collection_xml = BoardGameGem.request_xml("collection", options)
30
42
  if collection_xml.css("error").length > 0
31
- return nil
43
+ nil
32
44
  else
33
- return BGGCollection.new(collection_xml)
45
+ BGGCollection.new(collection_xml)
34
46
  end
35
47
  end
36
48
 
37
- def BoardGameGem.search(query, options = {})
49
+ def self.search(query, options = {})
38
50
  options[:query] = query
39
51
  xml = BoardGameGem.request_xml("search", options)
40
- return {
52
+ {
41
53
  :total => xml.at_css("items")["total"].to_i,
42
54
  :items => xml.css("item").map { |x| BGGSearchResult.new(x) }
43
55
  }
@@ -45,31 +57,44 @@ module BoardGameGem
45
57
 
46
58
  private
47
59
 
48
- def BoardGameGem.request_xml(method, hash, attempt = 0)
60
+ def self.request_xml(method, hash, attempt = 0)
49
61
  params = BoardGameGem.hash_to_uri(hash)
50
- open("#{API_ROOT}/#{method}?#{params}") do |file|
51
- if file.status == 202
52
- if attempt < MAX_ATTEMPTS
62
+ value = BoardGameGem.retryable(tries: MAX_ATTEMPTS, on: OpenURI::HTTPError) do
63
+ open("#{API_ROOT}/#{method}?#{params}") do |file|
64
+ if file.status[0] != "200"
53
65
  sleep 0.05
54
- BoardGameGem.request_xml(method, hash, attempt + 1)
66
+ throw OpenURI::HTTPError
55
67
  else
56
- return nil
68
+ value = Nokogiri::XML(file.read)
57
69
  end
58
- else
59
- Nokogiri::XML(file.read)
60
70
  end
61
- end
71
+ end
72
+ value
62
73
  end
63
74
 
64
- def BoardGameGem.hash_to_uri(hash)
75
+ def self.hash_to_uri(hash)
65
76
  return hash.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
66
77
  end
78
+
79
+ def self.retryable(options = {}, &block)
80
+ opts = { :tries => 1, :on => Exception }.merge(options)
81
+
82
+ retry_exception, retries = opts[:on], opts[:tries]
83
+
84
+ begin
85
+ return yield
86
+ rescue retry_exception
87
+ retry if (retries -= 1) > 0
88
+ end
89
+
90
+ yield
91
+ end
67
92
  end
68
93
 
69
- require 'bgg_base'
70
- require 'bgg_item'
71
- require 'bgg_family'
72
- require 'bgg_user'
73
- require 'bgg_collection'
74
- require 'bgg_collection_item'
75
- require 'bgg_search_result'
94
+ require_relative 'bgg_base'
95
+ require_relative 'bgg_item'
96
+ require_relative 'bgg_family'
97
+ require_relative 'bgg_user'
98
+ require_relative 'bgg_collection'
99
+ require_relative 'bgg_collection_item'
100
+ require_relative 'bgg_search_result'
@@ -1,3 +1,3 @@
1
1
  module BoardGameGem
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
data/test.rb CHANGED
@@ -1,2 +1,2 @@
1
- require_relative 'lib/boardgamegem'
1
+ require_relative 'lib/board-game-gem'
2
2
  p BoardGameGem.get_items([1,2,3,4,5])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: board-game-gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Roussel
@@ -76,7 +76,6 @@ files:
76
76
  - lib/bgg_search_result.rb
77
77
  - lib/bgg_user.rb
78
78
  - lib/board-game-gem.rb
79
- - lib/boardgamegem.rb
80
79
  - lib/boardgamegem/version.rb
81
80
  - test.rb
82
81
  homepage: http://www.github.com/acceptableice/boardgamegem
data/lib/boardgamegem.rb DELETED
@@ -1,100 +0,0 @@
1
- require 'nokogiri'
2
- require 'open-uri'
3
-
4
- module BoardGameGem
5
- API_ROOT = "https://www.boardgamegeek.com/xmlapi2"
6
- MAX_ATTEMPTS = 10
7
-
8
- def self.get_item(id, statistics = false, options = {})
9
- options[:id] = id
10
- options[:stats] = statistics ? 1 : 0
11
- item = BGGItem.new(BoardGameGem.request_xml("thing", options))
12
- item.id == 0 ? nil : item
13
- end
14
-
15
- def self.fetch_items(ids, statistics = false, options = {})
16
- options[:id] = ids.join(",")
17
- options[:stats] = statistics ? 1 : 0
18
- item_xml = BoardGameGem.request_xml("thing", options)
19
- item_list = []
20
- item_xml.css("item").wrap("<item_data></item_data>")
21
- item_xml.css("item_data").each do |item_data|
22
- item_list.push(BGGItem.new(item_data))
23
- end
24
- item_list
25
- end
26
-
27
- def self.get_family(id, options = {})
28
- options[:id] = id
29
- family = BGGFamily.new(BoardGameGem.request_xml("family", options))
30
- family.id == 0 ? nil : family
31
- end
32
-
33
- def self.get_user(username, options = {})
34
- options[:name] = username
35
- user = BGGUser.new(BoardGameGem.request_xml("user", options))
36
- user.id == 0 ? nil : user
37
- end
38
-
39
- def self.get_collection(username, options = {})
40
- options[:username] = username
41
- collection_xml = BoardGameGem.request_xml("collection", options)
42
- if collection_xml.css("error").length > 0
43
- nil
44
- else
45
- BGGCollection.new(collection_xml)
46
- end
47
- end
48
-
49
- def self.search(query, options = {})
50
- options[:query] = query
51
- xml = BoardGameGem.request_xml("search", options)
52
- {
53
- :total => xml.at_css("items")["total"].to_i,
54
- :items => xml.css("item").map { |x| BGGSearchResult.new(x) }
55
- }
56
- end
57
-
58
- private
59
-
60
- def self.request_xml(method, hash, attempt = 0)
61
- params = BoardGameGem.hash_to_uri(hash)
62
- value = BoardGameGem.retryable(tries: MAX_ATTEMPTS, on: OpenURI::HTTPError) do
63
- open("#{API_ROOT}/#{method}?#{params}") do |file|
64
- if file.status[0] != "200"
65
- sleep 0.05
66
- throw OpenURI::HTTPError
67
- else
68
- value = Nokogiri::XML(file.read)
69
- end
70
- end
71
- end
72
- value
73
- end
74
-
75
- def self.hash_to_uri(hash)
76
- return hash.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
77
- end
78
-
79
- def self.retryable(options = {}, &block)
80
- opts = { :tries => 1, :on => Exception }.merge(options)
81
-
82
- retry_exception, retries = opts[:on], opts[:tries]
83
-
84
- begin
85
- return yield
86
- rescue retry_exception
87
- retry if (retries -= 1) > 0
88
- end
89
-
90
- yield
91
- end
92
- end
93
-
94
- require_relative 'bgg_base'
95
- require_relative 'bgg_item'
96
- require_relative 'bgg_family'
97
- require_relative 'bgg_user'
98
- require_relative 'bgg_collection'
99
- require_relative 'bgg_collection_item'
100
- require_relative 'bgg_search_result'