class Pact::Hal::Link
Constants
- DEFAULT_GET_HEADERS
- DEFAULT_POST_HEADERS
Attributes
attrs[R]
href[R]
http_client[R]
request_method[R]
Public Class Methods
new(attrs, http_client)
click to toggle source
# 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
Public Instance Methods
expand(params)
click to toggle source
# 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 = {})
click to toggle source
# 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)
click to toggle source
# File lib/pact/hal/link.rb, line 52 def get!(*args) get(*args).assert_success! end
name()
click to toggle source
# File lib/pact/hal/link.rb, line 44 def name @attrs['name'] end
post(payload = nil, headers = {})
click to toggle source
# 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 = {})
click to toggle source
# File lib/pact/hal/link.rb, line 64 def post!(payload = nil, headers = {}) post(payload, headers).assert_success! end
put(payload = nil, headers = {})
click to toggle source
# 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)
click to toggle source
# 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
title()
click to toggle source
# File lib/pact/hal/link.rb, line 40 def title @attrs['title'] end
title_or_name()
click to toggle source
# File lib/pact/hal/link.rb, line 36 def title_or_name title || name end
with_query(query)
click to toggle source
# 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
Private Instance Methods
expand_url(params, url)
click to toggle source
# File lib/pact/hal/link.rb, line 105 def expand_url(params, url) params.inject(url) do | url, (key, value) | url.gsub('{' + key.to_s + '}', ERB::Util.url_encode(value)) end end
wrap_response(href, http_response)
click to toggle source
# File lib/pact/hal/link.rb, line 90 def wrap_response(href, http_response) require 'pact/hal/entity' # avoid circular reference require 'pact/hal/non_json_entity' if http_response.success? if http_response.json? Entity.new(href, http_response.body, @http_client, http_response) else NonJsonEntity.new(href, http_response.raw_body, @http_client, http_response) end else ErrorEntity.new(href, http_response.raw_body, @http_client, http_response) end end