Module: Zenlight::Requester

Included in:
Instance
Defined in:
lib/zenlight/requester.rb

Instance Method Summary collapse

Instance Method Details

#download(_url) ⇒ Object



38
39
40
# File 'lib/zenlight/requester.rb', line 38

def download _url
  open(_url).read.force_encoding("UTF-8")
end

#get(_path, _params = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zenlight/requester.rb', line 8

def get _path, _params = nil
  setup = {
    method: __callee__.to_s,
    url: "https://#{self.config.subdomain}.zendesk.com" << _path,
    user: "#{self.config.email}/token",
    password: self.config.token,
    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