Class: Stretchr::Request
- Inherits:
-
Object
- Object
- Stretchr::Request
- Defined in:
- lib/stretchr/request.rb
Overview
The request class is where most of the url building magic takes place It collects all methods passed to it and uses them to build a url for you
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
-
#at(path) ⇒ Object
Set the path directly without having to go through it piece by piece.
-
#create(body) ⇒ Object
(also: #post)
Performs a POST to create a new resource Returns a Stretchr::Response object.
-
#get ⇒ Object
(also: #read)
Performs a GET for the current request Returns a Stretchr::Response object.
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
-
#limit(value) ⇒ Object
Sets the maximum number of items you want to get back.
-
#make_request!(options = {}) ⇒ Object
Actually sends the request to the transporter.
-
#method_missing(method, *args) ⇒ Object
Catch everyting not defined and turn it into url parameters If you include an argument, it will be passed into the url as the ID for the collection you specified in the method name.
-
#order(value) ⇒ Object
Set the order of the response.
-
#page(value) ⇒ Object
Convenience method for using pages instead of skip values defaults to 1000 if no limit is known yet.
-
#param(key, value = nil) ⇒ Object
Set params for the url.
-
#path ⇒ Object
Returns the current path that the request is working with.
-
#remove ⇒ Object
(also: #delete)
Performs a DELETE to remove an object deletes an object or entire collection SERIOUSLY - THIS DELETES THINGS.
-
#replace(body) ⇒ Object
(also: #put)
Performs a PUT to replace an object Returns a Stretchr::Response object.
-
#skip(value) ⇒ Object
Tell stretchr to skip some items in the response.
-
#to_uri ⇒ Object
Builds a URI object.
-
#to_url ⇒ Object
Returns the full url, including the path for the current request.
-
#update(body) ⇒ Object
(also: #patch)
Performs a PATCH to update an object will not delete non-included fields just update included ones Returns a Stretchr::Response object.
-
#where(key, value = nil) ⇒ Object
Set filters for the url.
Constructor Details
#initialize(options = {}) ⇒ Request
Returns a new instance of Request.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/stretchr/request.rb', line 8 def initialize( = {}) # Pass in the client so we get all the options required self.client = [:client] # URL BUILDING # This is where we'll initialize everything needed to build up the urls @path_elements = [] @params = Stretchr::Bag.new # a bag to hold the params for th url building @filters = Stretchr::Bag.new({prefix: ":"}) # a bag to hold the filters end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Catch everyting not defined and turn it into url parameters If you include an argument, it will be passed into the url as the ID for the collection you specified in the method name
Examples
r = Stretchr::Request.new r.people(1).cars.path #=> “people/1/cars”
168 169 170 171 172 |
# File 'lib/stretchr/request.rb', line 168 def method_missing(method, *args) @path_elements << method.to_s @path_elements << args[0] if args[0] return self end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/stretchr/request.rb', line 7 def client @client end |
Instance Method Details
#at(path) ⇒ Object
Set the path directly without having to go through it piece by piece
Examples
r= Stretchr::Request.new r.at(“people/1/cars”).path #=> “people/1/cars”
179 180 181 182 |
# File 'lib/stretchr/request.rb', line 179 def at(path) @path_elements += path.split("/") return self end |
#create(body) ⇒ Object Also known as: post
Performs a POST to create a new resource Returns a Stretchr::Response object
Examples
r = Stretchr::Request.new r.people.create(“ryan”) #=> Stretchr::Response object
113 114 115 |
# File 'lib/stretchr/request.rb', line 113 def create(body) self.make_request!({uri: self.to_uri, method: :post, body: body}) end |
#get ⇒ Object Also known as: read
Performs a GET for the current request Returns a Stretchr::Response object
Examples
r = Stretchr::Request.new r.people.get #=> Stretchr::Response object
102 103 104 |
# File 'lib/stretchr/request.rb', line 102 def get self.make_request!({uri: self.to_uri, method: :get}) end |
#limit(value) ⇒ Object
Sets the maximum number of items you want to get back
Examples
r = Stretchr::Request.new r.accounts.limit(10).get #=> Returns 10 accounts
63 64 65 |
# File 'lib/stretchr/request.rb', line 63 def limit(value) param("limit", value) end |
#make_request!(options = {}) ⇒ Object
Actually sends the request to the transporter
155 156 157 158 |
# File 'lib/stretchr/request.rb', line 155 def make_request!( = {}) [:client] = client self.client.transporter.make_request() end |
#order(value) ⇒ Object
Set the order of the response
Examples
r = Stretchr::Request.new r.accounts.order(“-name”).get #=> Orders accounts by name, descending
92 93 94 |
# File 'lib/stretchr/request.rb', line 92 def order(value) param("order", value) end |
#page(value) ⇒ Object
Convenience method for using pages instead of skip values defaults to 1000 if no limit is known yet
Examples
r = Stretchr::Request.new r.accounts.limit(10).page(2).get #=> Returns 10 accounts, starting at 11
82 83 84 85 |
# File 'lib/stretchr/request.rb', line 82 def page(value) l = @params.get("limit") || 1000 skip((l.to_i * value.to_i) - l.to_i) end |
#param(key, value = nil) ⇒ Object
Set params for the url
Examples
r = Stretchr::Request.new r.param(“key”, “value”)
43 44 45 46 |
# File 'lib/stretchr/request.rb', line 43 def param(key, value = nil) @params.set(key, value) return self end |
#path ⇒ Object
Returns the current path that the request is working with
Examples
r = Stretchr::Request.new r.people(1).cars.path #=> “people/1/cars”
24 25 26 |
# File 'lib/stretchr/request.rb', line 24 def path @path_elements.join("/") end |
#remove ⇒ Object Also known as: delete
Performs a DELETE to remove an object deletes an object or entire collection SERIOUSLY - THIS DELETES THINGS
Examples
r = Stretchr::Request.new r.people.remove #=> Stretchr::Response object
149 150 151 |
# File 'lib/stretchr/request.rb', line 149 def remove self.make_request!({uri: self.to_uri, method: :delete}) end |
#replace(body) ⇒ Object Also known as: put
Performs a PUT to replace an object Returns a Stretchr::Response object
Examples
r = Stretchr::Request.new r.people.replace(“ryan”) #=> Stretchr::Response object
124 125 126 |
# File 'lib/stretchr/request.rb', line 124 def replace(body) self.make_request!({uri: self.to_uri, method: :put, body: body}) end |
#skip(value) ⇒ Object
Tell stretchr to skip some items in the response
Examples
r = Stretchr::Request.new r.accounts.limit(10).skip(10).get #=> Returns 10 accounts, starting at 11
72 73 74 |
# File 'lib/stretchr/request.rb', line 72 def skip(value) param("skip", value) end |
#to_uri ⇒ Object
Builds a URI object
34 35 36 |
# File 'lib/stretchr/request.rb', line 34 def to_uri URI::HTTPS.build(host: merge_host, path: merge_path, query: merge_query) end |
#to_url ⇒ Object
Returns the full url, including the path for the current request
29 30 31 |
# File 'lib/stretchr/request.rb', line 29 def to_url to_uri.to_s end |
#update(body) ⇒ Object Also known as: patch
Performs a PATCH to update an object will not delete non-included fields just update included ones Returns a Stretchr::Response object
Examples
r = Stretchr::Request.new r.people.update(“ryan”) #=> Stretchr::Response object
137 138 139 |
# File 'lib/stretchr/request.rb', line 137 def update(body) self.make_request!({uri: self.to_uri, method: :patch, body: body}) end |
#where(key, value = nil) ⇒ Object
Set filters for the url
Examples
r = Stretchr::Request.new r.where(“key”, “value”)
53 54 55 56 |
# File 'lib/stretchr/request.rb', line 53 def where(key, value = nil) @filters.set(key, value) return self end |