Sha256: b10e6f4bec2f5b84e4f9d3a4ae0ec19591ee1051979beb921419646581179983

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module RongCloud
  module API
    module Resourceable
      def self.included(klass)
        klass.extend(ClassMethods)
      end

      module ClassMethods
        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

        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
      end
    end

    include Resourceable

    resources :chatroom
    resources :conversation
    resources :group
    resources :jwt
    resources :message
    resources :push
    resources :sensitiveword
    resources :stat
    resources :statusmessage
    resources :ultragroup
    resources :user
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rongcloud-server-sdk-1.1.0 lib/rongcloud/api.rb