Class: Pact::Hal::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/hal/link.rb

Constant Summary collapse

DEFAULT_GET_HEADERS =
{
  "Accept" => "application/hal+json"
}.freeze
DEFAULT_POST_HEADERS =
{
  "Accept" => "application/hal+json",
  "Content-Type" => "application/json"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, http_client) ⇒ Link

Returns a new instance of Link.



18
19
20
21
22
23
# File 'lib/pact/hal/link.rb', line 18

def initialize(attrs, http_client)
  @attrs = attrs
  @request_method = attrs.fetch(:method, :get).to_sym
  @href = attrs.fetch('href')
  @http_client = http_client
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



7
8
9
# File 'lib/pact/hal/link.rb', line 7

def href
  @href
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



7
8
9
# File 'lib/pact/hal/link.rb', line 7

def request_method
  @request_method
end

Instance Method Details

#expand(params) ⇒ Object



68
69
70
71
72
# File 'lib/pact/hal/link.rb', line 68

def expand(params)
  expanded_url = expand_url(params, href)
  new_attrs = @attrs.merge('href' => expanded_url)
  Link.new(new_attrs, http_client)
end

#get(payload = {}, headers = {}) ⇒ Object



48
49
50
# File 'lib/pact/hal/link.rb', line 48

def get(payload = {}, headers = {})
  wrap_response(href, @http_client.get(href, payload, DEFAULT_GET_HEADERS.merge(headers)))
end

#get!(*args) ⇒ Object



52
53
54
# File 'lib/pact/hal/link.rb', line 52

def get!(*args)
  get(*args).assert_success!
end

#nameObject



44
45
46
# File 'lib/pact/hal/link.rb', line 44

def name
  @attrs['name']
end

#post(payload = nil, headers = {}) ⇒ Object



60
61
62
# File 'lib/pact/hal/link.rb', line 60

def post(payload = nil, headers = {})
  wrap_response(href, @http_client.post(href, payload ? payload.to_json : nil, DEFAULT_POST_HEADERS.merge(headers)))
end

#post!(payload = nil, headers = {}) ⇒ Object



64
65
66
# File 'lib/pact/hal/link.rb', line 64

def post!(payload = nil, headers = {})
  post(payload, headers).assert_success!
end

#put(payload = nil, headers = {}) ⇒ Object



56
57
58
# File 'lib/pact/hal/link.rb', line 56

def put(payload = nil, headers = {})
  wrap_response(href, @http_client.put(href, payload ? payload.to_json : nil, DEFAULT_POST_HEADERS.merge(headers)))
end

#run(payload = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/pact/hal/link.rb', line 25

def run(payload = nil)
  case request_method
  when :get
    get(payload)
  when :put
    put(payload)
  when :post
    post(payload)
  end
end

#titleObject



40
41
42
# File 'lib/pact/hal/link.rb', line 40

def title
  @attrs['title']
end

#title_or_nameObject



36
37
38
# File 'lib/pact/hal/link.rb', line 36

def title_or_name
  title || name
end

#with_query(query) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pact/hal/link.rb', line 74

def with_query(query)
  if query && query.any?
    uri = URI(href)
    existing_query_params = Rack::Utils.parse_nested_query(uri.query)
    uri.query = Rack::Utils.build_nested_query(existing_query_params.merge(query))
    new_attrs = attrs.merge('href' => uri.to_s)
    Link.new(new_attrs, http_client)
  else
    self
  end
end