class Faraday::Hypermedia::History
Constants
- State
Public Class Methods
new()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 9 def initialize @stored_states = [build_state] @current_index = 0 reset_current_links end
Public Instance Methods
back()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 87 def back if @current_index >= 1 @current_index -= 1 reset_current_links end end
current_links()
click to toggle source
いったんここに実装
# File lib/faraday/hypermedia/history.rb, line 20 def current_links @current_links ||= build_current_links end
current_state()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 15 def current_state @stored_states[@current_index] end
fill_in_template_params(template_params)
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 51 def fill_in_template_params(template_params) template_urls = current_links.keys.select { |k| k =~ RE_URI_TEMPLATE } template_urls.each do |template_url| expanded_url = URITemplate.new(template_url).expand(template_params) # Addressable のほうが良いかも link_params = current_links.delete(template_url) current_links.store(expanded_url, link_params) end end
forward()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 94 def forward if @current_index < @stored_states.length - 1 @current_index += 1 reset_current_links end end
pp_current_links()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 24 def pp_current_links # TODO: きれいにする # current_links.to_a.join("#{current_links.class::DELIMETER}\n") rels = current_links.map{|url, params| params['rel'].to_a.map{|rel| [rel, [url, params]] }}.flatten(1).group_by{|rel, _| rel} rels.each do |rel, links| puts("rel=#{rel}") if links.length >= 2 links.each.with_index(1) do |(_, link), i| url, params = link params = params.dup params.delete('rel') print(" (#{i}) <#{url}>") print(" (#{params})") unless params.empty? puts end else url, params = links.first.last params = params.dup params.delete('rel') print(" <#{url}>") print(" (#{params})") unless params.empty? puts end end nil end
push(response_env)
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 82 def push(response_env) url = response_env[:url] push_state(response_env, '', url) end
push_state(data, title, url)
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 64 def push_state(data, title, url) if url == current_state.url replace_state(data, title, url) else @stored_states.slice!((@current_index+1)..-1) # current_index の先から最後まで削除 state = build_state(data: data, title: title, url: url) @stored_states.push(state) @current_index += 1 reset_current_links state end end
replace_state(data, title, url)
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 77 def replace_state(data, title, url) reset_current_links @stored_states[@current_index] = build_state(data: data, title: title, url: url) end
reset_current_links()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 60 def reset_current_links @current_links = nil end
Private Instance Methods
build_current_links()
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 111 def build_current_links current_response_headers = current_state.data[:response_headers] return HttpLinkHeader.new unless current_response_headers links = current_response_headers['link'].to_s.scan(/<[^>]*>[^,]*/) link_templates = current_response_headers['link-template'].to_s.scan(/<[^>]*>[^,]*/) # 自前でsplitする HttpLinkHeader.new(links + link_templates) end
build_state(source = {})
click to toggle source
# File lib/faraday/hypermedia/history.rb, line 103 def build_state(source = {}) State.new( source[:data] || {}, source[:title] || '', source[:url] || URI('navigation:blank') ) end