Class: Cenit::API::DataType

Inherits:
Object
  • Object
show all
Includes:
HttpCreatable, HttpReadable, UrlSlug, Enumerable, Origin::Queryable
Defined in:
lib/cenit/api/data_type.rb

Instance Attribute Summary

Attributes included from UrlSlug

#parent, #slug

Instance Method Summary collapse

Methods included from HttpCreatable

#create, #post

Methods included from HttpDataRequester

#do_data_request, #do_request

Methods included from HttpQuery

#method_missing, #query

Methods included from HttpReadable

#get

Methods included from UrlSlug

#url

Constructor Details

#initialize(slug, parent = nil) ⇒ DataType

Returns a new instance of DataType.



12
13
14
15
16
# File 'lib/cenit/api/data_type.rb', line 12

def initialize(slug, parent = nil)
  @slug = slug
  @parent = parent
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cenit::API::HttpQuery

Instance Method Details

#countObject



23
24
25
26
27
28
29
# File 'lib/cenit/api/data_type.rb', line 23

def count
  if (response = get).code == 200
    response['count']
  else
    nil
  end
end

#default_headersObject



18
19
20
21
# File 'lib/cenit/api/data_type.rb', line 18

def default_headers
  Hash[super].deep_merge('X-Query-Selector' => process_hash(selector).to_json,
                         'X-Query-Options' => options.to_json)
end

#eachObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cenit/api/data_type.rb', line 31

def each
  if block_given?
    page = 1
    while page
      if (response = get(query: { page: page, limit: 0 })).code == 200
        (response.values.detect { |v| v.is_a?(Array) } || []).each do |item|
          yield(item)
        end
        page =
          if response['total_pages'] > page
            page + 1
          else
            nil
          end
      else
        fail Error, "Unsuccessful response code: #{response.code}"
      end
    end
  end
end