Module: Legion::API::Routes::Marketplace

Defined in:
lib/legion/api/marketplace.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.register_collection(app) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/api/marketplace.rb', line 29

def self.register_collection(app)
  app.get '/api/marketplace' do
    query   = params[:q] || params[:query]
    entries = query ? Legion::Registry.search(query) : Legion::Registry.all
    entries = entries.select { |e| e.status.to_s == params[:status] } if params[:status]
    entries = entries.select { |e| e.risk_tier == params[:tier] }     if params[:tier]

    paginated = entries.slice((page_offset)..(page_offset + page_limit - 1)) || []
    content_type :json
    status 200
    Legion::JSON.dump({
                        data: paginated.map(&:to_h),
                        meta: response_meta.merge(total: entries.size, limit: page_limit, offset: page_offset)
                      })
  end
end

.register_member(app) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/api/marketplace.rb', line 46

def self.register_member(app)
  app.get '/api/marketplace/:name' do
    entry = Legion::Registry.lookup(params[:name])
    unless entry
      halt 404, { 'Content-Type' => 'application/json' },
           Legion::JSON.dump({ error: { code: 'not_found', message: "Extension #{params[:name]} not found" } })
    end

    json_response(entry.to_h.merge(stats: Legion::Registry.usage_stats(params[:name])))
  end
end

.register_review_actions(app) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/legion/api/marketplace.rb', line 58

def self.register_review_actions(app) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  app.post '/api/marketplace/:name/submit' do
    begin
      Legion::Registry.submit_for_review(params[:name])
    rescue ArgumentError => e
      Legion::Logging.warn "API POST /api/marketplace/#{params[:name]}/submit: #{e.message}" if defined?(Legion::Logging)
      halt 404, { 'Content-Type' => 'application/json' },
           Legion::JSON.dump({ error: { code: 'not_found', message: e.message } })
    end
    json_response({ name: params[:name], status: 'pending_review' }, status_code: 202)
  end

  app.post '/api/marketplace/:name/approve' do
    body = parse_request_body
    begin
      Legion::Registry.approve(params[:name], notes: body[:notes])
    rescue ArgumentError => e
      Legion::Logging.warn "API POST /api/marketplace/#{params[:name]}/approve: #{e.message}" if defined?(Legion::Logging)
      halt 404, { 'Content-Type' => 'application/json' },
           Legion::JSON.dump({ error: { code: 'not_found', message: e.message } })
    end
    entry = Legion::Registry.lookup(params[:name])
    json_response({ name: params[:name], status: 'approved', entry: entry.to_h })
  end

  app.post '/api/marketplace/:name/reject' do
    body = parse_request_body
    begin
      Legion::Registry.reject(params[:name], reason: body[:reason])
    rescue ArgumentError => e
      Legion::Logging.warn "API POST /api/marketplace/#{params[:name]}/reject: #{e.message}" if defined?(Legion::Logging)
      halt 404, { 'Content-Type' => 'application/json' },
           Legion::JSON.dump({ error: { code: 'not_found', message: e.message } })
    end
    entry = Legion::Registry.lookup(params[:name])
    json_response({ name: params[:name], status: 'rejected', entry: entry.to_h })
  end

  app.post '/api/marketplace/:name/deprecate' do
    body = parse_request_body
    sunset = begin
      body[:sunset_date] ? Date.parse(body[:sunset_date].to_s) : nil
    rescue ArgumentError => e
      Legion::Logging.debug "Marketplace#deprecate invalid sunset_date '#{body[:sunset_date]}': #{e.message}" if defined?(Legion::Logging)
      nil
    end
    begin
      Legion::Registry.deprecate(params[:name], successor: body[:successor], sunset_date: sunset)
    rescue ArgumentError => e
      Legion::Logging.warn "API POST /api/marketplace/#{params[:name]}/deprecate: #{e.message}" if defined?(Legion::Logging)
      halt 404, { 'Content-Type' => 'application/json' },
           Legion::JSON.dump({ error: { code: 'not_found', message: e.message } })
    end
    entry = Legion::Registry.lookup(params[:name])
    json_response({ name: params[:name], status: 'deprecated', entry: entry.to_h })
  end
end

.register_stats(app) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/legion/api/marketplace.rb', line 116

def self.register_stats(app)
  app.get '/api/marketplace/:name/stats' do
    data = Legion::Registry.usage_stats(params[:name])
    unless data
      halt 404, { 'Content-Type' => 'application/json' },
           Legion::JSON.dump({ error: { code: 'not_found', message: "Extension #{params[:name]} not found" } })
    end

    json_response(data)
  end
end

.registered(app) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/legion/api/marketplace.rb', line 21

def self.registered(app)
  app.helpers Helpers
  register_collection(app)
  register_member(app)
  register_review_actions(app)
  register_stats(app)
end