embulk-input-spotx 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b6cc25940e7346519766d95f9ce9d30dfa3e9c08
4
+ data.tar.gz: 519189017d5aab5267ff4367f6f967930dadf6b2
5
+ SHA512:
6
+ metadata.gz: 8eebb03ec9f0f91222d7fbc98495b112f15acb1f43baf68d78f34a057fb9effbf09fa22bc3d85a4ec947d26a1d669a7a8e176c89b14eeb89301bcc80ffced317
7
+ data.tar.gz: 280aa257e4ccb08e49954944de3d939f997fe4f0c881a43010b485445f0b029581cb82d3170498219064514a7cb86a248decae5572ac811f546899d0f2be5895
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *~
2
+ /pkg/
3
+ /tmp/
4
+ /.bundle/
5
+ /Gemfile.lock
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ jruby-9.1.13.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org/'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+
2
+ MIT License
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Spotx API data input plugin for Embulk
2
+
3
+ Loads records from Spotx API
4
+
5
+ ## Overview
6
+
7
+ * **Plugin type**: input
8
+ * **Resume supported**: no
9
+ * **Cleanup supported**: no
10
+ * **Guess supported**: no
11
+
12
+ ## Configuration
13
+
14
+ - **endpoint**: Spotx API endpoint to get data from (string, required)
15
+ - **client_id**: Spotx API client ID (string, required)
16
+ - **client_secret**: Spotx API client secret (string, required)
17
+ - **refresh_token**: Spotx API refresh token (string, required)
18
+
19
+ ## Example
20
+
21
+ ```yaml
22
+ in:
23
+ type: spotx
24
+ endpoint: https://api.spotxchange.com/1.0/Publisher($ID)/Channels/AdvertiserReport?date_range=last_7_days
25
+ client_id: $client_id
26
+ client_secret: $client_secret
27
+ refresh_token: $refresh_token
28
+ parser:
29
+ type: json
30
+ ```
31
+
32
+
33
+ ## Build
34
+
35
+ ```
36
+ $ rake build
37
+ ```
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task default: :build
@@ -0,0 +1,22 @@
1
+
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "embulk-input-spotx"
4
+ spec.version = "0.1.0"
5
+ spec.authors = ["Ming Liu"]
6
+ spec.summary = "Spotx API data input plugin for Embulk"
7
+ spec.description = "Loads records from Spotx API."
8
+ spec.email = ["liuming@lmws.net"]
9
+ spec.licenses = ["MIT"]
10
+ spec.homepage = "https://github.com/liuming/embulk-input-spotx"
11
+
12
+ spec.files = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
13
+ spec.test_files = spec.files.grep(%r{^(test|spec)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_dependency 'http', '~> 3.0.0'
17
+
18
+ #spec.add_dependency 'YOUR_GEM_DEPENDENCY', ['~> YOUR_GEM_DEPENDENCY_VERSION']
19
+ spec.add_development_dependency 'embulk', ['>= 0.8.39']
20
+ spec.add_development_dependency 'bundler', ['>= 1.10.6']
21
+ spec.add_development_dependency 'rake', ['>= 10.0']
22
+ end
@@ -0,0 +1,91 @@
1
+ require 'http'
2
+
3
+ module Embulk
4
+ module Input
5
+
6
+ class Spotx < InputPlugin
7
+ Plugin.register_input("spotx", self)
8
+
9
+ def self.transaction(config, &control)
10
+ task = {
11
+ "endpoint" => config.param("endpoint", :string),
12
+ "client_id" => config.param("client_id", :string),
13
+ "client_secret" => config.param("client_secret", :string),
14
+ "refresh_token" => config.param("refresh_token", :string),
15
+ "headers" => config.param("headers", :hash, default: {}),
16
+ }
17
+
18
+ columns = [
19
+ Column.new(0, "record", :json),
20
+ ]
21
+
22
+ resume(task, columns, 1, &control)
23
+ end
24
+
25
+ def self.resume(task, columns, count, &control)
26
+ _task_reports = yield(task, columns, count)
27
+
28
+ next_config_diff = {}
29
+ return next_config_diff
30
+ end
31
+
32
+ # TODO
33
+ # def self.guess(config)
34
+ # sample_records = [
35
+ # {"example"=>"a", "column"=>1, "value"=>0.1},
36
+ # {"example"=>"a", "column"=>2, "value"=>0.2},
37
+ # ]
38
+ # columns = Guess::SchemaGuess.from_hash_records(sample_records)
39
+ # return {"columns" => columns}
40
+ # end
41
+
42
+ def init
43
+ @endpoint = task["endpoint"]
44
+ @client_id = task["client_id"]
45
+ @client_secret = task["client_secret"]
46
+ @refresh_token = task["refresh_token"]
47
+ @headers = task["headers"]
48
+ end
49
+
50
+ def refresh_access_token
51
+ uri = 'https://publisher-api.spotxchange.com/1.0/token'
52
+ request_body = {
53
+ client_id: @client_id,
54
+ client_secret: @client_secret,
55
+ refresh_token: @refresh_token,
56
+ grant_type: 'refresh_token',
57
+ }
58
+ response = ::HTTP.post(uri, form: request_body)
59
+ response_body = JSON.parse(response.body.to_s)
60
+ response_body['value']['data']['access_token']
61
+ end
62
+
63
+ def request_data
64
+ auth = {"Authorization" => "Bearer #{refresh_access_token}"}
65
+ response = ::HTTP.get(@endpoint, headers: @headers.merge(auth))
66
+ body = ''
67
+ response.body.each do |chunk|
68
+ chunk.gsub!('{"value":{"data":[', '')
69
+ chunk.gsub!(']}}', '')
70
+ chunk.gsub!('},{', "}\n{")
71
+ body += chunk
72
+ end
73
+ return body
74
+ end
75
+
76
+ def run
77
+ content = request_data
78
+ content.split("\n").map do |line|
79
+ row = JSON.parse(line)
80
+ page_builder.add([row])
81
+ row
82
+ end
83
+ page_builder.finish
84
+
85
+ task_report = {}
86
+ return task_report
87
+ end
88
+ end
89
+
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embulk-input-spotx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ming Liu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: embulk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.39
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.39
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.10.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.10.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Loads records from Spotx API.
70
+ email:
71
+ - liuming@lmws.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".ruby-version"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - embulk-input-spotx.gemspec
83
+ - lib/embulk/input/spotx.rb
84
+ homepage: https://github.com/liuming/embulk-input-spotx
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.13
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Spotx API data input plugin for Embulk
108
+ test_files: []