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



47
48
49
50
51
52
53
54
# File 'lib/bookingsync/api/relation.rb', line 47

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
  @available_methods = Set.new methods || [@method]
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:



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

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
18
19
# File 'lib/bookingsync/api/relation.rb', line 13

def self.from_links(client, links)
  relations = {}
  links.each do |name, options|
    relations[name] = from_link(client, name, options)
  end if links
  relations
end

Instance Method Details

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

Make an API request with the curent Relation.

Parameters:

  • data (Hash|String) (defaults to: nil)

    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: nil)

    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:



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

def call(data = nil, options = nil)
  m = options && options[:method]
  @client.call m || @method, @href_template, data, options || {}
end

#get(options = nil) ⇒ BookingSync::API::Response

Make an API request with the curent Relation using GET.

Parameters:

  • options (Hash) (defaults to: nil)

    Options to configure the API request.

Options Hash (options):

  • headers: (Hash)

    Hash of API headers to set.

  • query: (Hash)

    Hash of URL query params to set.

  • method: (Symbol)

    Symbol HTTP method.

Returns:



63
64
65
66
67
# File 'lib/bookingsync/api/relation.rb', line 63

def get(options = nil)
  options ||= {}
  options[:method] = :get
  call options
end

#href(options = nil) ⇒ Object



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

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