Class: BookingSync::API::Relation
- Inherits:
-
Object
- Object
- BookingSync::API::Relation
- Defined in:
- lib/bookingsync/api/relation.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#href_template ⇒ Object
readonly
Returns the value of attribute href_template.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.from_link(client, name, options) ⇒ BookingSync::API::Relation
Build a single Relation from the given options.
-
.from_links(client, links) ⇒ Hash
Build a hash of Relations from the ‘links` key in JSON API response.
Instance Method Summary collapse
-
#call(data = {}, options = {}) ⇒ BookingSync::API::Response
Make an API request with the curent Relation.
-
#get(data = {}) ⇒ BookingSync::API::Response
Make an API request with the curent Relation using GET.
-
#href(options = {}) ⇒ String
Expanded URL.
-
#initialize(client, name, href, method = nil) ⇒ Relation
constructor
A Relation represents an available next action for a resource.
-
#post(data = {}) ⇒ BookingSync::API::Response
Make an API request with the curent Relation using POST.
Constructor Details
#initialize(client, name, href, method = nil) ⇒ Relation
A Relation represents an available next action for a resource.
45 46 47 48 49 50 51 |
# File 'lib/bookingsync/api/relation.rb', line 45 def initialize(client, name, href, method = nil) @client = client @name = name.to_sym @href = href @href_template = ::Addressable::Template.new(href.to_s) @method = (method || :get).to_sym end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/bookingsync/api/relation.rb', line 5 def client @client end |
#href_template ⇒ Object (readonly)
Returns the value of attribute href_template.
5 6 7 |
# File 'lib/bookingsync/api/relation.rb', line 5 def href_template @href_template end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/bookingsync/api/relation.rb', line 5 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/bookingsync/api/relation.rb', line 5 def name @name end |
Class Method Details
.from_link(client, name, options) ⇒ BookingSync::API::Relation
Build a single Relation from the given options.
29 30 31 32 33 34 35 36 |
# File 'lib/bookingsync/api/relation.rb', line 29 def self.from_link(client, name, ) case when Hash new client, name, [:href], [:method] when String new client, name, end end |
.from_links(client, links) ⇒ Hash
Build a hash of Relations from the ‘links` key in JSON API response.
13 14 15 16 17 |
# File 'lib/bookingsync/api/relation.rb', line 13 def self.from_links(client, links) links.to_h.each_with_object({}) do |(name, ), relations| relations[name] = from_link(client, name, ) end end |
Instance Method Details
#call(data = {}, options = {}) ⇒ BookingSync::API::Response
Make an API request with the curent Relation.
85 86 87 88 |
# File 'lib/bookingsync/api/relation.rb', line 85 def call(data = {}, = {}) m = .delete(:method) client.call m || method, href_template, data, end |
#get(data = {}) ⇒ BookingSync::API::Response
Make an API request with the curent Relation using GET.
60 61 62 |
# File 'lib/bookingsync/api/relation.rb', line 60 def get(data = {}) call data, method: :get end |
#href(options = {}) ⇒ String
Returns expanded URL.
95 96 97 98 |
# File 'lib/bookingsync/api/relation.rb', line 95 def href( = {}) return @href if @href_template.nil? @href_template.(.to_h).to_s end |
#post(data = {}) ⇒ BookingSync::API::Response
Make an API request with the curent Relation using POST.
71 72 73 |
# File 'lib/bookingsync/api/relation.rb', line 71 def post(data = {}) call data, method: :post end |