module Zenlight::Requester

Public Instance Methods

download(_url) click to toggle source
# File lib/zenlight/requester.rb, line 40
def download _url
  open(_url).read.force_encoding("UTF-8")
end
get(_path, _params = nil) click to toggle source
# File lib/zenlight/requester.rb, line 8
def get _path, _params = nil
  _user = "#{self.config.email}"
  _user << "/token" if self.config.token
  setup = {
    method: __callee__.to_s,
    url: "https://#{self.config.subdomain}.zendesk.com" << _path,
    user: _user,
    password: self.config.token || self.config.password,
    headers: {
      accept: :json,
      content_type: :json
    }
  }
  case __callee__.to_s.downcase
  when "get","post","put","patch"
    if setup[:url].include?('upload') || setup[:url].include?('/attachments')
      setup[:headers][:content_type] = 'application/*'
      setup[:payload] = _params
    else
      setup[:headers][:content_type] = :json
      setup.merge!(payload: _params.to_json) if _params
    end
    setup[:headers][:content_type] = 'application/merge-patch+json' if __callee__.to_s.downcase == "patch"
  end
  prepared = RestClient::Request.new(setup)
  begin
    response = Zenlight::Response.new(prepared.execute)
  rescue RestClient::ExceptionWithResponse => e
    response = Zenlight::Response.new(e.response)
  end
  response
end