KerbalStuff 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. data/lib/KerbalStuff.rb +79 -35
  2. metadata +2 -2
@@ -5,8 +5,6 @@ require 'uri'
5
5
  module KerbalStuff
6
6
 
7
7
  api_url = "https://kerbalstuff.com/api"
8
- search_mod = "https://kerbalstuff.com/api/search/mod?query="
9
- search_user = "https://kerbalstuff.com/api/search/user?query="
10
8
  user_url = "https://kerbalstuff.com/api/user/"
11
9
  mod_url = "https://kerbalstuff.com/api/mod/"
12
10
 
@@ -22,44 +20,77 @@ module KerbalStuff
22
20
  response
23
21
  end
24
22
 
23
+ def self.get_json(url)
24
+ response = get_https_response(url)
25
+ json = JSON.parse(response.body)
26
+
27
+ if json.is_a?(Hash)
28
+ if json.has_key? 'error'
29
+ return "error", "#{json['reason']}"
30
+ else
31
+ return json
32
+ end
33
+ end
34
+ end
35
+
25
36
  # Searches for mods with the specified keyword/phrase.
26
37
  #
27
38
  # @param query [String] the keyword/phrase to search for.
28
- # @return [Hash] A parsed JSON output containing the mods which were found.
39
+ # @return [Hash] A parsed JSON output containing the mods which were found. Will return a [String] if no results were found.
29
40
  def self.search_mod(query)
30
- response = get_https_response("#{search_mod}#{query}")
31
- JSON.parse(response.body)
41
+ res = get_json("https://kerbalstuff.com/api/search/mod?query=#{query}")
42
+
43
+ if res.length == 0
44
+ return "No results were found for '#{query}'."
45
+ else
46
+ return res
47
+ end
32
48
  end
33
49
 
34
50
  # Searches for users with the specified keyword/phrase.
35
51
  #
36
52
  # @param query [String] the keyword/phrase to search for.
37
- # @return [Hash] A parsed JSON output containing the users which were found.
53
+ # @return [Hash] A parsed JSON output containing the users which were found. Will return a [String] if no results were found.
38
54
  def self.search_user(query)
39
- response = get_https_response("#{search_user}#{query}")
40
- JSON.parse(response.body)
55
+ res = get_json("https://kerbalstuff.com/api/search/user?query=#{query}")
56
+
57
+ if res.length == 0
58
+ return "No results were found for '#{query}'."
59
+ else
60
+ return res
61
+ end
41
62
  end
42
63
 
43
64
  # Retrieves information about the specified user.
44
65
  #
45
66
  # @param username [String] the username to retrieve its information.
46
- # @return [Hash] A parsed JSON output containing the information about the user.
67
+ # @return [Hash] A parsed JSON output containing the information about the user. Will return a [String] if no users were found.
47
68
  def self.user(username)
48
69
  raise "username cannot be nil" unless username.length > 0
49
70
 
50
- response = get_https_response("#{user_url}#{username}")
51
- JSON.parse(response.body)
71
+ res = get_json("https://kerbalstuff.com/api/user/#{username}")
72
+
73
+ if res.include? 'error'
74
+ return res[1]
75
+ else
76
+ return res
77
+ end
52
78
  end
53
79
 
54
80
  # Retrieves the information about the specified mod.
55
81
  #
56
82
  # @param id [Fixnum] the id to retrieve its information.
57
- # @return [Hash] A parsed JSON output containing the information about the mod.
83
+ # @return [Hash] A parsed JSON output containing the information about the mod. Will return a [String] if no mods were found.
58
84
  def self.mod(id)
59
- raise "id cannot be nil" unless id.is_a?(Integer) and id > 0
85
+ raise "id cannot be nil" unless id.is_a?(Integer)
86
+
87
+ res = get_json("https://kerbalstuff.com/api/mod/#{id}")
60
88
 
61
- response = get_https_response("#{mod_url}#{id}")
62
- JSON.parse(response.body)
89
+ if res.include? 'error'
90
+ return res[1]
91
+ else
92
+ return res
93
+ end
63
94
  end
64
95
 
65
96
  # Retrieves the information about the last released version of the specified mod.
@@ -67,10 +98,15 @@ module KerbalStuff
67
98
  # @param username [Fixnum] the id to retrieve information of its latest version released.
68
99
  # @return [Hash] A parsed JSON output containing the information about the latest version released.
69
100
  def self.get_latest_version(id)
70
- raise "id cannot be nil" unless id.is_a?(Integer) and id > 0
101
+ raise "id cannot be nil" unless id.is_a?(Integer)
71
102
 
72
- response = get_https_response("#{mod}#{id}/latest")
73
- JSON.parse(response.body)
103
+ res = get_json("https://kerbalstuff.com/api/mod/#{id}/latest")
104
+
105
+ if res.include? 'error'
106
+ return res[1]
107
+ else
108
+ return res
109
+ end
74
110
  end
75
111
 
76
112
  # Retrieves basic mod information - name, author, description, how many times it was downloaded, url to the mod and latest version.
@@ -80,31 +116,39 @@ module KerbalStuff
80
116
  def self.get_basic_mod_info(id)
81
117
  oldHash = mod(id)
82
118
 
83
- modHash = Hash.new()
84
- modHash["name"] = oldHash["name"]
85
- modHash["author"] = oldHash["author"]
86
- modHash["description"] = oldHash["short_description"]
87
- modHash["downloads"] = oldHash["downloads"]
88
- modHash["url"] = "https://kerbalstuff.com/mod/#{id}/}"
89
- modHash["latest_version"] = oldHash["versions"][0]["friendly_version"]
90
-
91
- modHash
119
+ if oldHash.is_a?(String)
120
+ return oldHash
121
+ else
122
+ modHash = Hash.new()
123
+ modHash["name"] = oldHash["name"]
124
+ modHash["author"] = oldHash["author"]
125
+ modHash["description"] = oldHash["short_description"]
126
+ modHash["downloads"] = oldHash["downloads"]
127
+ modHash["url"] = "https://kerbalstuff.com/mod/#{id}/}"
128
+ modHash["latest_version"] = oldHash["versions"][0]["friendly_version"]
129
+
130
+ return modHash
131
+ end
92
132
  end
93
133
 
94
134
  # Retrieves basic user info - username, mods, irc nick and forum username.
95
135
  #
96
136
  # @param username [String] The user to gather information for.
97
137
  # @return [Hash] A hash containing the basic user info.
98
- def self.get_basic_user_info(string)
99
- oldHash = user(string)
138
+ def self.get_basic_user_info(username)
139
+ oldHash = user(username)
100
140
 
101
- userHash = Hash.new()
102
- userHash["username"] = oldHash["username"]
103
- userHash["mods"] = oldHash["mods"]
104
- userHash["ircNick"] = oldHash["ircNick"]
105
- userHash["forumusername"] = oldHash["forumusername"]
141
+ if oldHash.is_a?(String)
142
+ return oldHash
143
+ else
144
+ userHash = Hash.new()
145
+ userHash["username"] = oldHash["username"]
146
+ userHash["mods"] = oldHash["mods"]
147
+ userHash["ircNick"] = oldHash["ircNick"]
148
+ userHash["forumusername"] = oldHash["forumusername"]
106
149
 
107
- userHash
150
+ return userHash
151
+ end
108
152
  end
109
153
 
110
154
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: KerbalStuff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-22 00:00:00.000000000 Z
12
+ date: 2014-09-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple API wrapper for KerbalStuff
15
15
  email: rockytvbr@gmail.com