Module: Vm::Requestable

Included in:
Authenticable, VimeoResource
Defined in:
lib/vm/requestable.rb

Overview

Provides methods to send HTTP requests to Vimeo V3 API

Instance Method Summary collapse

Instance Method Details

#request!(params = {}) ⇒ Object

Executes an HTTP request against the Vimeo V3 API and returns the parsed result or raise an error in case of failure


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vm/requestable.rb', line 15

def request!(params = {})
  url = URI.parse params[:host]
  http = Net::HTTP.new url.host, url.port
  http.use_ssl = true
  request = case params[:method]
    when :get then Net::HTTP::Get.new params[:path]
    when :post then
      if params[:json]
        Net::HTTP::Post.new params[:path], initheader = {'Content-Type' =>'application/json'}
      else
        Net::HTTP::Post.new params[:path]
      end
  end
  if params[:json]
    request.body = params[:body].to_json
  else
    request.set_form_data params[:body]
  end if params[:body]

  request['Authorization'] = 'Bearer ' + params[:auth] if params[:auth]
  response = http.request(request)

  body = JSON.parse response.body if response.body

  if params[:valid_if] ? params[:valid_if].call(response, body) : true
    body = params[:extract].call body if params[:extract]
    body ? deep_symbolize_keys(body) : true
  else
    raise RequestError, body
  end
end