Class: Feed2Gram::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/feed2gram/http.rb

Constant Summary collapse

BASE =
"https://graph.facebook.com/v18.0".freeze

Class Method Summary collapse

Class Method Details

.get(path, params = {}) ⇒ Object



8
9
10
# File 'lib/feed2gram/http.rb', line 8

def self.get(path, params = {})
  send(path, :get, params)
end

.post(path, params = {}) ⇒ Object



12
13
14
# File 'lib/feed2gram/http.rb', line 12

def self.post(path, params = {})
  send(path, :post, params)
end

.send(path, method, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/feed2gram/http.rb', line 16

def self.send(path, method, params = {})
  uri = URI("#{BASE}#{path}")
  uri.query = URI.encode_www_form(params)
  res = (method == :get) ? Net::HTTP.get_response(uri) : Net::HTTP.post_form(uri, {})
  data = JSON.parse(res.body, symbolize_names: true)

  if res.is_a?(Net::HTTPSuccess)
    data
  else
    raise Error, data[:error]
  end
end