Module: RongCloud::API::Resourceable::ClassMethods

Defined in:
lib/rongcloud/api.rb

Instance Method Summary collapse

Instance Method Details

#member(url, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rongcloud/api.rb', line 21

def member(url, options = {})
  meth = if options[:as]
    options[:as]
  else
    resources_name = name.split("::").last.downcase
    resources_name_in_url = "/#{resources_name}/"
    raise ArgumentError unless url.start_with?(resources_name_in_url)
    url.delete_prefix(resources_name_in_url)
      .delete_suffix(".json")
      .tr("/", "_")
      .downcase
  end

  format = options[:format] || :form
  define_method(meth) do |payload, opts = {}|
    client.post(url, opts.merge(format => payload))
  end
end

#resources(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rongcloud/api.rb', line 9

def resources(name)
  require_relative "api/#{name}"

  class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}
      @#{name} ||= Struct.new(:client) do
        include ::RongCloud::API::#{name.to_s.capitalize}
      end.new(client)
    end
  CODE
end