class Medlink::Client

Public Instance Methods

post(url, params = {}) click to toggle source
# File lib/medlink/client.rb, line 4
def post(url, params = {})
  response = faraday_client.post do |req|
    req.url api_url(url)

    req.headers['Authorization']    = "Token token=\"#{token}\""
    req.headers['Content-Type']     = "application/json"
    req.headers['Accept']           = "application/json"

    req.body = JSON.generate(params)
  end

  raise UnauthorizedError, "Token '#{token}' is not valid" if response.status == 401

  json = JSON.parse response.body

  if json.is_a? Hash and json.has_key?("errors")
    error = json["errors"].first
    raise(Error, "Error #{error["code"]}: #{error["detail"]}")
  end

  json

end

Private Instance Methods

api_url(url) click to toggle source
# File lib/medlink/client.rb, line 44
def api_url(url)
  "/api/#{url}"
end
faraday_client() click to toggle source
# File lib/medlink/client.rb, line 30
def faraday_client
  @faraday_client ||= Faraday.new(url: site) do |faraday|
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end
site() click to toggle source
# File lib/medlink/client.rb, line 40
def site
  Medlink.configuration.site
end
token() click to toggle source
# File lib/medlink/client.rb, line 36
def token
  Medlink.configuration.token
end