class Riksteatern::Api

Attributes

config[RW]

Public Class Methods

new(config) click to toggle source
# File lib/riksteatern/api.rb, line 10
def initialize(config)
  @config = config

  if @config.username.nil? || @config.password.nil?
    raise UnauthorizedUser, "Blank username/password"
  end
end

Public Instance Methods

get(path, params = {}) click to toggle source
# File lib/riksteatern/api.rb, line 34
def get(path, params = {})
  http_get prepared_uri(path, params)
end
get_array(endpoint, field_name, params) click to toggle source
# File lib/riksteatern/api.rb, line 30
def get_array(endpoint, field_name, params)
  get_json(endpoint, params)[field_name] || []
end
get_json(endpoint, params = {}) click to toggle source
# File lib/riksteatern/api.rb, line 38
def get_json(endpoint, params = {})
  load_json get("#{endpoint}.json", params) || "{}"
end
locations(params = {}) click to toggle source
# File lib/riksteatern/api.rb, line 18
def locations(params = {})
  get_array(:lokal, 'locations', params)
end
productions(params = {}) click to toggle source
# File lib/riksteatern/api.rb, line 22
def productions(params = {})
  get_array(:produktion, 'productions', params)
end
repertoires(params = {}) click to toggle source
# File lib/riksteatern/api.rb, line 26
def repertoires(params = {})
  get_array(:repertoar, 'repertoires', params)
end

Private Instance Methods

endpoint_uri(endpoint) click to toggle source
# File lib/riksteatern/api.rb, line 60
def endpoint_uri(endpoint)
  URI.parse("#{@config.base_url}/#{endpoint}")
end
http_get(uri) click to toggle source
# File lib/riksteatern/api.rb, line 44
def http_get(uri)
  uri.userinfo = URI.escape "#{@config.username}:#{@config.password}", '@'

  @config.http_client.get(uri)
end
load_json(doc) click to toggle source
# File lib/riksteatern/api.rb, line 50
def load_json(doc)
  @config.json_parser.call(doc)
end
prepared_uri(endpoint, params = {}) click to toggle source
# File lib/riksteatern/api.rb, line 54
def prepared_uri(endpoint, params = {})
  endpoint_uri(endpoint).tap do |uri|
    uri.query = URI.encode_www_form(params)
  end
end