class BerlinBuehnen::Client

Constants

API_FORMAT
API_KEY_PARAM_NAME
API_USERNAME_PARAM_NAME
API_VERSION
DEFAULT_OPTIONS
USER_AGENT

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/berlin_buehnen/client.rb, line 17
def initialize(options = {})
  store_options(options)
  raise ArgumentError, "An api key must be present" if api_key.nil?
  raise ArgumentError, "An api username must be present" if api_username.nil?
end

Public Instance Methods

api_host() click to toggle source
# File lib/berlin_buehnen/client.rb, line 49
def api_host
  host
end
api_key() click to toggle source
# File lib/berlin_buehnen/client.rb, line 40
def api_key
  @options[:api_key]
end
api_language() click to toggle source
# File lib/berlin_buehnen/client.rb, line 53
def api_language
  @options[:language]
end
api_url() click to toggle source
# File lib/berlin_buehnen/client.rb, line 57
def api_url
  [api_host, api_language, "api", API_VERSION].join("/")
end
api_username() click to toggle source

accessors for options

# File lib/berlin_buehnen/client.rb, line 36
def api_username
  @options[:username]
end
get(path, query={}, options={}) click to toggle source
# File lib/berlin_buehnen/client.rb, line 23
def get(path, query={}, options={})
  handle_response {
    self.class.get(*construct_query_arguments(path, options.merge(:query => query)))
  }
end
head(path, query={}, options={}) click to toggle source
# File lib/berlin_buehnen/client.rb, line 29
def head(path, query={}, options={})
  handle_response {
    self.class.head(*construct_query_arguments(path, options.merge(:query => query)))
  }
end
host()
Alias for: site
site() click to toggle source
# File lib/berlin_buehnen/client.rb, line 44
def site
  @options[:site]
end
Also aliased as: host

Private Instance Methods

construct_query_arguments(path_or_uri, options={}, body_or_query=:query) click to toggle source
# File lib/berlin_buehnen/client.rb, line 77
def construct_query_arguments(path_or_uri, options={}, body_or_query=:query)
  uri = URI.parse(path_or_uri)
  path = uri.path
  scheme = "http"
  options = options.dup
  options[body_or_query] ||= {}
  options[body_or_query][:format] = API_FORMAT
  options[body_or_query][API_USERNAME_PARAM_NAME.to_sym] = api_username
  options[body_or_query][API_KEY_PARAM_NAME.to_sym] = api_key

  [
    "#{scheme}://#{api_url}#{path}#{uri.query ? "?#{uri.query}" : ""}",
    options
  ]
end
handle_response(&block) click to toggle source
# File lib/berlin_buehnen/client.rb, line 63
def handle_response(&block)
  response = block.call
  return unless response

  raise ResponseError.new(response) unless response.success?

  BerlinBuehnen::Response.create(response)
end
store_options(options={}) click to toggle source
# File lib/berlin_buehnen/client.rb, line 72
def store_options(options={})
  @options ||= DEFAULT_OPTIONS.dup
  @options.merge!(options)
end