Method: Cts::Mpx::Query#range

Defined in:
lib/cts/mpx/query.rb

#rangeString

Returns String range in the service shell style.

Returns:

  • (String)

    String range in the service shell style



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cts/mpx/query.rb', line 28

class Query
  include Creatable

  attribute name: 'account_id', kind_of: String
  attribute name: 'endpoint', kind_of: String
  attribute name: 'extra_path', kind_of: String
  attribute name: 'fields', kind_of: String
  attribute name: 'ids', kind_of: Array
  attribute name: 'page', kind_of: Driver::Page
  attribute name: 'query', kind_of: Hash
  attribute name: 'range', kind_of: String
  attribute name: 'return_count'
  attribute name: 'return_entries'
  attribute name: 'service', kind_of: String
  attribute name: 'sort', kind_of: String

  # List of attributes availble
  # @return [Symbol[]]
  def attributes
    %i[account_id endpoint extra_path fields ids query range return_count return_entries service sort]
  end

  # Instiatiate a page and query, set return's to false.
  def initialize
    @page = Driver::Page.new

    @return_entries = true
    @return_count = false
    @query = {}
  end

  # List of entries created from the page
  # @return [Entries] populated Entries object
  def entries
    @page.to_mpx_entries
  end

  # Run the query
  # @param [User] user user to make calls with
  # @return [Self]
  def run(user: nil)
    Driver::Helpers.required_arguments %i[user], binding
    Driver::Exceptions.raise_unless_argument_error? user, User

    raise "service must be set" unless service
    raise "endpoint must be set" unless endpoint

    response = Services::Data.get params.merge(user: user)
    @page = response.page
    self
  end

  # Hash representation of the query data.  Has a key for params and for entries.
  # @param [Boolean] include_entries include the entries array or not
  # @return [Hash]
  def to_h(include_entries: true)
    h = { params: params }
    h[:entries] = entries.to_h if include_entries
    h
  end

  def save_results_to_file(filename)
    File.write filename, Oj.dump(to_h)
  end

  def self.load_results_from_file(filename)
    tmp_to_h = Oj.load File.read filename
    n = Query.create tmp_to_h[:params]
    n.page.entries = tmp_to_h[:entries][:entries]
    n.page.xmlns = tmp_to_h[:entries][:xmlns]
    n
  end

  private

  # List of parameters that are currently set in the query
  # @return [Hash]
  def params
    output = {}

    attributes.each do |attribute|
      output.store attribute, instance_variable_get("@#{attribute}") unless instance_variable_get("@#{attribute}").nil?
    end

    output[:count] = output.delete :return_count unless output[:return_count].nil?
    output[:entries] = output.delete :return_entries unless output[:return_entries].nil?

    output
  end
end