Module: Grabbio::Utils

Included in:
Client
Defined in:
lib/grabbio/utils.rb

Instance Method Summary collapse

Instance Method Details

#make_request(request) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grabbio/utils.rb', line 26

def make_request(request)
  url = URI.parse(URI.escape(request))
  body = Net::HTTP.get_response(url).body
  begin
    result = JSON.parse(body)
  rescue
    result = {'error' => 'JSON Parse Error (grabb.io got it wrong)'}
  end

  if !result["error"].nil?
    raise GrabbioError.new(result["error"])
  elsif !result["errors"].nil?
    result["errors"].each do |k,v|
      raise GrabbioError.new("#{k} #{v}")
    end
  else
    result
  end


end

#parameters_to_string(parameters = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/grabbio/utils.rb', line 7

def parameters_to_string(parameters = {})
  first = true
  args = ""
  parameters.each do |k,v|
    if first
      first = false
      args += "?"
    else
      args += "&"
    end
    args += "#{k}=#{v}"
  end
  args
end

#sign_request(secret_key, parameters) ⇒ Object



22
23
24
# File 'lib/grabbio/utils.rb', line 22

def sign_request(secret_key, parameters)
   Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key,parameters)).chomp.gsub(/\n/,'')
end