Module: Trailblazer::Developer::Client

Defined in:
lib/trailblazer/developer/client.rb

Defined Under Namespace

Classes: Diagram

Class Method Summary collapse

Class Method Details

.Diagram(id, body) ⇒ Object



18
19
20
# File 'lib/trailblazer/developer/client.rb', line 18

def Diagram(id, body)
  Diagram.new(id, body).freeze
end

.duplicate(id:, **options) ⇒ Object



44
45
46
47
48
49
# File 'lib/trailblazer/developer/client.rb', line 44

def duplicate(id:, **options)
  token = retrieve_token(**options)

  response = request(body: nil, token: token, url: "/api/v1/diagrams/#{id}/duplicate", method: :get, **options)
  parse_response(response)
end

.export_diagram(id:, query:, **options) ⇒ Object



37
38
39
40
41
42
# File 'lib/trailblazer/developer/client.rb', line 37

def export_diagram(id:, query:, **options)
  response = request(body: nil, url: "/api/v1/diagrams/#{id}/export#{query}", method: :get, **options)

#      parse_response(response)
  response.body
end

.import(id:, query: "", **options) ⇒ Object



22
23
24
25
# File 'lib/trailblazer/developer/client.rb', line 22

def import(id:, query:"", **options)
  token = retrieve_token(**options)
  export_diagram(id: id, token: token, query: query, **options)
end

.new_diagram(token:, **options) ⇒ Object

DISCUSS: do we need that?



52
53
54
55
56
57
58
# File 'lib/trailblazer/developer/client.rb', line 52

def new_diagram(token:, **options)
  response = request(body: nil, url: "/api/v1/diagrams/new", method: :get, token: token, **options)

  # TODO: use Dry::Struct
  # TODO: handle unauthorized/errors
  parse_response(response)
end

.parse_response(response) ⇒ Object



71
72
73
74
75
76
# File 'lib/trailblazer/developer/client.rb', line 71

def parse_response(response)
  diagram = Diagram.new
  Diagram::Representer.new(diagram).from_json(response.body) # a parsed hash would be cooler?

  diagram
end

.push(operation:, name:) ⇒ Object

TODO: remove me!



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/trailblazer/developer/client.rb', line 79

def self.push(operation:, name:)
  xml = Trailblazer::Diagram::BPMN.to_xml(operation["__activity__"], operation["__sequence__"].map(&:id))
  token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJpZCI6MywidXNlcm5hbWUiOiJhcG90b25pY2siLCJlbWFpbCI6Im5pY2tAdHJhaWxibGF6ZXIudG8ifQ." # rubocop:disable Metrics/LineLength
  conn = Faraday.new(url: "https://api.trb.to")
  response = conn.post do |req|
    req.url "/dev/v1/import"
    req.headers["Content-Type"] = "application/json"
    req.headers["Authorization"] = token
    require "base64"

    req.body = %({ "name": "#{name}", "xml":"#{Base64.strict_encode64(xml)}" })
  end

  puts response.status.inspect
end

.request(host:, url:, method:, token:, body:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/trailblazer/developer/client.rb', line 60

def request(host:, url:, method:, token:, body:, **)
  conn = Faraday.new(url: host)

  conn.send(method) do |req|
    req.url url
    req.headers["Content-Type"] = "application/json"
    req.body = body
    req.headers["Authorization"] = token
  end
end

.retrieve_token(email:, api_key:, url: "/signin", **options) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/trailblazer/developer/client.rb', line 27

def retrieve_token(email:, api_key:, url: "/signin", **options)
  body = JSON.generate({email: email, api_key: api_key})

  response = request(token: nil, method: :get, url: url, body: body, **options)
  return false unless response.status == 200

  # token = CGI::Cookie.parse(response.headers["set-cookie"])["token"][0]
  JSON.parse(response.body)["token"]
end