class FootballApi::Competition
Attributes
id[RW]
Response sample : [
{ "id":"1064", "name":"Superliga", "region":"Albania" } ...
]
name[RW]
Response sample : [
{ "id":"1064", "name":"Superliga", "region":"Albania" } ...
]
region[RW]
Response sample : [
{ "id":"1064", "name":"Superliga", "region":"Albania" } ...
]
Public Class Methods
all()
click to toggle source
# File lib/football_api/competition.rb, line 17 def self.all response && response.map do |comp| new(comp) end end
new(hash = {})
click to toggle source
# File lib/football_api/competition.rb, line 27 def initialize(hash = {}) @id = hash[:id] @name = hash[:name] @region = hash[:region] end
where(options = {})
click to toggle source
# File lib/football_api/competition.rb, line 23 def self.where(options = {}) response.select{ |c| matches_options(c, options) }.map{ |hsh| new(hsh) } end
Private Instance Methods
matches_options(object, options)
click to toggle source
Matches the two given hashes. For example, if an options hash has:
=> {id:1, name: 'ze'}
And the object has:
=> {id:1, name: 'ze', region: 'beira'}
This function returns the reduce(:&) from the array:
- true, true, true
-
-> true
Where the two first trues are from *id == id* and *name == name* and since the options hash has no key :region, it returns true since that's a non searched option
# File lib/football_api/competition.rb, line 48 def matches_options(object, options) options.keys.map{ |key| object.has_key?(key) ? options[key] == object[key] : true }.reduce(:&) end