Module: Appilf::Util

Defined in:
lib/appilf/util.rb

Class Method Summary collapse

Class Method Details

.array_from_response_item(api_elements_hash) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/appilf/util.rb', line 21

def self.array_from_response_item(api_elements_hash)
  items = []
  api_elements_hash['data'].each do |api_element_hash|
    items << translate_from_response({'data' => api_element_hash})
  end
  items
end

.flippa_class_mapping(flippa_type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/appilf/util.rb', line 35

def self.flippa_class_mapping(flippa_type)
  @flippa_class_mappings ||= {
      'listings' => Appilf::Listing,
      'users' => Appilf::User,
      'domain-traits' => Appilf::DomainTrait,
      'watched-items' => Appilf::WatchedItem,
      'saved-searches' => Appilf::SavedSearch
  }
  @flippa_class_mappings[flippa_type] || AppilfObject
end

.object_from_response_item(response_item) ⇒ Object



29
30
31
32
33
# File 'lib/appilf/util.rb', line 29

def self.object_from_response_item(response_item)
  flippa_type = response_item['data']
  flippa_type = flippa_type.fetch('type', nil) if flippa_type
  flippa_class_mapping(flippa_type).send(:new, response_item)
end

.paginated_resource?(response_data) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/appilf/util.rb', line 55

def self.paginated_resource?(response_data)
  page_links = response_data.fetch('links', nil)
  return false unless page_links
  page_links.keys.include?('prev') && page_links.keys.include?('next')
end

.parse_query_params(params = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/appilf/util.rb', line 46

def self.parse_query_params(params = {})
  @pagination_params_map.each_pair do |old_key,new_key|
    original_value = params.delete(old_key)
    next unless original_value
    params[new_key] = original_value
  end
  params
end

.translate_from_response(response_data) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/appilf/util.rb', line 11

def self.translate_from_response(response_data)
  if response_data['data'].is_a?(Array) && paginated_resource?(response_data)
    Appilf::ResourcePage.new(response_data)
  elsif response_data['data'].is_a?(Array)
    array_from_response_item(response_data)
  else
    object_from_response_item(response_data)
  end
end