module RedRoosterApi
Public Instance Methods
Source
# File lib/red_rooster_api.rb, line 14 def fetch_json(url, fetch: nil, token: nil) body = if fetch fetch.call(url, headers: RedRoosterConfig.default_headers(token: token)) else live_fetch(url, token: token) end JSON.parse(body) end
Source
# File lib/red_rooster_api.rb, line 54 def fetch_public_vouchers(fetch: nil) payload = fetch_json(RedRoosterConfig::PUBLIC_VOUCHERS_URL, fetch: fetch) Array(payload) rescue StandardError [] end
Source
# File lib/red_rooster_api.rb, line 37 def fetch_stores(fetch: nil) payload = fetch_json(RedRoosterConfig::STORE_SYNC_URL, fetch: fetch) RedRoosterStores.parse_stores(payload) end
Source
# File lib/red_rooster_api.rb, line 61 def fetch_user_vouchers(token, fetch: nil) url = "#{RedRoosterConfig::MOBILE_SERVICES_URL}customer/vouchers?status=ACTIVE" payload = fetch_json(url, fetch: fetch, token: token) list = payload["data"] Array(list).map { |entry| normalize_user_voucher(entry) } rescue StandardError [] end
Source
# File lib/red_rooster_api.rb, line 23 def live_fetch(url, token: nil) uri = URI.parse(url) request = Net::HTTP::Get.new(uri) RedRoosterConfig.default_headers(token: token).each { |key, value| request[key] = value } request["User-Agent"] = RedRoosterConfig::USER_AGENT response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end raise StandardError, "HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess) response.body end
Source
# File lib/red_rooster_api.rb, line 70 def normalize_user_voucher(entry) return entry if entry.is_a?(Hash) && entry["title"] { "title" => entry["title"] || entry["name"] || entry["description"], "description" => entry["description"] } end