Class: ApipieBindings::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie_bindings/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, api) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (NameError)

7
8
9
10
11
# File 'lib/apipie_bindings/resource.rb', line 7

def initialize(name, api)
  raise NameError.new("Resource '#{name}' does not exist in the API") unless api.apidoc[:docs][:resources].key?(name)
  @name = name
  @api = api
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'lib/apipie_bindings/resource.rb', line 5

def name
  @name
end

Instance Method Details

#action(name) ⇒ ApipieBindings::Action

Get ApipieBindings::Action

Parameters:

  • name (Symbol)

    action name

Returns:


50
51
52
# File 'lib/apipie_bindings/resource.rb', line 50

def action(name)
  ApipieBindings::Action.new(@name, name, @api)
end

#actionsArray<ApipieBindings::Action>

Get list of all actions for a resource

Returns:


34
35
36
# File 'lib/apipie_bindings/resource.rb', line 34

def actions
  apidoc[:methods].map { |a| action(a[:name].to_sym) }
end

#apidocHash

Get API documentation for a resource

Returns:

  • (Hash)

27
28
29
# File 'lib/apipie_bindings/resource.rb', line 27

def apidoc
  @api.apidoc[:docs][:resources][@name]
end

#call(action, params = {}, headers = {}, options = {}) ⇒ Hash

Execute an action on a resource

Parameters:

  • action (Symbol)
  • params (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)

20
21
22
# File 'lib/apipie_bindings/resource.rb', line 20

def call(action, params={}, headers={}, options={})
  @api.call(@name, action, params, headers, options)
end

#has_action?(name) ⇒ Bool

Determine if resource has a particular action

Parameters:

  • name (Symbol)

    name of action to check

Returns:

  • (Bool)

42
43
44
# File 'lib/apipie_bindings/resource.rb', line 42

def has_action?(name)
  apidoc[:methods].any? { |action| action[:name].to_sym == name }
end

#inspectString

Note:

same as to_s method

Get string representation

Returns:

  • (String)

    ApipieBindings::Resource as a string


72
73
74
# File 'lib/apipie_bindings/resource.rb', line 72

def inspect
  to_s
end

#singular_nameString

Get simiple string representation

Returns:

  • (String)

    ApipieBindings::Resource as a string


57
58
59
# File 'lib/apipie_bindings/resource.rb', line 57

def singular_name
  ApipieBindings::Inflector.singularize(@name.to_s)
end

#to_sString

Get string representation

Returns:

  • (String)

    ApipieBindings::Resource as a string


64
65
66
# File 'lib/apipie_bindings/resource.rb', line 64

def to_s
  "<Resource :#{@name}>"
end