stitch_data 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Rakefile +6 -0
- data/lib/stitch_data/api.rb +50 -0
- data/lib/stitch_data/errors.rb +6 -0
- data/lib/stitch_data/version.rb +3 -0
- data/lib/stitch_data.rb +24 -0
- data/spec/lib/stitch_data/stitch_data_spec.rb +72 -0
- data/spec/spec_helper.rb +25 -0
- metadata +169 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 6b9d6b674966e5a8c42715ad952b620a63775e5f
|
|
4
|
+
data.tar.gz: ebc36c487b1598138b3850485ef432ec83ffc911
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a7532a43feceff09cb0117b6f8bd016c364c9d02c85404f58a3b68e3a8e33f5c1109ead228b4a924b783db7ff7e7b8cb946ee37f1f7f86765665da651e98dfa5
|
|
7
|
+
data.tar.gz: 2841465f8ac9f1a8ee34d67d5e83bd184ac6d0b7b97508ce76a60801c2e79e5601061f84e89c41da1c810c8240333db09e3365cdca3ab98b59e47e1bdfd2179b
|
data/Rakefile
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module StitchData
|
|
2
|
+
class Api
|
|
3
|
+
attr_accessor :table_name, :sequence, :key_names, :data
|
|
4
|
+
API_BASE_URL = 'https://api.stitchdata.com/v2/import'.freeze
|
|
5
|
+
DEFAULT_API_ACTION = "upsert".freeze
|
|
6
|
+
DEFAULT_REQUEST_PARAMS = { content_type: 'application/json', accept: 'json' }.freeze
|
|
7
|
+
|
|
8
|
+
def initialize(table_name, sequence, key_names, data)
|
|
9
|
+
validate_key_names_is_array(key_names)
|
|
10
|
+
@request_params = { Authorization: "Bearer #{StitchData.configuration.token}" }.merge(DEFAULT_REQUEST_PARAMS)
|
|
11
|
+
@table_name = table_name.to_s
|
|
12
|
+
@sequence = sequence.to_sym
|
|
13
|
+
@key_names = key_names.map(&:to_s)
|
|
14
|
+
@data = build_records(data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def upsert!
|
|
18
|
+
stitch_post_request("#{API_BASE_URL}/push")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def validate!
|
|
22
|
+
stitch_post_request("#{API_BASE_URL}/validate")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def build_records(records)
|
|
28
|
+
records.map do |record|
|
|
29
|
+
{
|
|
30
|
+
client_id: StitchData.configuration.client_id,
|
|
31
|
+
table_name: table_name,
|
|
32
|
+
sequence: record[sequence].to_i,
|
|
33
|
+
action: DEFAULT_API_ACTION,
|
|
34
|
+
key_names: key_names,
|
|
35
|
+
data: record
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def validate_key_names_is_array(key_names)
|
|
41
|
+
raise StitchData::Errors::WrongUpsertFields, 'key_names field must be an Array' unless key_names.is_a?(Array)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def stitch_post_request(url)
|
|
45
|
+
JSON.parse(RestClient.post(url, @data.to_json, @request_params))
|
|
46
|
+
rescue RestClient::ExceptionWithResponse => e
|
|
47
|
+
{ 'status' => e.message, 'message' => JSON.parse(e.response)['errors'] }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/lib/stitch_data.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'rest-client'
|
|
3
|
+
require 'stitch_data/api'
|
|
4
|
+
require 'stitch_data/errors'
|
|
5
|
+
module StitchData
|
|
6
|
+
def self.configuration
|
|
7
|
+
@configuration ||= Configuration.new
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.configure
|
|
11
|
+
self.configuration ||= Configuration.new
|
|
12
|
+
yield(configuration) if block_given?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Main configuration class.
|
|
16
|
+
class Configuration
|
|
17
|
+
attr_accessor :token, :client_id
|
|
18
|
+
|
|
19
|
+
def initialize
|
|
20
|
+
@token = nil
|
|
21
|
+
@client_id = nil
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require 'rspec'
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
RSpec.describe 'StitchData' do
|
|
5
|
+
before :each do
|
|
6
|
+
mocked_time = Time.new
|
|
7
|
+
@first_record = { id: '1234', event_name: 'logged_in', created_at: mocked_time }
|
|
8
|
+
@second_record = { id: '12345', event_name: 'logged_out', created_at: mocked_time }
|
|
9
|
+
@data = [@first_record, @second_record]
|
|
10
|
+
@sequence = :created_at
|
|
11
|
+
@table_name = 'events'
|
|
12
|
+
@key_names = [:id]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe :initalize do
|
|
16
|
+
|
|
17
|
+
it 'should validate upsert field key_name is of Array data type' do
|
|
18
|
+
expect { StitchData::Api.new(@table_name, @sequence, :id, @data) }
|
|
19
|
+
.to raise_error(StitchData::Errors::WrongUpsertFields)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'should build valid data structue' do
|
|
23
|
+
stitch_data = StitchData::Api.new(@table_name, @sequence, @key_names, @data)
|
|
24
|
+
expect(stitch_data.data).to eq(
|
|
25
|
+
[
|
|
26
|
+
{
|
|
27
|
+
client_id: StitchData.configuration.client_id,
|
|
28
|
+
table_name: @table_name,
|
|
29
|
+
sequence: @first_record[@sequence].to_i,
|
|
30
|
+
action: StitchData::Api::DEFAULT_API_ACTION,
|
|
31
|
+
key_names: @key_names.map(&:to_s),
|
|
32
|
+
data: @first_record
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
client_id: StitchData.configuration.client_id,
|
|
36
|
+
table_name: @table_name,
|
|
37
|
+
sequence: @second_record[@sequence].to_i,
|
|
38
|
+
action: StitchData::Api::DEFAULT_API_ACTION,
|
|
39
|
+
key_names: @key_names.map(&:to_s),
|
|
40
|
+
data: @second_record
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
describe :stitch_post_request do
|
|
47
|
+
context :succesful_request do
|
|
48
|
+
before :each do
|
|
49
|
+
stub_request(:post, "https://api.stitchdata.com/v2/import/validate")
|
|
50
|
+
.to_return(status: 200, body: { status: "OK", message: "Valid" }.to_json)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'should return a hash with status and message' do
|
|
54
|
+
expect(StitchData::Api.new(@table_name, @sequence, @key_names, @data).validate!)
|
|
55
|
+
.to eq("status" => "OK", "message" => "Valid")
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
context :failed_request do
|
|
59
|
+
before :each do
|
|
60
|
+
stub_request(:post, "https://api.stitchdata.com/v2/import/validate")
|
|
61
|
+
.to_return(status: 403, body: '{ "status":"ERROR","errors":"An array of records is expected" }')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it 'should return hash with status and message' do
|
|
65
|
+
expect(StitchData::Api.new(@table_name, @sequence, @key_names, @data).validate!)
|
|
66
|
+
.to eq("status" => "403 Forbidden", "message" => "An array of records is expected")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
end
|
|
72
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "codeclimate-test-reporter"
|
|
2
|
+
CodeClimate::TestReporter.start
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "pry"
|
|
6
|
+
require 'json'
|
|
7
|
+
require 'webmock/rspec'
|
|
8
|
+
require './lib/stitch_data/api'
|
|
9
|
+
require './lib/stitch_data'
|
|
10
|
+
|
|
11
|
+
RSpec.configure do |config|
|
|
12
|
+
config.disable_monkey_patching!
|
|
13
|
+
config.formatter = "documentation"
|
|
14
|
+
config.color = true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
require 'simplecov'
|
|
18
|
+
|
|
19
|
+
# save to CircleCI's artifacts directory if we're on CircleCI
|
|
20
|
+
if ENV['CIRCLE_ARTIFACTS']
|
|
21
|
+
dir = File.join(ENV['CIRCLE_ARTIFACTS'], "coverage")
|
|
22
|
+
SimpleCov.coverage_dir(dir)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
SimpleCov.start
|
metadata
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: stitch_data
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Omri Shuva
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-12-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.1'
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: 3.1.0
|
|
37
|
+
type: :development
|
|
38
|
+
prerelease: false
|
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - "~>"
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '3.1'
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 3.1.0
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: webmock
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: pry
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
- !ruby/object:Gem::Dependency
|
|
76
|
+
name: codeclimate-test-reporter
|
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
type: :development
|
|
83
|
+
prerelease: false
|
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - "~>"
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
- !ruby/object:Gem::Dependency
|
|
90
|
+
name: simplecov
|
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
type: :development
|
|
97
|
+
prerelease: false
|
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
- !ruby/object:Gem::Dependency
|
|
104
|
+
name: json
|
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - "~>"
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: 1.8.0
|
|
110
|
+
type: :runtime
|
|
111
|
+
prerelease: false
|
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - "~>"
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: 1.8.0
|
|
117
|
+
- !ruby/object:Gem::Dependency
|
|
118
|
+
name: rest-client
|
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - "~>"
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: 1.8.0
|
|
124
|
+
type: :runtime
|
|
125
|
+
prerelease: false
|
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
127
|
+
requirements:
|
|
128
|
+
- - "~>"
|
|
129
|
+
- !ruby/object:Gem::Version
|
|
130
|
+
version: 1.8.0
|
|
131
|
+
description: Wraper for the upsert method used for import data into stitch
|
|
132
|
+
email: omri@tailorbrands.com
|
|
133
|
+
executables: []
|
|
134
|
+
extensions: []
|
|
135
|
+
extra_rdoc_files: []
|
|
136
|
+
files:
|
|
137
|
+
- Rakefile
|
|
138
|
+
- lib/stitch_data.rb
|
|
139
|
+
- lib/stitch_data/api.rb
|
|
140
|
+
- lib/stitch_data/errors.rb
|
|
141
|
+
- lib/stitch_data/version.rb
|
|
142
|
+
- spec/lib/stitch_data/stitch_data_spec.rb
|
|
143
|
+
- spec/spec_helper.rb
|
|
144
|
+
homepage: https://github.com/TailorBrands/stitch_data
|
|
145
|
+
licenses: []
|
|
146
|
+
metadata: {}
|
|
147
|
+
post_install_message:
|
|
148
|
+
rdoc_options: []
|
|
149
|
+
require_paths:
|
|
150
|
+
- lib
|
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
|
+
requirements:
|
|
153
|
+
- - ">="
|
|
154
|
+
- !ruby/object:Gem::Version
|
|
155
|
+
version: '0'
|
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
|
+
requirements:
|
|
158
|
+
- - ">="
|
|
159
|
+
- !ruby/object:Gem::Version
|
|
160
|
+
version: '0'
|
|
161
|
+
requirements: []
|
|
162
|
+
rubyforge_project:
|
|
163
|
+
rubygems_version: 2.5.2
|
|
164
|
+
signing_key:
|
|
165
|
+
specification_version: 4
|
|
166
|
+
summary: Ruby wraper for the Stitch Data Api
|
|
167
|
+
test_files:
|
|
168
|
+
- spec/lib/stitch_data/stitch_data_spec.rb
|
|
169
|
+
- spec/spec_helper.rb
|