Class: Daru::IO::Importers::JSON
- Defined in:
- lib/daru/io/importers/json.rb
Overview
JSON Importer Class, that extends from_json
and read_json
methods
to Daru::DataFrame
Direct Known Subclasses
Class Method Summary collapse
-
.from(instance) ⇒ Daru::IO::Importers::JSON
Loads from a Ruby structure of Hashes and / or Arrays.
-
.read(path) ⇒ Daru::IO::Importers::JSON
Reads data from a json file / remote json response.
Instance Method Summary collapse
-
#call(*columns, order: nil, index: nil, **named_columns) ⇒ Daru::DataFrame
Imports a
Daru::DataFrame
from a JSON Importer instance. -
#from(instance) ⇒ Daru::IO::Importers::JSON
Loads from a Ruby structure of Hashes and / or Arrays.
-
#initialize ⇒ JSON
constructor
Checks for required gem dependencies of JSON Importer.
-
#read(path) ⇒ Daru::IO::Importers::JSON
Reads data from a json file / remote json response.
Methods inherited from Base
Methods inherited from Base
Constructor Details
#initialize ⇒ JSON
Checks for required gem dependencies of JSON Importer
13 14 15 16 17 |
# File 'lib/daru/io/importers/json.rb', line 13 def initialize require 'open-uri' require 'json' optional_gem 'jsonpath' end |
Class Method Details
.from(instance) ⇒ Daru::IO::Importers::JSON
Loads from a Ruby structure of Hashes and / or Arrays
51 52 53 54 55 |
# File 'lib/daru/io/importers/json.rb', line 51 def from(instance) @file_data = instance @json = @file_data.is_a?(String) ? ::JSON.parse(@file_data) : @file_data self end |
.read(path) ⇒ Daru::IO::Importers::JSON
Reads data from a json file / remote json response
35 36 37 38 39 |
# File 'lib/daru/io/importers/json.rb', line 35 def read(path) @file_data = ::JSON.parse(open(path).read) @json = @file_data self end |
Instance Method Details
#call(*columns, order: nil, index: nil, **named_columns) ⇒ Daru::DataFrame
107 108 109 110 111 112 113 114 115 |
# File 'lib/daru/io/importers/json.rb', line 107 def call(*columns, order: nil, index: nil, **named_columns) init_opts(*columns, order: order, index: index, **named_columns) @data = fetch_data @index = at_jsonpath(@index) @order = at_jsonpath(@order) @order ||= Array.new(@columns.count) { |x| x } + @named_columns.keys Daru::DataFrame.new(@data, order: @order, index: @index) end |
#from(instance) ⇒ Daru::IO::Importers::JSON
Loads from a Ruby structure of Hashes and / or Arrays
51 52 53 54 55 |
# File 'lib/daru/io/importers/json.rb', line 51 def from(instance) @file_data = instance @json = @file_data.is_a?(String) ? ::JSON.parse(@file_data) : @file_data self end |
#read(path) ⇒ Daru::IO::Importers::JSON
Reads data from a json file / remote json response
35 36 37 38 39 |
# File 'lib/daru/io/importers/json.rb', line 35 def read(path) @file_data = ::JSON.parse(open(path).read) @json = @file_data self end |