Class: Contentful::Request
- Inherits:
-
Object
- Object
- Contentful::Request
- Defined in:
- lib/contentful/request.rb
Overview
This object represents a request that is to be made. It gets initialized by the client with domain specific logic. The client later uses the Request’s #url and #query methods to execute the HTTP request.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#absolute? ⇒ Boolean
Returns true if endpoint is an absolute url.
-
#copy ⇒ Object
Returns a new Request object with the same data.
-
#get ⇒ Object
Delegates the actual HTTP work to the client.
-
#initialize(client, endpoint, query = {}, id = nil) ⇒ Request
constructor
A new instance of Request.
-
#url ⇒ Object
Returns the final URL, relative to a contentful space.
Constructor Details
#initialize(client, endpoint, query = {}, id = nil) ⇒ Request
Returns a new instance of Request.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/contentful/request.rb', line 8 def initialize(client, endpoint, query = {}, id = nil) @client = client @endpoint = endpoint @query = (normalize_query(query) if query && !query.empty?) if id @type = :single # Given the deprecation of `URI::escape` and `URI::encode` # it is needed to replace it with `URI::encode_www_form_component`. # This method, does replace spaces with `+` instead of `%20`. # Therefore, to keep backwards compatibility, we're replacing the resulting `+` # back with `%20`. @id = URI.encode_www_form_component(id).gsub('+', '%20') else @type = :multi @id = nil end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/contentful/request.rb', line 6 def client @client end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
6 7 8 |
# File 'lib/contentful/request.rb', line 6 def endpoint @endpoint end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/contentful/request.rb', line 6 def id @id end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
6 7 8 |
# File 'lib/contentful/request.rb', line 6 def query @query end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/contentful/request.rb', line 6 def type @type end |
Instance Method Details
#absolute? ⇒ Boolean
Returns true if endpoint is an absolute url
39 40 41 |
# File 'lib/contentful/request.rb', line 39 def absolute? @endpoint.start_with?('http') end |
#copy ⇒ Object
Returns a new Request object with the same data
44 45 46 |
# File 'lib/contentful/request.rb', line 44 def copy Marshal.load(Marshal.dump(self)) end |
#get ⇒ Object
Delegates the actual HTTP work to the client
34 35 36 |
# File 'lib/contentful/request.rb', line 34 def get client.get(self) end |
#url ⇒ Object
Returns the final URL, relative to a contentful space
29 30 31 |
# File 'lib/contentful/request.rb', line 29 def url "#{@endpoint}#{@type == :single ? "/#{id}" : ''}" end |