framed_uploader 0.0.1

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: a702bf11763ea273e84d175266a9d89216ba1cb8
4
+ data.tar.gz: 2881546305989da89820a2a31b821dae7912ef34
5
+ SHA512:
6
+ metadata.gz: b7ae8d65b1120a3d4b816696b2ea86f3915dc4e577effa8994031c33b268b5ff3a1881e3f2784ecb6458249b2043461233cf1e9d444207b3e70392b4467766b7
7
+ data.tar.gz: f97db4922009c66a03e8b4585703eea9a8dcd966e5827b49a5b5c32c2fb0e9add96cc57ced21333979381f327508edc3e81676efd8d126fe80c111f62f3f6444
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.ltate
15
+ /.bundle
16
+ /vendor/bundle/
17
+ /vendor/ruby/
18
+ db/*.sqlite3
19
+ /db/*.sqlite3-journal
20
+ /log/*
21
+ /tmp/*
22
+ **.war
23
+ *.rbc
24
+ *.sassc
25
+ .rspec
26
+ .redcar/
27
+ .sass-cache
28
+ /config/config.yml
29
+ /config/database.yml
30
+ /config/application.yml
31
+ /config/secrets.yml
32
+ /coverage.data
33
+ /db/*.javadb/
34
+ /db/*.sqlite3
35
+ /spec/tmp/*
36
+ /cache
37
+ /capybara*
38
+ /capybara-*.html
39
+ /gems
40
+ /specifications
41
+ rerun.txt
42
+ .zeus.sock
43
+ tags
44
+ gems.tags
45
+ **.orig
46
+ .DS_Store
47
+ /nbproject/
48
+ .idea
49
+ /*.tmproj
50
+ **.swp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ framed_uploader
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in framed_uploader.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ ## Framed Uploader
2
+
3
+ This is the Ruby client library for uploading custom data to the [Framed](http://framed.io/) data platform.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'framed_uploader'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install framed_uploader
20
+
21
+ ## Uploading user information
22
+
23
+ You can upload custom information about your users (ex: a dump of a users SQL table), which will be integrated
24
+ directly into Framed's machine learning models for making more accurate predictions.
25
+
26
+ The uploader can be used as a library from your application code, or with the bundled CLI runner.
27
+ You will need your API key, which you can get from your Framed dashboard.
28
+
29
+ As a library:
30
+
31
+ Using the CLI runner script:
32
+
33
+ ```ruby
34
+ bundle exec framed-upload API_KEY FILENAME
35
+ ```
36
+
37
+ Or as a library:
38
+
39
+ ```ruby
40
+ require 'framed_uploader'
41
+
42
+ uploader = FramedUploader::Uploader.new("my-api-key")
43
+ uploader.upload("/path/to/users.csv")
44
+ ```
45
+
46
+ Using the CLI:
47
+
48
+ Usage: `bundle exec framed-upload API_KEY [FILENAMES ...]`
49
+
50
+ Example:
51
+
52
+ ```
53
+ bundle exec framed-upload abcd1234 users.csv
54
+ ```
55
+
56
+ Please note that user data **must** be in a CSV-formatted file named `users.csv`, and there **must** be an
57
+ `id` column containing a value that uniquely identifies each user (ex: the primary key on a `users` table).
58
+ For example:
59
+
60
+ ```
61
+ id,name,email
62
+ 3,"John Smith","john@example.com"
63
+ 18,"Jane Doe',"jane@example.com"
64
+ 88,"Fred Flinstone","fred@example.com"
65
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/framed-upload ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'framed_uploader'
4
+
5
+ api_key, *filenames = ARGV
6
+
7
+ USAGE = "Usage: framed-upload API_KEY [FILENAME ...]"
8
+
9
+ if api_key.nil? || filenames.empty?
10
+ puts USAGE
11
+ exit 1
12
+ end
13
+
14
+ uploader = FramedUploader::Uploader.new(api_key)
15
+ uploader.upload(*filenames)
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'framed_uploader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "framed_uploader"
8
+ spec.version = FramedUploader::VERSION
9
+ spec.authors = ["Andrew Berls"]
10
+ spec.email = ["andrew.berls@gmail.com"]
11
+ spec.summary = %q{Framed user data uploader}
12
+ spec.description = %q{Framed user data uploader}
13
+ spec.homepage = ""
14
+ spec.license = ""
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'aws-sdk', '~> 2'
22
+ spec.add_dependency 'addressable', '~> 2.3.5'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,83 @@
1
+ require 'addressable/template'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'aws-sdk'
5
+
6
+ module FramedUploader
7
+ class CredentialsError < StandardError; end
8
+ class FileNotFoundError < StandardError; end
9
+
10
+ class Uploader
11
+ CREDS_ENDPOINT = 'https://app.framed.io/uploads/1.0/credentials'.freeze
12
+
13
+ def initialize(api_key)
14
+ @api_key = api_key
15
+ end
16
+
17
+ def upload(*filenames)
18
+ validate_files!(filenames)
19
+
20
+ creds_response = get_credentials!
21
+ tmpl = Addressable::Template.new(creds_response.fetch(:template))
22
+ bucket = creds_response.fetch(:bucket)
23
+ company_id = creds_response.fetch(:company_id)
24
+ batch_timestamp = Time.now.to_i
25
+ s3 = s3_client(creds_response)
26
+
27
+ filenames.each do |filename|
28
+ s3_key = tmpl.expand({"company_id" => company_id,
29
+ "timestamp" => batch_timestamp,
30
+ "filename" => filename}).path
31
+
32
+ File.open(filename, 'rb') do |body|
33
+ s3.put_object(bucket: bucket, key: s3_key, body: body)
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def validate_files!(filenames)
41
+ filenames.each do |filename|
42
+ path = File.expand_path(filename)
43
+ if !File.exists?(path)
44
+ raise FileNotFoundError.new("#{path} doesn't exist")
45
+ end
46
+ end
47
+ end
48
+
49
+ # options - Hash of
50
+ # :region
51
+ # :access_key
52
+ # :secret_key
53
+ # :session_token
54
+ def s3_client(options)
55
+ region = options.fetch(:region)
56
+ access_key = options.fetch(:access_key)
57
+ secret_key = options.fetch(:secret_key)
58
+ session_token = options.fetch(:session_token)
59
+
60
+ s3 = Aws::S3::Client.new(
61
+ region: region,
62
+ credentials: Aws::Credentials.new(access_key, secret_key, session_token)
63
+ )
64
+ end
65
+
66
+ # Retrieve S3 credentials and information from the Framed API
67
+ def get_credentials!
68
+ uri = URI(CREDS_ENDPOINT)
69
+ req = Net::HTTP::Post.new(uri.request_uri)
70
+ req.basic_auth(@api_key, "")
71
+ resp = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |https|
72
+ https.request(req)
73
+ end
74
+
75
+ if resp.is_a?(Net::HTTPSuccess)
76
+ JSON.parse(resp.body, {:symbolize_names => true})
77
+ else
78
+ raise CredentialsError.new("Error retrieving credentials; please try again shortly (#{resp.body})")
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module FramedUploader
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "framed_uploader/version"
2
+ require "framed_uploader/uploader"
3
+
4
+ module FramedUploader
5
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: framed_uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Berls
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.3.5
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
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: Framed user data uploader
70
+ email:
71
+ - andrew.berls@gmail.com
72
+ executables:
73
+ - framed-upload
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".ruby-gemset"
79
+ - ".ruby-version"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/framed-upload
84
+ - framed_uploader.gemspec
85
+ - lib/framed_uploader.rb
86
+ - lib/framed_uploader/uploader.rb
87
+ - lib/framed_uploader/version.rb
88
+ homepage: ''
89
+ licenses:
90
+ - ''
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Framed user data uploader
112
+ test_files: []