Module: Googol::Requestable

Included in:
Authenticable, YoutubeResource
Defined in:
lib/googol/requestable.rb

Overview

Provides methods to send HTTP requests to Google V3 API

Instance Method Summary collapse

Instance Method Details

#request!(params = {}) ⇒ Object

Note:

Not the best code quality, feel free to refactor!

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



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
46
47
48
49
50
51
52
53
54
55
# File 'lib/googol/requestable.rb', line 18

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 :post
    if params[:json]
      Net::HTTP::Post.new params[:path], initheader = {'Content-Type' =>'application/json'}
    else
      Net::HTTP::Post.new params[:path]
    end
  when :delete
    Net::HTTP::Delete.new params[:path]
  when :put
    if params[:json]
      Net::HTTP::Put.new params[:path], initheader = {'Content-Type' =>'application/json'}
    end
  else
    Net::HTTP::Get.new params[:path]
  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 response.code == params.fetch(:code, 200).to_s
    body ? deep_symbolize_keys(body) : true
  else
    raise RequestError, body
  end
end

#symbolize(value) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/googol/requestable.rb', line 60

def symbolize(value)
  case value
    when Hash then deep_symbolize_keys value
    when Array then value.map{|item| symbolize item}
    else value
  end
end