Module: DeskLight::Requester

Defined in:
lib/desk_light/requester.rb

Class Method Summary collapse

Class Method Details

.download(_url, _authenticate = true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/desk_light/requester.rb', line 27

def self.download _url, _authenticate = true
  uri = URI.parse(_url)
  path = uri.path
  path << "?#{uri.query}" if (uri.query || "").strip != ""
  if _authenticate
    consumer = OAuth::Consumer.new(
      DeskLight.config.key,
      DeskLight.config.secret,
      scheme: :header
    )
    access_token = OAuth::AccessToken.from_hash(
      consumer,
      oauth_token: DeskLight.config.api_token,
      oauth_token_secret: DeskLight.config.api_secret
    )
    response = access_token.get(_url)
  else
    http = Net::HTTP.new(uri.host,uri.port)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.use_ssl = DeskLight.config.uri.scheme == 'https'
    request = Net::HTTP::Get.new(path)
    response = http.request(request)
  end
  if location = response['location']
    return self.download(location,false)
  end
  DeskLight::Response.new(response)
end

.get(_path, _params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/desk_light/requester.rb', line 8

def self.get _path, _params = {}
  _uri = URI.parse("https://#{DeskLight.config.subdomain}.desk.com" << _path)
  _new_params = CGI.parse(_uri.query || "").merge!(_params)
  _url = "#{_uri.scheme}://#{_uri.host}#{_uri.path}"
  _url << "?#{URI.encode_www_form(_new_params)}" if _new_params.count > 0
  consumer = OAuth::Consumer.new(
    DeskLight.config.key,
    DeskLight.config.secret,
    :scheme => :header
  )
  access_token = OAuth::AccessToken.from_hash(
    consumer,
    :oauth_token => DeskLight.config.api_token,
    :oauth_token_secret => DeskLight.config.api_secret
  )
  DeskLight::Response.new(access_token.get(_url))
end