Class: Embulk::Input::HttpInputPlugin

Inherits:
InputPlugin
  • Object
show all
Defined in:
lib/embulk/input/http.rb

Defined Under Namespace

Classes: Iter, IterJson, IterXML

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transaction(config, &control) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/embulk/input/http.rb', line 10

def self.transaction(config, &control)
  url = config.param("url", :string)
  schema = config.param("schema", :array)
  method = config.param("method", :string, default: "get")
  params = config.param("params", :array, default: [])
  iterate = config.param("iterate", :hash)
  open_timeout = config.param("open_timeout", :float, default: 2.0)
  read_timeout = config.param("read_timeout", :float, default: 10.0)

  data_type = iterate["type"]
  unless ["json", "xml"].include?(data_type)
    raise "Unknown data_type #{data_type}, only supported for json or xml"
  end

  columns = schema.each_with_index.map do |c, i|
    Column.new(i, c["name"], c["type"].to_sym)
  end

  task = {
    :url => url,
    :method => method,
    :params => params,
    :schema => schema,
    :iterate => iterate,
    :open_timeout => open_timeout,
    :read_timeout => read_timeout
  }

  report = yield(task, columns, 1)
  config.merge(report["done"].flatten.compact)
  {}
end

Instance Method Details

#runObject



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
# File 'lib/embulk/input/http.rb', line 43

def run
  schema = @task["schema"]
  iterate = @task["iterate"]

  data = fetch.body
  data_type = iterate["type"]

  case data_type
  when "json"
    iter = IterJson.new(data, iterate["path"])
  when "xml"
    iter = IterXML.new(data, iterate["path"])
  else
    raise "Unsupported data_type #{data_type}"
  end

  rows = 0
  iter.each do |e|
    rows += 1
    @page_builder.add(schema.map{|c|
      name = c["name"]
      type = c["type"]
      val = e[name].nil? ? "" : e[name]
      case type
      when "string"
        val
      when "long"
        val.to_i
      when "double"
        val.to_f
      when "boolean"
        ["yes", "true", "1"].include?(val)
      when "timestamp"
        (val.nil? || val.empty?) ? nil : Time.strptime(val, c["format"])
      else
        raise "Unsupported type #{type}"
      end
    })
  end
  @page_builder.finish

  {:rows => rows}
end