Sha256: a365c185a371d9e80b72baa8829eb8da8dff4f07bd83a4b8ce14537de40d6662

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

module ChatWork
  module Operations
    ACCEPT_PARAMS_ID = %i(file_id task_id message_id)

    attr_accessor :assign_path

    def install_class_operations(*operations)
      define_create if operations.include?(:create)
      define_get if operations.include?(:get)
    end

    def define_get
      instance_eval do
        def get(params = {})
          @assign_path = parse_if_hash_key_exists(path, params, :room_id)
          attach_nested_resource_id(params)
          convert(ChatWork.client.get(@assign_path, params))
        end
      end
    end

    def define_create
      instance_eval do
        def create(params = {})
          @assign_path = parse_if_hash_key_exists(path, params, :room_id)
          attach_nested_resource_id(params)
          convert(ChatWork.client.post(@assign_path, params))
        end
      end
    end

    private
    def parse_if_hash_key_exists(string, hash, key)
      if hash.include?(key)
        string % hash.delete(key)
      else
        string
      end
    end

    def attach_nested_resource_id(params)
      ACCEPT_PARAMS_ID.each do |id_name|
        next unless params.include? id_name
        @assign_path += "/#{params.delete(id_name)}"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
chatwork-0.4.1 lib/chatwork/operations.rb
chatwork-0.4.0 lib/chatwork/operations.rb
chatwork-0.3.1 lib/chatwork/operations.rb
chatwork-0.3.0 lib/chatwork/operations.rb
chatwork-0.2.0 lib/chatwork/operations.rb
chatwork-0.1.2 lib/chatwork/operations.rb
chatwork-0.1.1 lib/chatwork/operations.rb
chatwork-0.1.0 lib/chatwork/operations.rb