Sha256: e89b7a2b1ac1981e727fe6461718769259443433dbcf112d270bee478682bccf

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# Copyright (C) 2017  Stratumn SAS
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

module AgentClient
  ##
  # Represents a link in a Stratumn application
  class Segment
    include Request
    include Helper

    attr_accessor :agent, :meta, :state, :link, :link_hash

    def initialize(agent, obj)
      self.agent = agent

      self.link = obj['link']
      self.meta = link['meta']
      self.state = link['state']
      self.link_hash = obj['meta']['linkHash']

      agent.agent_info['actions'].each do |(method, _)|
        add_transition_method(method)
      end
    end

    def previous
      agent.get_segment(meta['prevLinkHash']) if meta['prevLinkHash']
    end

    def find_segments(options = {})
      agent.find_segments(options)
    end

    def load
      agent.get_segment(link_hash)
    end

    def self.from(segment)
      agent = Agent.load(segment['meta']['agentUrl'])

      new(agent, segment)
    end

    private

    def add_transition_method(method)
      define_singleton_method(method) do |*args|
        url = "#{agent.url}/segments/#{link_hash}/#{method}"

        result = post(url, json: args)

        self.class.new(agent, result)
      end

      singleton_class.send(:alias_method, underscore(method), method)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stratumn_agent_client-1.0.0 lib/agent_client/segment.rb