Class: PactBroker::Api::Resources::Pacticipant

Inherits:
BaseResource
  • Object
show all
Defined in:
lib/pact_broker/api/resources/pacticipant.rb

Instance Method Summary collapse

Instance Method Details

#allowed_methodsObject



20
21
22
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 20

def allowed_methods
  ["GET", "PUT", "PATCH", "DELETE", "OPTIONS"]
end

#content_types_acceptedObject



13
14
15
16
17
18
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 13

def content_types_accepted
  [
    ["application/json", :from_json],
    ["application/merge-patch+json", :from_merge_patch_json]
  ]
end

#content_types_providedObject



9
10
11
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 9

def content_types_provided
  [["application/hal+json", :to_json]]
end

#create_new_pacticipantObject



91
92
93
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 91

def create_new_pacticipant
  pacticipant_service.create parsed_pacticipant(OpenStruct.new).to_h.merge(:name => pacticipant_name)
end

#delete_resourceObject



62
63
64
65
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 62

def delete_resource
  pacticipant_service.delete(pacticipant_name)
  true
end

#from_jsonObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 36

def from_json
  if pacticipant
    @pacticipant = update_existing_pacticipant
  else
    if request.patch? # for backwards compatibility, wish I hadn't done this
      @pacticipant = create_new_pacticipant
      response.headers["Location"] = pacticipant_url(base_url, pacticipant)
    else
      return 404
    end
  end
  response.body = to_json
end

#from_merge_patch_jsonObject



50
51
52
53
54
55
56
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 50

def from_merge_patch_json
  if request.patch?
    from_json
  else
    415
  end
end

#known_methodsObject



24
25
26
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 24

def known_methods
  super + ["PATCH"]
end

#malformed_request?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 28

def malformed_request?
  if request.patch? || request.put?
    invalid_json? || validation_errors_for_schema?
  else
    false
  end
end

#parsed_pacticipant(pacticipant) ⇒ Object



71
72
73
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 71

def parsed_pacticipant(pacticipant)
  decorator_class(:pacticipant_decorator).new(pacticipant).from_json(request_body)
end

#policy_nameObject



75
76
77
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 75

def policy_name
  :'pacticipants::pacticipant'
end

#resource_exists?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 58

def resource_exists?
  !!pacticipant
end

#schemaObject



79
80
81
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 79

def schema
  PactBroker::Api::Contracts::PacticipantSchema
end

#to_jsonObject



67
68
69
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 67

def to_json
  decorator_class(:pacticipant_decorator).new(pacticipant).to_json(decorator_options)
end

#update_existing_pacticipantObject



83
84
85
86
87
88
89
# File 'lib/pact_broker/api/resources/pacticipant.rb', line 83

def update_existing_pacticipant
  if request.really_put?
    @pacticipant = pacticipant_service.replace(pacticipant_name, parsed_pacticipant(OpenStruct.new))
  else
    @pacticipant = pacticipant_service.update(pacticipant_name, parsed_pacticipant(pacticipant))
  end
end