stilts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in stilts.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,103 @@
1
+ module Stilts
2
+ # Used to set up and modify settings for the notifier.
3
+ class Configuration
4
+
5
+ OPTIONS = [:api_key, :host, :http_open_timeout, :http_read_timeout, :project_root,
6
+ :port, :protocol, :proxy_host, :secure, :framework].freeze
7
+
8
+ # The API key for your project, found on the project edit form.
9
+ attr_accessor :api_key
10
+
11
+ # The host to connect to
12
+ attr_accessor :host
13
+
14
+ # The port on which your server runs (defaults to 443 for secure
15
+ # connections, 80 for insecure connections).
16
+ attr_accessor :port
17
+
18
+ # +true+ for https connections, +false+ for http connections.
19
+ attr_accessor :secure
20
+
21
+ # A list of parameters that should be filtered out of what is sent to Hoptoad.
22
+ # By default, all "password" attributes will have their contents replaced.
23
+ attr_reader :params_filters
24
+
25
+ # A list of filters for cleaning and pruning the backtrace. See #filter_backtrace.
26
+ attr_reader :backtrace_filters
27
+
28
+ # A list of filters for ignoring exceptions. See #ignore_by_filter.
29
+ attr_reader :ignore_by_filters
30
+
31
+ # The path to the project in which the error occurred, such as the RAILS_ROOT
32
+ attr_accessor :project_root
33
+
34
+ # The url of the notifier library being used to send notifications
35
+ attr_accessor :notifier_url
36
+
37
+ # The logger used by
38
+ attr_accessor :logger
39
+
40
+ # The framework is configured to use
41
+ attr_accessor :framework
42
+
43
+ alias_method :secure?, :secure
44
+
45
+ def initialize
46
+ @secure = false
47
+ @host = 'localhost'
48
+ @framework = 'Standalone'
49
+ end
50
+
51
+ # Allows config options to be read like a hash
52
+ #
53
+ # @param [Symbol] option Key for a given attribute
54
+ def [](option)
55
+ send(option)
56
+ end
57
+
58
+ def url
59
+ url = URI::HTTP.build({
60
+ :host => @host,
61
+ :scheme => protocol,
62
+ :path => "/",
63
+ :port => @port
64
+ })
65
+ end
66
+
67
+ # Returns a hash of all configurable options
68
+ def to_hash
69
+ OPTIONS.inject({}) do |hash, option|
70
+ hash.merge(option.to_sym => send(option))
71
+ end
72
+ end
73
+
74
+ # Returns a hash of all configurable options merged with +hash+
75
+ #
76
+ # @param [Hash] hash A set of configuration options that will take precedence over the defaults
77
+ def merge(hash)
78
+ to_hash.merge(hash)
79
+ end
80
+
81
+ def port
82
+ @port || default_port
83
+ end
84
+
85
+ def protocol
86
+ if secure?
87
+ 'https'
88
+ else
89
+ 'http'
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def default_port
96
+ if secure?
97
+ 443
98
+ else
99
+ 80
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,14 @@
1
+ # Simply grab the headers out of the request for use later
2
+ module Stilts
3
+ class Rack
4
+
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ Stilts.sender.user_agent = env["HTTP_USER_AGENT"]
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
Binary file
@@ -0,0 +1,37 @@
1
+ module Stilts
2
+ module Helpers
3
+
4
+ def resize(image_source)
5
+ b = Benchmark.measure do
6
+ @response = Stilts.sender.send_to_resizer({:image_source => image_source}.to_json.to_s)
7
+ end
8
+ logger.debug { "RESIZER BENCHMARK :: #{b}" }
9
+
10
+ image_url = url
11
+ image_url.path = ""
12
+ image_tag("#{image_url}#{@response['image_url']}")
13
+ end
14
+
15
+ def stream_resize(image_source)
16
+ b = Benchmark.measure do
17
+ @params = Addressable::URI.parse(url.to_s)
18
+ @params.query_values = {:image_source => image_source, :stream => "1"}
19
+ end
20
+ logger.debug { "STREAM BENCHMARK :: #{b}" }
21
+
22
+ stream_url = url
23
+ stream_url.path = "/resize"
24
+ stream_url.query = @params.query
25
+ stream_url.to_s
26
+ end
27
+
28
+ def url
29
+ Stilts.configuration.url.dup
30
+ end
31
+
32
+ def logger
33
+ Stilts.configuration.logger
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,26 @@
1
+ require 'stilts'
2
+ require 'rails'
3
+
4
+ module Stilts
5
+ class Railtie < Rails::Railtie
6
+ initializer "stilts.use_rack_middleware" do |app|
7
+ app.config.middleware.use "Stilts::Rack"
8
+ end
9
+
10
+ config.before_initialize do
11
+ Stilts.configure do |config|
12
+ config.host = "localhost"
13
+ config.port = 3000
14
+ config.logger ||= Rails.logger
15
+ config.framework = "Rails: #{::Rails::VERSION::STRING}"
16
+ end
17
+
18
+ if defined?(ActionView::Base)
19
+
20
+ require 'stilts/rails/helpers'
21
+ ActionView::Base.send :include, Stilts::Helpers
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,97 @@
1
+ module Stilts
2
+ class Sender
3
+ RESIZER_PATH = "/resize"
4
+ RESIZER_HEADERS = {
5
+ 'Content-Type' => 'application/json',
6
+ 'Accept' => 'application/json'
7
+ }
8
+
9
+ STREAMER_PATH = "/resize"
10
+ STREAMER_HEADERS = {
11
+ 'Content-Type' => 'image/jpeg',
12
+ 'Accept' => 'image/jpeg'
13
+ }
14
+ HTTP_ERRORS = [Timeout::Error,
15
+ Errno::EINVAL,
16
+ Errno::ECONNRESET,
17
+ EOFError,
18
+ Net::HTTPBadResponse,
19
+ Net::HTTPHeaderSyntaxError,
20
+ Net::ProtocolError,
21
+ Errno::ECONNREFUSED].freeze
22
+
23
+ attr_accessor :user_agent
24
+
25
+ def initialize(options = {})
26
+ [:protocol, :host, :port, :secure].each do |option|
27
+ instance_variable_set("@#{option}", options[option])
28
+ end
29
+ # , :http_open_timeout, :http_read_timeout
30
+ end
31
+
32
+ def send_to_resizer(data)
33
+ http =
34
+ Net::HTTP.new(url.host, url.port)
35
+
36
+ http.read_timeout = 5
37
+ http.open_timeout = 5
38
+ http.use_ssl = false
39
+ headers = RESIZER_HEADERS.merge({'User-Agent' => user_agent})
40
+
41
+ response = begin
42
+ logger.debug{"POSTED JSON: #{data} TO: #{url.to_s}" }
43
+ http.post(RESIZER_PATH, data, headers)
44
+ rescue *HTTP_ERRORS => e
45
+ log :error, "Timeout while contacting the server. #{e}"
46
+ nil
47
+ end
48
+
49
+ case response
50
+ when Net::HTTPSuccess then
51
+ log :info, "Success: #{response.class}", response
52
+ return JSON.parse(response.body)
53
+ else
54
+ log :error, "Failure: #{response.class}", response
55
+ end
56
+ end
57
+
58
+ def send_to_streamer(data)
59
+ http =
60
+ Net::HTTP.new(url.host, url.port)
61
+
62
+ http.read_timeout = 5
63
+ http.open_timeout = 5
64
+ http.use_ssl = false
65
+
66
+ response = begin
67
+ http.get(STREAMER_PATH+data, STREAMER_HEADERS)
68
+ rescue *HTTP_ERRORS => e
69
+ log :error, "Timeout while contacting the server. #{e}"
70
+ nil
71
+ end
72
+
73
+ case response
74
+ when Net::HTTPSuccess then
75
+ log :info, "Success: #{response.class}", response
76
+ return response.body
77
+ else
78
+ log :error, "Failure: #{response.class}", response
79
+ end
80
+ end
81
+
82
+ def url
83
+ Stilts.configuration.url.dup
84
+ end
85
+
86
+ def log(level, message, response = nil)
87
+ logger.send level, LOG_PREFIX + message if logger
88
+ Stilts.report_environment_info
89
+ Stilts.report_response_body(response.body) if response && response.respond_to?(:body)
90
+ end
91
+
92
+ def logger
93
+ Stilts.configuration.logger
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module Stilts
2
+ VERSION = "0.0.1"
3
+ end
data/lib/stilts.rb ADDED
@@ -0,0 +1,62 @@
1
+ module Stilts
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'addressable/uri'
5
+ # begin
6
+ # require 'active_support'
7
+ # rescue LoadError
8
+ # require 'activesupport'
9
+ # end
10
+
11
+ require 'stilts/configuration'
12
+ require 'stilts/sender'
13
+ require 'stilts/rack'
14
+ require 'stilts/railtie.rb' if defined?(Rails)
15
+
16
+
17
+
18
+ LOG_PREFIX = "[Stilts] "
19
+
20
+ class << self
21
+
22
+ # The sender object is responsible for delivering formatted data to the server.
23
+ # Must respond to #send_to_hoptoad. See Stilts::Sender.
24
+ attr_accessor :sender
25
+
26
+ # A configuration object. Must act like a hash and return sensible
27
+ # values for all configuration options. See Stilts::Configuration.
28
+ attr_accessor :configuration
29
+
30
+ def configure(silent = false)
31
+ self.configuration ||= Configuration.new
32
+ yield(configuration)
33
+ self.sender = Sender.new(configuration)
34
+ end
35
+
36
+ def logger
37
+ self.configuration.logger
38
+ end
39
+
40
+ # Prints out the environment info to the log for debugging help
41
+ def report_environment_info
42
+ write_verbose_log("Environment Info: #{environment_info}")
43
+ end
44
+
45
+ # Prints out the response body from Stilts for debugging help
46
+ def report_response_body(response)
47
+ write_verbose_log("Response from Server: \n#{response}")
48
+ end
49
+
50
+ # Returns the Ruby version, Rails version, and current Rails environment
51
+ def environment_info
52
+ info = "[Ruby: #{RUBY_VERSION}]"
53
+ info << " [#{configuration.framework}]"
54
+ end
55
+
56
+ # Writes out the given message to the #logger
57
+ def write_verbose_log(message)
58
+ logger.info LOG_PREFIX + message if logger
59
+ end
60
+
61
+ end
62
+ end
data/stilts.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stilts/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stilts"
7
+ s.version = Stilts::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Graham Powrie"]
10
+ s.email = ["graham@developmentnow.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{On-the-fly image resizing and management for mobile devices}
13
+ s.description = %q{}
14
+
15
+ s.rubyforge_project = "stilts"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('addressable', '~> 2.2.2')
23
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stilts
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Graham Powrie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-17 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: addressable
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 2
31
+ - 2
32
+ version: 2.2.2
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: ""
36
+ email:
37
+ - graham@developmentnow.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Rakefile
48
+ - lib/stilts.rb
49
+ - lib/stilts/configuration.rb
50
+ - lib/stilts/rack.rb
51
+ - lib/stilts/rails/.DS_Store
52
+ - lib/stilts/rails/helpers.rb
53
+ - lib/stilts/railtie.rb
54
+ - lib/stilts/sender.rb
55
+ - lib/stilts/version.rb
56
+ - stilts.gemspec
57
+ has_rdoc: true
58
+ homepage: ""
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: stilts
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: On-the-fly image resizing and management for mobile devices
89
+ test_files: []
90
+