Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Fetching
- class ClubsFetcher < InfoFetcher
- URL = 'http://api.eliteprospects.com:80/beta/teams'.freeze
- LIMIT = 1000.freeze
- FIELDS = %w(name country.name locality.name latestTeamStats.league.name).freeze
- class << self
- def fetch
- raw_attrs = get_raw_clubs_attributes
- parse_clubs_attributes(raw_attrs)
- end
- private
- def get_raw_clubs_attributes(filter = {})
- attrs = []
- (0..total_count).step(LIMIT).each do |offset|
- attrs += get_attrs_part(offset, FIELDS, filter)
- end
- attrs
- end
- def parse_clubs_attributes(raw_clubs_attrs)
- clubs_attrs = []
- raw_clubs_attrs.each do |raw_attrs|
- address = [raw_attrs.fetch('country', nil)&.fetch('name', nil),
- raw_attrs.fetch('locality', nil)&.fetch('name', nil)].join(' ')
- division = raw_attrs.fetch('latestTeamStats', nil)&.fetch('league')&.fetch('name')
- club_attrs = { name: raw_attrs['name'],
- address: address,
- division: division
- }
- clubs_attrs << club_attrs
- end
- clubs_attrs
- end
- def total_count
- path = URL + '?limit=0'
- response = HTTParty.get(path)
- JSON.parse(response.body)['metadata']['totalCount']
- end
- def get_attrs_part(offset, fields, filter = {})
- _fields = fields.join(',')
- _filter = filter.map { |k, v| "#{k}=#{v}" }.join('&')
- path = URL + "?limit=#{LIMIT}&offset=#{offset}&filter=(#{_filter})&fields=#{_fields}"
- response = HTTParty.get(path)
- JSON.parse(response.body)['data']
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement