Class: Starcraft2::Utils
- Inherits:
-
Object
- Object
- Starcraft2::Utils
- Defined in:
- lib/starcraft2/utils.rb
Class Method Summary collapse
- .get_json ⇒ Object
- .load(klass, options, class_map = {}, build_map = {}) ⇒ Object
- .underscore(string) ⇒ Object
Class Method Details
.get_json ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/starcraft2/utils.rb', line 24 def self.get_json response = yield case response.code when 200 body = JSON.parse(response.body) case body['code'] when 404 raise Starcraft2::NotFoundError, body['message'] || body['reason'] when 500 raise Starcraft2::ApiError, body['message'] || body['reason'] else body end when 404 raise Starcraft2::NotFoundError, 'Record not found.' when 500 raise Starcraft2::ApiError, 'An API error occurred.' end end |
.load(klass, options, class_map = {}, build_map = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/starcraft2/utils.rb', line 13 def self.load(klass, , class_map = {}, build_map = {}) .each do |k, v| k = self.underscore(k.to_s) k = k.to_sym v = class_map[k].new(v) if class_map.keys.include?(k) v = build_map[k].build(v) if build_map.keys.include?(k) klass.send(:"#{k}=", v) end end |
.underscore(string) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/starcraft2/utils.rb', line 3 def self.underscore(string) word = string.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') word.tr!('-', '_') word.downcase! word end |