class UniversalGitClient::Normalizers::Base
Attributes
resource[R]
response[R]
Public Class Methods
new(response, resource)
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 14 def initialize(response, resource) @response = response @resource = resource end
Public Instance Methods
body_as_object()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 31 def body_as_object @body_as_object ||= begin struct = JSON.parse(response.body, object_class: OpenStruct) if !struct.is_a?(Array) && struct.values # Bitbucket embeds collections inside values struct.values else struct end end end
current_page_index()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 58 def current_page_index response.request.options[:query][:page].to_s rescue StandardError nil end
first_page_index()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 64 def first_page_index fetch_rel_value('first') end
last_page_index()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 76 def last_page_index fetch_rel_value('last') end
next_page_index()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 72 def next_page_index fetch_rel_value('next') end
normalize()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 19 def normalize object = body_as_object serializer = serializer_for_resource serializer_options = {} serializer_options.deep_merge!(pagination_options) if object.is_a?(Array) serializer.new(object, serializer_options) end
pagination_options()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 43 def pagination_options { meta: { pagination: { self: current_page_index, first: first_page_index, prev: prev_page_index, next: next_page_index, last: last_page_index, per_page: per_page_index } } } end
per_page_index()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 80 def per_page_index response.request.options[:query][:per_page].to_s rescue StandardError nil end
prev_page_index()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 68 def prev_page_index fetch_rel_value('prev') end
serializer_for_resource()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 27 def serializer_for_resource self.class.const_get("#{resource}Serializer") end
Private Instance Methods
fetch_rel_value(rel)
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 88 def fetch_rel_value(rel) Hash[URI.decode_www_form(links.by_rel(rel).target.query)]['page'] rescue StandardError nil end
links()
click to toggle source
# File lib/universal-git-client/normalizers/base.rb, line 94 def links @links ||= Nitlink::Parser.new.parse(response) end