module Kf5Api::Base

Public Instance Methods

basic_auth(user_name = nil, password = nil, use_password = false) click to toggle source
# File lib/kf5_api/base.rb, line 30
def basic_auth(user_name = nil, password = nil, use_password = false)
  user_name ||= use_password ? Kf5Api.config.user_name : "#{Kf5Api.config.user_name}/token"
  password ||= use_password ? Kf5Api.config.password : Kf5Api.config.token
  { username: user_name, password: password }
end
get(action, query_params = {}) click to toggle source
# File lib/kf5_api/base.rb, line 6
def get(action, query_params = {})
  url = Kf5Api.server + action
  query_params = query_params.inject({}){ |memo, (k,v)| memo[k.to_s] = v; memo }

  response = HTTParty.get(url, query: query_params, basic_auth: basic_auth, header: { 'Content-Type' => 'application/json' })

  unless response.code == 200
    Kf5Api.logger.error "[Kf5Api] url: #{url}, status: #{response.code}, body: #{response.parsed_response}"
  end

  response
end
put(action, body) click to toggle source
# File lib/kf5_api/base.rb, line 19
def put(action, body)
  url = Kf5Api.server + action
  response = HTTParty.put(url, body: body.to_json, basic_auth: basic_auth, header: { 'Content-Type' => 'application/json' })

  unless response.code == 200
    Kf5Api.logger.error "[Kf5Api] url: #{url}, status: #{response.code}, body: #{response.parsed_response}"
  end

  response
end