Class: BookingSync::API::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/bookingsync/api/relation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name, href, method = nil) ⇒ Relation

A Relation represents an available next action for a resource.

Parameters:

  • client (BookingSync::API::Client)

    The client that made the HTTP request.

  • name (Symbol)

    The name of the relation.

  • href (String)

    The String URL of the location of the next action.

  • method (Symbol) (defaults to: nil)

    The Symbol HTTP method. Default: :get



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

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/bookingsync/api/relation.rb', line 5

def client
  @client
end

#href_templateObject (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

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/bookingsync/api/relation.rb', line 5

def method
  @method
end

#nameObject (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

Build a single Relation from the given options.

Parameters:

  • client (BookingSync::API::Client)

    The client that made the HTTP request

  • name (Symbol)

    Name of the Relation.

  • options (Hash)

    A Hash containing the other Relation properties.

Options Hash (options):

  • href: (String)

    The String URL of the next action’s location.

  • method: (String)

    The optional String HTTP method.

Returns:



29
30
31
32
33
34
35
36
# File 'lib/bookingsync/api/relation.rb', line 29

def self.from_link(client, name, options)
  case options
  when Hash
    new client, name, options[:href], options[:method]
  when String
    new client, name, options
  end
end

Build a hash of Relations from the ‘links` key in JSON API response.

Parameters:

  • client (BookingSync::API::Client)

    The client that made the HTTP request.

  • links (Hash)

    Hash of relation_name => relation options.

Returns:

  • (Hash)

    Hash of relation_name => relation elements.



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, options), relations|
    relations[name] = from_link(client, name, options)
  end
end

Instance Method Details

#call(data = {}, options = {}) ⇒ BookingSync::API::Response

Make an API request with the curent Relation.

Parameters:

  • data (Hash|String) (defaults to: {})

    The Optional Hash or Resource body to be sent. :get or :head requests can have no body, so this can be the options Hash instead.

  • options (Hash) (defaults to: {})

    A Hash of option to configure the API request.

Options Hash (options):

  • headers: (Hash)

    A Hash of API headers to set.

  • query: (Hash)

    Hash of URL query params to set.

  • method: (Symbol)

    Symbol HTTP method.

Returns:



85
86
87
88
# File 'lib/bookingsync/api/relation.rb', line 85

def call(data = {}, options = {})
  m = options.delete(:method)
  client.call m || method, href_template, data, options
end

#get(data = {}) ⇒ BookingSync::API::Response

Make an API request with the curent Relation using GET.

Parameters:

  • options (Hash)

    Options to configure the API request.

Returns:



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.

Parameters:

  • options (Hash) (defaults to: {})

    Params to be included in expanded URL

Returns:

  • (String)

    expanded URL



95
96
97
98
# File 'lib/bookingsync/api/relation.rb', line 95

def href(options = {})
  return @href if @href_template.nil?
  @href_template.expand(options.to_h).to_s
end

#post(data = {}) ⇒ BookingSync::API::Response

Make an API request with the curent Relation using POST.

Parameters:

  • options (Hash)

    Options to configure the API request.

Returns:



71
72
73
# File 'lib/bookingsync/api/relation.rb', line 71

def post(data = {})
  call data, method: :post
end