class Salah::HTTP

Constants

BASE_URI
BadContentTypeError
NoLocationError

Public Class Methods

new(key: nil) click to toggle source
# File lib/salah/http.rb, line 11
def initialize(key: nil)
  @key = key
end

Public Instance Methods

day(date, params) click to toggle source

@param [Date, String] date

The date to request prayers for, either as a Date object or a String in iso8601 format.

@param [Hash] options

A hash of options that map to PrayerTimes.date options.

@note

This method provides an interface to the [times/day.json endpoint](https://prayertimes.date/api/docs/day)
# File lib/salah/http.rb, line 55
def day(date, params)
  date = date.iso8601 if date.respond_to?(:iso8601)
  get "/v2/times/day.json", {key: @key, date: date}.merge!(params)
end
this_week(params) click to toggle source

@note

This method provides an interface to the [times/this_week.json endpoint](https://prayertimes.date/api/docs/this_week).

@return [Salah::Response]

# File lib/salah/http.rb, line 41
def this_week(params)
  get "/v2/times/this_week.json", {key: @key}.merge!(params)
end
today(params) click to toggle source

@note

This method provides an interface to the [times/today.json endpoint](https://prayertimes.date/api/docs/today).

@return [Salah::Response]

# File lib/salah/http.rb, line 21
def today(params)
  get "/v2/times/today.json", {key: @key}.merge!(params)
end
tomorrow(params) click to toggle source

@note

This method calls {#day} with tomorrow's date. Tomorrow's date is calculated relative to system time.

@return [Salah::Response]

# File lib/salah/http.rb, line 31
def tomorrow(params)
  day Date.today.next_day, params
end

Private Instance Methods

default_headers() click to toggle source
# File lib/salah/http.rb, line 100
def default_headers
  {"User-Agent" => "salah.rb v#{Salah::VERSION}"}
end
get(path, params) click to toggle source
# File lib/salah/http.rb, line 62
def get(path, params)
  params = parse_params!(params)
  response = http.request Net::HTTP::Get.new(join_path(path, params), default_headers)
  if Net::HTTPOK === response
    parse_response(response, response["content-type"])
  else
    raise BadResponseError.new("Bad response from API (#{response.class})", response)
  end
end
http() click to toggle source
# File lib/salah/http.rb, line 87
def http
  @http ||= Net::HTTP.start(BASE_URI.host, BASE_URI.port, use_ssl: true)
end
join_path(path, params) click to toggle source
# File lib/salah/http.rb, line 83
def join_path(path, params)
  [path, "?", URI.encode_www_form(params)].join
end
parse_params!(params) click to toggle source
# File lib/salah/http.rb, line 72
def parse_params!(params)
  params.delete_if { |_, v| v.nil? }
  params[:school] = Integer(params[:school] || Salah::School.find_by_name("Muslim World League"))
  params[:juristic] = Integer(params[:juristic]) if params[:juristic]
  params[:latitude] = params.delete(:lat) if params[:lat]
  params[:longitude] = params.delete(:lng) if params[:lng]
  return params if params[:city] || params[:ip]
  return params if params[:latitude] && params[:longitude]
  raise NoLocationError.new("Please provide a city, longitude and latitude coordinates or an IP address")
end
parse_response(res, content_type) click to toggle source
# File lib/salah/http.rb, line 91
def parse_response(res, content_type)
  case content_type
  when "application/json", "application/json; charset=utf-8"
    Salah::Response.new res, OpenStruct.from_hash(JSON.parse(res.body))
  else
    raise BadContentTypeError.new("The content-type '#{content_type}' is not recognized")
  end
end