Class: Outbrain::Request

Inherits:
Config
  • Object
show all
Defined in:
lib/outbrain/request.rb

Constant Summary

Constants inherited from Config

Config::BASE

Class Method Summary collapse

Methods inherited from Config

api=, api_version=

Class Method Details

.all(resource, options = {}) ⇒ Object



16
17
18
# File 'lib/outbrain/request.rb', line 16

def self.all(resource, options={})
  where(resource, {}, options)
end

.create(resource, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/outbrain/request.rb', line 35

def self.create(resource, options={})
  attributes = options.fetch(:attributes, {})

  response = api.post("/amplify/#{api_version}/#{resource}", attributes.to_json)
  json_body = JSON.parse(response.body)

  if response.status == 200
    options[:as].new(json_body)
  else
    a = options[:as].new
    a.errors.push(json_body)
    a
  end
end

.find(resource_path, id, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/outbrain/request.rb', line 20

def self.find(resource_path, id, options={})
  response = api.get("/amplify/#{api_version}/#{resource_path}/#{id}")
  json_body = JSON.parse(response.body)

  fail InvalidOption 'requires an as option' unless options[:as]

  if response.status == 200
    options[:as].new(json_body)
  else
    a = options[:as].new
    a.errors.push(json_body)
    a
  end
end

.where(resource_path, query = {}, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/outbrain/request.rb', line 4

def self.where(resource_path, query={}, options={})
  response = api.get("/amplify/#{api_version}/#{resource_path}")
  resource_name = options.fetch(:resource_name, resource_path)
  json_body = JSON.parse(response.body) # catch and raise proper api error

  fail InvalidOption 'requires an as option' unless options[:as]

  json_body[resource_name].map do |obj|
    options[:as].new(obj)
  end
end