WWW::GoDaddy::REST::Resource - Represent a REST resource
$client = WWW::GoDaddy::REST->new(...); $resource = WWW::GoDaddy::REST::Resource->new({ client => $client, fields => { 'type' => 'automobile', 'id' => '1001', 'make' => 'Tesla', 'model' => 'S' 'links' => { 'self' => 'https://example.com/v1/automobiles/1001', 'schemas' => 'https://example.com/v1/schemas' }, 'actions' => { 'charge' => 'https://example.com/v1/automobiles/1001?charge' } # ... # see: https://github.com/godaddy/gdapi/blob/master/specification.md }, }); $resource->f('id'); # get 1001 $resource->f('id','2000'); # set to 2000 and return 2000 # follow a link in links section $schemas_resource = $resource->follow_link('schemas'); # perform an action in the actions section $result_resource = $resource->do_action('charge',{ 'with' => 'quick_charger' });
Base class used to represent a REST resource.
Given a hash reference of "ATTRIBUTES" and values, return a new instance of this object.
It is likely more important that you use the new_subclassed class method.
new_subclassed
Example:
my $resource = WWW::GoDaddy::REST::Resource->new({ client => WWW::GoDaddy::REST->new(...), fields => { id => '...', ... }, });
This takes the same paramegers as new and is the preferred construction method. This tries to find the appropriate subclass of WWW::GoDaddy::REST::Resource and passes along the paramegers to the new method of that subclass instead.
new
WWW::GoDaddy::REST::Resource
See also: new
Given a list of schema type names, find the best implementation sub class.
Returns the string of the class name. If no good subclass candidate is found, returns undef.
find_implementation( 'schema' ); # WWW::GoDaddy::REST::Schema
Register a subclass handler for a schema type given a schema name and the name of a WWW::GoDaddy::REST::Resource subclass.
This can take as many schema => resource class pairs as you want.
WWW::GoDaddy::REST::Resource->register_subclass( 'account' => 'My::AccountRes' ); WWW::GoDaddy::REST::Resource->register_subclass( 'foo' => 'Bar', 'baz' => 'Buzz' );
Instance of WWW::GoDaddy::REST associated with the resource.
Hash reference containing the raw data for the underlying resource.
Several methods delegate to this underlying structure such as f, and field.
f
field
Optionally present instance of an HTTP::Response object so that you can inspect the HTTP information related to the resource.
Get or set a field by name. You may also use the longer name field.
When performing a set, it also returns the new value that was set.
$res->f('field_name'); # get $res->f('field_name','new'); # set
Get a field by name. If it is a resource, this will turn it into an object instead of giving you the raw hash reference as the return value.
Note, if the field is a 'map' or 'array' of resources, every item in those lists will be 'resourcified'.
If this is not a resource, then it does return the raw value.
# return value is a WWW::GoDaddy::REST::Resource, not a hash ref $driver = $car->f('driver');
See f if you want the raw value. This will return the raw value, if the value does not look like a resource.
Get or set a field by name. You may also use the shorter name f.
$res->field('field_name'); # get $res->fieldf('field_name','new'); # set
Does a PUT at this resources URI. Returns a new resource object.
$r2 = $r1->save();
Does a DELETE on this resource. Returns a new resource object. This return value likely is only useful to get at the http_response attribute.
http_response
Does a POST with the supplied data on the action URL with the given name.
If the action with the provided name does not exist, this method will die. See also: action and actions
action
actions
$r2 = $r1->do_action('some_action',{ a => 'a_v' });
Gets the resource by following the link URL with the provided name.
If the link with the provided name does not exist, this method will die. See also: link and link
link
$r2 = $r1->follow_link('some_link');
Return the id of this instance
Return the name of the schema type that this object belongs to.
Return the full URI to the schema type that this object belongs to.
Returns the URL for the schema collection. This differs from the client schemas_url method since it has more places to look for hints of the schemas collection url (headers, json response etc).
schemas_url
$r->schemas_url();
Find and return the WWW::GoDaddy::REST::Schema object that this is.
Return the link URL for the given name or undef if it does not exist.
# https://example.com/v1/thing/... $r->link('self'); # 'https://example.com/v1/me/1'
Return the hashref that contains the link => url information
$r->links(); # { # 'self' => 'https://example.com/v1/me/1', # 'some_link' => 'https://example.com/v1/me/1/some_link' # }
Return the action URL for the given name.
$r->action('custom_action'); # https://example.com/v1/thing/1001?some_action
Return the hashref that contains the action => url information
$r->actions(); # { # 'custom_action' => 'https://example.com/v1/thing/1001?some_action' # }
Returns a list of resources that this resource contains. This implementation simply returns a list of 'self'. It is here to be consistent with the implementation found in WWW::GoDaddy::REST::Collection.
@items = $resource->items();
Returns a hashref that represents this object. This exists to make using the JSON module more convenient. This does NOT return a JSON STRING, just a perl data structure.
See to_string.
to_string
Returns a JSON string that represents this object. This takes an optional parameter, "pretty". If true, the json output will be prettified. This defaults to false.
The resource is returned as a perl data structure. Note, if there are no fields, then the http_respons is consulted, if json data is found in the content, that is returned (for instance, a plane old string or integer).
fields
David Bartle, <davidb@mediatemple.net>
<davidb@mediatemple.net>
Copyright (c) 2014 Go Daddy Operating Company, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
To install WWW::GoDaddy::REST, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WWW::GoDaddy::REST
CPAN shell
perl -MCPAN -e shell install WWW::GoDaddy::REST
For more information on module installation, please visit the detailed CPAN module installation guide.