Class: Restfolia::EntryPoint
- Inherits:
-
Object
- Object
- Restfolia::EntryPoint
- Includes:
- HTTP::Configuration
- Defined in:
- lib/restfolia/entry_point.rb
Overview
Public: Responsible for request and validate a Resource. Abstract details of how to deal with HTTP, like headers, cookies, auth etc. The correct form to create an EntryPoint, is using Restfolia.at or links from some instance of Restfolia::Resource. See the examples for more details.
Examples
ep = Restfolia.at("http://fakeurl.com/some/service")
# => #<EntryPoint ...>
resource = Restfolia.at("http://fakeurl.com/service/id/1").get
resource.links("contacts")
# => #<EntryPoint ...> to "contacts" from this resource
Instance Attribute Summary collapse
-
#rel ⇒ Object
readonly
Public: Returns String that represents the relation of EntryPoint.
-
#url ⇒ Object
readonly
Public: Returns the String url of EntryPoint.
Attributes included from HTTP::Configuration
Instance Method Summary collapse
-
#delete ⇒ Object
Public: Send Delete verb to EntryPoint’s url.
-
#get(params = nil) ⇒ Object
Public: Get the Resource from this EntryPoint’s url.
-
#initialize(url, rel = nil) ⇒ EntryPoint
constructor
Public: Creates an EntryPoint.
-
#inspect ⇒ Object
Returns url and rel for inspecting.
-
#post(params) ⇒ Object
Public: Post data to EntryPoint’s url.
-
#put(params) ⇒ Object
Public: Put data to EntryPoint’s url.
Methods included from HTTP::Configuration
#as, #headers, #set_cookies, #with_headers
Constructor Details
#initialize(url, rel = nil) ⇒ EntryPoint
Public: Creates an EntryPoint.
url - A String address of some API service. rel - An optional String that represents the relation of EntryPoint.
33 34 35 36 |
# File 'lib/restfolia/entry_point.rb', line 33 def initialize(url, rel = nil) @url = url @rel = rel end |
Instance Attribute Details
#rel ⇒ Object (readonly)
Public: Returns String that represents the relation of EntryPoint.
27 28 29 |
# File 'lib/restfolia/entry_point.rb', line 27 def rel @rel end |
#url ⇒ Object (readonly)
Public: Returns the String url of EntryPoint.
24 25 26 |
# File 'lib/restfolia/entry_point.rb', line 24 def url @url end |
Instance Method Details
#delete ⇒ Object
Public: Send Delete verb to EntryPoint’s url.
Examples
# Expecting API respond with 204 and empty body
resource = Restfolia.at("http://service.com/resource/1").delete
Returns depends on http code from response. For each “range” (ex. 2xx, 3xx … etc) you can have a different return value. For 2xx range, you can expect an instance of Restfolia::Resource. You can see Restfolia::HttpBehaviour for more details.
Raises Restfolia::ResponseError for unexpected conditions. See Restfolia::HTTP::Behaviour methods for more details. Raises URI::InvalidURIError if url attribute is invalid.
143 144 145 146 |
# File 'lib/restfolia/entry_point.rb', line 143 def delete http_resp = Restfolia::HTTP::Request.do_request(:delete, self.url, self.configuration) Restfolia::HTTP.response_by_status_code(http_resp) end |
#get(params = nil) ⇒ Object
Public: Get the Resource from this EntryPoint’s url.
params - an optional query String or Hash object. String parameter is passed direct as query. Hash object, before mounting query, URI.encode is used on values.
Examples
# GET on http://service.com/search
resource = Restfolia.at("http://service.com/search").get
# GET on http://service.com/search?q=test
resource = Restfolia.at("http://service.com/search").get(:q => "test")
# or if you want to control your query, you can send a String
resource = Restfolia.at("http://service.com/search").get("q=test")
Returns depends on http code from response. For each “range” (ex. 2xx, 3xx … etc) you can have a different return value. For 2xx range, you can expect an instance of Restfolia::Resource. You can see Restfolia::HttpBehaviour for more details.
Raises Restfolia::ResponseError for unexpected conditions. See Restfolia::HTTP::Behaviour methods for more details. Raises URI::InvalidURIError if url attribute is invalid.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/restfolia/entry_point.rb', line 62 def get(params = nil) query = if params && params.is_a?(String) params elsif params && params.is_a?(Hash) params.map { |k, v| "#{k}=#{URI.encode(v)}" }.join end args = self.configuration.merge(:query => query) http_resp = Restfolia::HTTP::Request.do_request(:get, self.url, args) Restfolia::HTTP.response_by_status_code(http_resp) end |
#inspect ⇒ Object
Returns url and rel for inspecting.
149 150 151 |
# File 'lib/restfolia/entry_point.rb', line 149 def inspect "#<#{self.class} @url=\"#{@url}\" @rel=\"#{@rel}\">" end |
#post(params) ⇒ Object
Public: Post data to EntryPoint’s url.
params - Hash object with data to be encoded as JSON and send as request body.
Examples
# Expecting API respond with 201 and Location header
data = {:name => "Test"}
resource = Restfolia.at("http://service.com/resource/1").post(data)
Returns depends on http code from response. For each “range” (ex. 2xx, 3xx … etc) you can have a different return value. For 2xx range, you can expect an instance of Restfolia::Resource. You can see Restfolia::HttpBehaviour for more details.
Raises Restfolia::ResponseError for unexpected conditions. See Restfolia::HTTP::Behaviour methods for more details. Raises URI::InvalidURIError if url attribute is invalid.
93 94 95 96 97 98 99 |
# File 'lib/restfolia/entry_point.rb', line 93 def post(params) body = MultiJson.dump(params) args = self.configuration.merge(:body => body) http_resp = Restfolia::HTTP::Request.do_request(:post, self.url, args) Restfolia::HTTP.response_by_status_code(http_resp) end |
#put(params) ⇒ Object
Public: Put data to EntryPoint’s url.
params - Hash object with data to be encoded as JSON and send as request body.
Examples
# Expecting API respond with 201 and Location header
data = {:name => "Test"}
resource = Restfolia.at("http://service.com/resource/1").put(data)
Returns depends on http code from response. For each “range” (ex. 2xx, 3xx … etc) you can have a different return value. For 2xx range, you can expect an instance of Restfolia::Resource. You can see Restfolia::HttpBehaviour for more details.
Raises Restfolia::ResponseError for unexpected conditions. See Restfolia::HTTP::Behaviour methods for more details. Raises URI::InvalidURIError if url attribute is invalid.
120 121 122 123 124 125 126 |
# File 'lib/restfolia/entry_point.rb', line 120 def put(params) body = MultiJson.dump(params) args = self.configuration.merge(:body => body) http_resp = Restfolia::HTTP::Request.do_request(:put, self.url, args) Restfolia::HTTP.response_by_status_code(http_resp) end |