Class: APIHub::Resource

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

Direct Known Subclasses

Base

Class Method Summary collapse

Methods inherited from Mash

#custom_reader, #custom_writer, #deep_merge, #deep_update, #delete, #dup, #fetch, #id, #initialize, #initializing_reader, #key?, #method_missing, new, #regular_dup, #replace, #respond_to?, #shallow_merge, #shallow_update, #type, #underbang_reader

Constructor Details

This class inherits a constructor from APIHub::Mash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class APIHub::Mash

Class Method Details

.delete(action = '', params = {}, options = {}) ⇒ Object



54
55
56
# File 'lib/apihub/resource.rb', line 54

def self.delete(action = '', params = {}, options = {})
  request(uri(action), options.merge(method: :delete, params: params))
end

.endpoint(value = nil) ⇒ Object Also known as: endpoint=



5
6
7
8
9
# File 'lib/apihub/resource.rb', line 5

def self.endpoint(value = nil)
  @endpoint = value if value
  return @endpoint if @endpoint
  superclass.respond_to?(:endpoint) ? superclass.endpoint : nil
end

.get(action = '', params = {}, options = {}) ⇒ Object



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

def self.get(action = '', params = {}, options = {})
  request(uri(action), options.merge(method: :get, params: params))
end

.options(value = nil) ⇒ Object Also known as: options=



17
18
19
20
21
# File 'lib/apihub/resource.rb', line 17

def self.options(value = nil)
  @options = value if value
  return @options if @options
  superclass.respond_to?(:options) ? superclass.options : {}
end

.path(value = nil) ⇒ Object Also known as: path=



11
12
13
14
15
# File 'lib/apihub/resource.rb', line 11

def self.path(value = nil)
  @path = value if value
  return @path if @path
  superclass.respond_to?(:path) ? superclass.path : nil
end

.post(action = '', params = {}, options = {}) ⇒ Object



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

def self.post(action = '', params = {}, options = {})
  request(uri(action), options.merge(method: :post, params: params))
end

.put(action = '', params = {}, options = {}) ⇒ Object



46
47
48
# File 'lib/apihub/resource.rb', line 46

def self.put(action = '', params = {}, options = {})
  request(uri(action), options.merge(method: :put, params: params))
end

.request(url, options = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/apihub/resource.rb', line 58

def self.request(url, options = {})
  options = Nestful::Helpers.deep_merge(self.options, options)

  self.new Nestful::Request.new(
    url, options
  ).execute
end

.uri(*parts) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/apihub/resource.rb', line 33

def self.uri(*parts)
  # If an absolute URI already
  if (uri = parts.first) && uri.is_a?(URI)
    return uri if uri.host
  end

  URI.parse(Nestful::Helpers.to_path(url, *parts))
end

.urlObject



29
30
31
# File 'lib/apihub/resource.rb', line 29

def self.url
  URI.join(endpoint.to_s, path.to_s).to_s
end