Class: Outbrain::Request
Class Method Summary collapse
- .all(resource, options = {}) ⇒ Object
- .create(resource, options = {}) ⇒ Object
- .find(resource_path, id, options = {}) ⇒ Object
-
.get(resource, attributes) ⇒ Object
token retrieval.
- .get_json(root_path, query) ⇒ Object
-
.search(path, query = {}, options = {}) ⇒ Object
For api methods that return arrays without a root element or meta info.
- .update(resource_path, id, options = {}) ⇒ Object
- .where(resource_path, query = {}, options = {}) ⇒ Object
Methods inherited from Config
Class Method Details
.all(resource, options = {}) ⇒ Object
16 17 18 |
# File 'lib/outbrain/request.rb', line 16 def self.all(resource, ={}) where(resource, {}, ) end |
.create(resource, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/outbrain/request.rb', line 56 def self.create(resource, ={}) attributes = .fetch(:attributes, {}) response = api.post("#{resource}", attributes.to_json) json_body = JSON.parse(response.body) if response.status == 200 [:as].new(json_body) else a = [:as].new a.errors.push(json_body) a end end |
.find(resource_path, id, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/outbrain/request.rb', line 30 def self.find(resource_path, id, ={}) response = api.get("#{resource_path}/#{id}") json_body = JSON.parse(response.body) fail InvalidOption 'requires an as option' unless [:as] if response.status == 200 [:as].new(json_body) else a = [:as].new a.errors.push(json_body) a end end |
.get(resource, attributes) ⇒ Object
token retrieval
21 22 23 24 25 26 27 28 |
# File 'lib/outbrain/request.rb', line 21 def self.get(resource, attributes) user_name = attributes["user_name"] || attributes[:user_name] user_password = attributes["user_password"] || attributes[:user_password] api.basic_auth user_name, user_password response = api.get(resource) json_body = JSON.parse(response.body) end |
.get_json(root_path, query) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/outbrain/request.rb', line 87 def self.get_json(root_path, query) query_string = query.map{|k,v| "#{k}=#{v}"}.join("&") path = query_string.empty? ? root_path : "#{root_path}?#{query_string}" response = api.get(path) [JSON.parse(response.body), response.status] # catch and raise proper api error end |
.search(path, query = {}, options = {}) ⇒ Object
For api methods that return arrays without a root element or meta info
46 47 48 49 50 51 52 53 54 |
# File 'lib/outbrain/request.rb', line 46 def self.search(path, query={}, ={}) json_body, status = get_json(path, query) resource_class = .fetch(:as) if status == 200 json_body.map{|e| resource_class.new(e)} else Outbrain::Api::Relation.new(errors: json_body) end end |
.update(resource_path, id, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/outbrain/request.rb', line 71 def self.update(resource_path, id, ={}) attributes = .fetch(:attributes, {}) wrap_response = .fetch(:wrap_response, true) response = api.put("#{resource_path}/#{id}", attributes.to_json) success = (response.status == 200) return success unless wrap_response json_body = JSON.parse(response.body) if success [:as].new(json_body) else a = [:as].new a.errors.push(json_body) a end end |
.where(resource_path, query = {}, options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/outbrain/request.rb', line 3 def self.where(resource_path, query={}, ={}) fail InvalidOption 'requires an as option' unless [:as] json_body, status = get_json(resource_path, query) if status == 200 resource_name = .fetch(:resource_name, resource_path) Outbrain::Api::Relation .new(json_body.merge(relation_class: [:as], relation_name: resource_name)) else Outbrain::Api::Relation.new(errors: json_body) end end |