Class: INatGet::Data::Helper Private

Inherits:
Object
  • Object
show all
Defined in:
lib/inat-get/data/helpers/base.rb,
lib/inat-get/data/helpers/defs.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

Identifications, Observations, Places, Projects, Taxa, Users

Defined Under Namespace

Classes: Field, Identifications, Observations, Places, Projects, Taxa, Users

Constant Summary collapse

UUID_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/

Must be implemented in descendants collapse

Interface collapse

Definitions DSL collapse

Class Method Details

.field(key, cls, *args) ⇒ Field

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Raises:

  • (ArgumentError)


110
111
112
113
114
115
# File 'lib/inat-get/data/helpers/base.rb', line 110

def field key, cls, *args
  raise ArgumentError, "Invalid field key: #{ key.inspect }", caller_locations unless key.is_a?(Symbol)
  raise ArgumentError, "Invalid field class: #{ cls.inspect }", caller_locations unless cls.is_a?(Class) && cls < INatGet::Data::Helper::Field
  @fields ||= {}
  @fields[key] = cls.new(self.instance, key, *args)
end

.fieldsHash<Field>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



118
119
120
# File 'lib/inat-get/data/helpers/base.rb', line 118

def fields
  @fields ||= {}
end

Instance Method Details

#definitionsHash<Field>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



129
# File 'lib/inat-get/data/helpers/base.rb', line 129

def definitions() = self.class.fields

#endpointSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)


29
# File 'lib/inat-get/data/helpers/base.rb', line 29

def endpoint() = raise NotImplementedError, "Not implemented methods 'endpoint' for abstract Helper", caller_locations

#managerINatGet::Data::Manager

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/inat-get/data/helpers/base.rb', line 16

def manager
  @manager ||= get_manager
end

#prepare_query(**query) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inat-get/data/helpers/base.rb', line 36

def prepare_query **query
  # Вызывается в процессе нормализации условий — ДО преобразований для API и Sequel
  # Выполняем слеюущие преобразования
  #   - Date и диапазоны Date — в диапазоны Time
  #   - Одиночные значения там, где допустим перечень — в Set
  #   - Диапазоны Rank — в Set<Rank>
  #   - Широта, долгота и Location — в диапазоны широты и долготы
  #   - Символы в строки
  # Модели в примитивы на этом этапе НЕ преобразуем, тем более не преобразуем данные между полями.
  result = {}
  defs = self.definitions
  query.each do |key, value|
    definition = defs[key]
    raise KeyError, "Invalid query key: #{ key }", caller_locations if definition.nil?
    prepared = definition.prepare value
    if prepared.is_a?(Hash)
      result.merge! prepared
    else
      result[key] = prepared
    end
  end
  result
end

#query_to_api(**query) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Array of request definitions

Returns:

  • (Array<Hash>)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/inat-get/data/helpers/base.rb', line 73

def query_to_api **query
  # default implementation
  defs = self.definitions
  converted = {}
  query.each do |key, value|
    definition = defs[key]
    raise KeyError, "Invalid query key: #{ key }", caller_locations if definition.nil?
    converted_value = definition.to_api value
    if converted_value.is_a?(Hash)
      converted.merge! converted_value
    else
      converted[key] = converted_value
    end
  end
  [ { endpoint: manager.endpoint, query: converted } ]
end

#query_to_sequel(**query) ⇒ Sequel::SQL::Expression

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Sequel::SQL::Expression)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/inat-get/data/helpers/base.rb', line 91

def query_to_sequel **query
  # default implementation
  defs = self.definitions
  sequel_terms = []
  query.each do |key, value|
    definition = defs[key]
    raise KeyError, "Invalid query key: #{ key }", caller_locations if definition.nil?
    sequel_terms << definition.to_sequel(value)
  end
  Sequel.&(*sequel_terms)
end

#validate_query!(**query) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises exception if any field is not valid.

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
# File 'lib/inat-get/data/helpers/base.rb', line 62

def validate_query! **query
  defs = self.definitions
  query.each do |key, value|
    definition = defs[key]
    raise KeyError, "Invalid query key: #{ key }", caller_locations if definition.nil?
    raise ArgumentError, "Invalid query value: #{ key } => #{ value.inspect }" unless definition.valid?(value)
  end
end