Module: FitnessparkApi

Defined in:
lib/fitnesspark_api.rb,
lib/fitnesspark_api/version.rb,
lib/fitnesspark_api/http_client.rb

Defined Under Namespace

Modules: HttpClient

Constant Summary collapse

ResponseError =
Class.new(IOError)
STORES_URL =
'https://web-api.migros.ch/widgets/stores'
VISITORS_URL =
'https://blfa-api.migros.ch/fp/api/center/%<center_id>s/currentuser'
VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.api_keyObject



68
69
70
# File 'lib/fitnesspark_api.rb', line 68

def api_key
  ENV['FITNESSPARK_SESSION_KEY'] || HttpClient.api_key
end

.base_data_by_name(slug) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fitnesspark_api.rb', line 49

def base_data_by_name(slug)
  all_base_data = stores
  result = all_base_data['stores'].find do |store_hash|
    store_hash['name'] == slug ||
      store_hash['id'] == slug ||
      store_hash['slug'] == slug ||
      store_hash.dig('localized_slugs', 'de') == slug ||
      store_hash.dig('localized_slugs', 'it') == slug ||
      store_hash.dig('localized_slugs', 'fr') == slug ||
      store_hash['additional_slugs'].include?(slug) ||
      store_hash['additional_ids'].include?(slug)
  end

  return result if result

  all_slugs = stores['stores'].map { |s| s['slug'] }.uniq.sort
  raise(ArgumentError, "Unable to find #{slug.inspect}. Did you mean one of these: #{all_slugs.inspect}")
end

.center_volume_id(slug) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
# File 'lib/fitnesspark_api.rb', line 40

def center_volume_id(slug)
  base_data = base_data_by_name(slug)
  detail_data = store(base_data['id'])
  markets = detail_data['markets']
  raise(ArgumentError, "Found more than one center for slug #{slug}") unless markets.one?

  HttpClient.get_center_volume_id(markets.first['weblink']['url'])
end

.configObject



36
37
38
# File 'lib/fitnesspark_api.rb', line 36

def config
  YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'config.yml'))
end

.store(center_id) ⇒ Object



23
24
25
26
27
28
# File 'lib/fitnesspark_api.rb', line 23

def store(center_id)
  get_params = { 'key' => api_key }
  uri = URI(File.join(STORES_URL, center_id))
  uri.query = URI.encode_www_form(get_params)
  HttpClient.get_json(uri)
end

.stores(get_params = nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/fitnesspark_api.rb', line 14

def stores(get_params = nil)
  get_params ||= config.dig('stores', 'default_params')
  get_params['key'] ||= api_key

  uri = URI(STORES_URL)
  uri.query = URI.encode_www_form(get_params)
  HttpClient.get_json(uri)
end

.visitors(center_id) ⇒ Object



30
31
32
33
34
# File 'lib/fitnesspark_api.rb', line 30

def visitors(center_id)
  center_id = center_volume_id(center_id) if center_id.is_a?(String) && !center_id.match(/\A\d+\z/)
  uri = URI(format(VISITORS_URL, center_id: center_id))
  HttpClient.get_json(uri)
end