Class: Cloudxls::ReadRequest

Inherits:
Object
  • Object
show all
Includes:
BaseRequest
Defined in:
lib/cloudxls.rb

Constant Summary collapse

DATA_PARAMETERS =
%w[excel file]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseRequest

#api_base, #api_key, #data, #each, #initialize, #path_to, #save_as, #start, #test_key?, #write_to

Instance Attribute Details

#client_optionsObject (readonly)

Returns the value of attribute client_options.



160
161
162
# File 'lib/cloudxls.rb', line 160

def client_options
  @client_options
end

#file_formatObject

Returns the value of attribute file_format.



161
162
163
# File 'lib/cloudxls.rb', line 161

def file_format
  @file_format
end

#post_dataObject (readonly)

post_data is an array of key,value arrays. Reason:

  • A key can appear multiple times (for multiple sheets)

  • Parameters need to be in the right order: template - config - data

Example: [[“separator”, “,”], [“csv”, “hello,world”]]



159
160
161
# File 'lib/cloudxls.rb', line 159

def post_data
  @post_data
end

Instance Method Details

#add_data(params) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/cloudxls.rb', line 165

def add_data(params)
  params.map do |key,value|
    key = key.to_s
    if DATA_PARAMETERS.include?(key)
      value = UploadIO.new(value, "text/csv", "data.csv")
    end
    @post_data << [key, value]
  end
  self
end

#as_csvObject

Set request to CSV



202
203
204
205
# File 'lib/cloudxls.rb', line 202

def as_csv
  self.file_format = "csv"
  self
end

#as_jsonObject

Set request to JSON



193
194
195
196
# File 'lib/cloudxls.rb', line 193

def as_json
  self.file_format = "json"
  self
end

#response_bodyString

Response as string

Returns:

  • (String)


180
181
182
183
184
185
186
187
# File 'lib/cloudxls.rb', line 180

def response_body
  # TODO: optimize
  str = ""
  each do |chunk|
    str << chunk
  end
  str
end

#to_hString

Response as Hash (used with json)

Returns:

  • (String)


211
212
213
# File 'lib/cloudxls.rb', line 211

def to_h
  JSON.load(response_body)
end