transloadit_fetcher 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: e28abc03fbe22c43ec50854a9d8db75f945593da
4
+ data.tar.gz: 809c690374503db56e43360fe1f0cdbc1a2c4e38
5
+ SHA512:
6
+ metadata.gz: 192d617abeb7389c4738e8ecb0e7ada1973d5a5de2cf43a3dcbfb41b49fb5ee6fcbb07f86ffbf3cbf548dda92779c16c2b5b579b39841c02c5067884898cc487
7
+ data.tar.gz: 28d0d9d3c3a23c7623507996553e4e2da560bcec2b021c554dc242ac585f62b55dc7ca1b4c99a0453ad6556f490edd21d1d89ce8ff61f280c2c7c61b595eb4f2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Forge Apps, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Readme.md ADDED
@@ -0,0 +1,35 @@
1
+ # transloadit_fetcher
2
+ If you use [Transloadit](http://transloadit.com) and their Assembly Notifications (webhooks) then Transloadit Fetcher can help make your local development far easier.
3
+
4
+ In order to simulate the notification process you must visit your Transloadit dashboard, view your assemblies, find the one you care about, view the JSON that it outputs, and then manually POST that to your local development server. Transloadit-Fetcher automates this process.
5
+
6
+ It checks for new assemblies every minute, only fetching ones created since it last found any assemblies. It pulls these down, and performs a POST to a URL of your choosing that emulates the Transloadit assembly notification.
7
+
8
+ # Installation
9
+
10
+ In your gemfile:
11
+
12
+ `gem 'transloadit_fetcher', group :development`
13
+
14
+ In your terminal:
15
+
16
+ `bundle install`
17
+
18
+
19
+ # Usage
20
+ Find your API Key and Secret here: https://transloadit.com/accounts/credentials
21
+
22
+ `bundle exec transloadit_fetcher -l my_api_key my_api_secret http://127.0.0.1:3000`
23
+
24
+ The `-l` tells it to loop and check every 1 minute.
25
+
26
+
27
+ If you'd like you may run `bundle install --binstubs` and then run transloadit_fetcher with
28
+
29
+ `bin/transloadit_fetcher -l my_api_key my_api_secret http://127.0.0.1:3000`
30
+
31
+ #### ToDo:
32
+
33
+ * Improve handling of first fetch. It probably should just fetch all assemblies that were created in the last minute
34
+ * Add some better output depending on the response code of the HTTP POST.
35
+ * General Code cleanup
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/transloadit_fetcher.rb')
3
+ require 'trollop'
4
+
5
+ opts = Trollop::options do
6
+ opt :loop, "Run continuously", default: false
7
+ end
8
+
9
+ if ARGV[0].nil? || ARGV[1].nil? || ARGV[2].nil?
10
+ puts <<-EOS
11
+ Error: Missing parameters
12
+
13
+ Usage:
14
+
15
+ transloadit_fetcher [options] <api_key> <api_secret> <post_url>
16
+
17
+ EOS
18
+ exit 1
19
+ end
20
+
21
+ begin
22
+ @tf = TransloaditFetcher.new(ARGV[0], ARGV[1], ARGV[2])
23
+ if opts[:loop]
24
+ loop do
25
+ puts "Checking for new assemblies"
26
+ @since = @tf.fetch_and_post_assemblies(@since)
27
+ sleep 60
28
+ end
29
+ else
30
+ @tf.fetch_and_post_assemblies(@since)
31
+ end
32
+ rescue SystemExit, Interrupt
33
+ puts "Quitting..."
34
+ exit 0
35
+ rescue TransloaditFetcherError
36
+ exit 1
37
+ rescue Exception => e
38
+ puts e.class
39
+ puts e.message
40
+ puts e.backtrace
41
+ exit 1
42
+ end
@@ -0,0 +1,99 @@
1
+ require 'net/http'
2
+ require "rubygems"
3
+ require "transloadit"
4
+ require "time"
5
+
6
+ class TransloaditFetcher
7
+
8
+ def initialize(auth_key, auth_secret, post_url = nil)
9
+ @auth_key = auth_key
10
+ @auth_secret = auth_secret
11
+ @post_url = post_url
12
+ end
13
+
14
+ def fetch_and_post_assemblies(since = nil)
15
+ if since
16
+ page_size = 50
17
+ else
18
+ page_size = 2
19
+ end
20
+ assemblies = request_assembly_list( {since: since, page_size: page_size} )
21
+
22
+ latest_time = nil
23
+
24
+ assemblies.each do |assembly|
25
+ created_time = Time.parse(assembly["created"])
26
+ if latest_time.nil? || created_time > latest_time
27
+ latest_time = created_time + 1
28
+ end
29
+
30
+ assembly_response = request_assembly assembly
31
+ if @post_url
32
+ post_assembly_response assembly_response
33
+ end
34
+ end
35
+ return latest_time
36
+ end
37
+
38
+ def request_assembly_list(options={})
39
+
40
+ request_url = "http://api2.transloadit.com/assemblies"
41
+
42
+ params = {
43
+ page: 1,
44
+ pagesize: options[:page_size] || 50,
45
+ type: "completed",
46
+ auth: {
47
+ key: @auth_key,
48
+ expires: (Time.now.utc + 60).strftime('%Y/%m/%d %H:%M:%S+00:00')
49
+ }
50
+ }
51
+
52
+ if options[:since]
53
+ params[:fromdate] = options[:since].utc.strftime('%Y-%m-%d %H:%M:%S')
54
+ end
55
+
56
+ transloadit_request = Transloadit::Request.new request_url, @auth_secret
57
+
58
+ response = transloadit_request.get(params)
59
+ if response["error"]
60
+ puts "Error fetching assembly list:"
61
+ puts response["error"]
62
+ puts response["message"]
63
+ raise TransloaditFetcherError
64
+ else
65
+ puts "Successfully fetched #{response["items"].count} assemblies"
66
+ return response["items"]
67
+ end
68
+ end
69
+
70
+ def request_assembly(assembly)
71
+ request_url = "http://api2.transloadit.com/assemblies/#{assembly["id"]}"
72
+
73
+ params = {
74
+ :auth => {
75
+ :key => @auth_key,
76
+ :expires => (Time.now.utc + 60).strftime('%Y/%m/%d %H:%M:%S+00:00')
77
+ }
78
+ }
79
+
80
+ transloadit_request = Transloadit::Request.new request_url, @auth_secret
81
+ response = transloadit_request.get(params)
82
+ if response["error"]
83
+ puts "Error fetching assembly #{assembly["id"]}:"
84
+ puts response["error"]
85
+ puts response["message"]
86
+ raise TransloaditFetcherError
87
+ else
88
+ puts "successfully fetched #{assembly["id"]}"
89
+ return response
90
+ end
91
+ end
92
+
93
+ def post_assembly_response(assembly_response)
94
+ uri = URI(@post_url)
95
+ res = Net::HTTP.post_form(uri, 'transloadit' => assembly_response.to_s)
96
+ puts "Posted assembly to #{@post_url}"
97
+ end
98
+ end
99
+ class TransloaditFetcherError < StandardError; end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transloadit_fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben McFadden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: transloadit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: trollop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: If you're using Transloadit with the callback URL options, this gem will
42
+ fetch assemblies from Transloadit periodically and post them to your local environment
43
+ allowing for substantially easier development.
44
+ email: ben@forgeapps.com
45
+ executables:
46
+ - transloadit_fetcher
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - LICENSE
51
+ - Readme.md
52
+ - bin/transloadit_fetcher
53
+ - lib/transloadit_fetcher.rb
54
+ homepage: https://github.com/ForgeApps/Transloadit_Fetcher
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Simplifies the process of integrating with Transloadit for local development
78
+ test_files: []