smelter 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9365372b5d0b36b5e219c4d6585ea1b3d95ae74
4
+ data.tar.gz: 9bef443700491ec4e2c9e4be8f091b1bfb9796a2
5
+ SHA512:
6
+ metadata.gz: 54818a4ebce7782d880ddfbe72819e5857c56a31a8291351d6abb07c57629e76320e3e36c3a680d8c35db1eb98ae650262dd8849cdef5fe41e74d2db003a2706
7
+ data.tar.gz: 8c90fc55b6e7658b0d638d7a0bea14d70360ca2ef72dcb886896a20c31d683e3f174875f384f84f8ad49093d5b36e041a4cd523fbd1511bae31d7b059d66cef9
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smelter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jon Stokes
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,135 @@
1
+ # Smelter
2
+
3
+ Smelter is a small gem that has a pretty specific use case. You can store
4
+ scripts and extensions in a data store (redis, postgres, or anything else)
5
+ and then use that stored code to dynamically mutate a shared context.
6
+
7
+ All of the caveats around `context_eval`ing some code from a data store apply.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'smelter'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install smelter
24
+
25
+ ## Usage
26
+
27
+ The classes in `spec/support/test_classes.rb` and the specs will tell you all you
28
+ need to know about how this works. But here's an overview, with code from the specs.
29
+
30
+ Consider the following extension and script:
31
+
32
+ ```ruby
33
+ Test::Extension.define "test/my_extension" do
34
+ extension do
35
+ def subtract(a, b)
36
+ a - b
37
+ end
38
+ end
39
+ end
40
+
41
+ Test::Script.define "test/my_script" do
42
+ extensions 'test/*'
43
+
44
+ script do
45
+ number do |context|
46
+ context['number'] * 10
47
+ end
48
+
49
+ subtraction_result do |context|
50
+ subtract(context['number'], 1)
51
+ end
52
+ end
53
+ end
54
+ ```
55
+
56
+ Now imagine that the extension and script above are stored in some data store,
57
+ like redis or postgres. You then can do something like the following:
58
+
59
+ ```ruby
60
+ # Find the script by name in the database, and get a ScriptRunner
61
+ # object that has the script's extensions defined on it.
62
+ runner = Test::Script.runner("test/my_script")
63
+
64
+ # Prepare a context Hash (or Struct) that the script can mutate
65
+ # by running the Procs associated with each context attribute from
66
+ # top to bottom.
67
+ context = { 'number' => 5 }
68
+
69
+ # Mutate the context by running the script on it.
70
+ runner.run(context)
71
+
72
+ # Results!
73
+ context
74
+ #=> { 'number' => 50, 'subtraction_result' => 49 }
75
+ ```
76
+
77
+ Like I said, a pretty specific use case.
78
+
79
+ Stretched.io uses uses this code to prepare JSON objects from web pages
80
+ by extracting different attributes using [Nokogiri](http://nokogiri.org) and the [Buzzsaw DSL](http://github.com/jonstokes/buzzsaw.git).
81
+
82
+ Here's some actual code that I use with Stretched:
83
+
84
+ ```ruby
85
+ Stretched::Script.define "globals/scripts/product_page" do
86
+ extensions 'globals/extensions/*'
87
+
88
+ script do
89
+ title do |context|
90
+ next unless context['title'].present?
91
+ context['title'].squeeze(' ')
92
+ end
93
+
94
+ availability do |context|
95
+ if %w(AuctionListing ClassifiedListing).include?(context['type'])
96
+ "in_stock"
97
+ else
98
+ context['availability'].try(:downcase)
99
+ end
100
+ end
101
+
102
+ image do |context|
103
+ if context['image']
104
+ clean_up_image_url(context['image'])
105
+ end
106
+ end
107
+
108
+ price_in_cents do |context|
109
+ convert_dollars_to_cents(context['price_in_cents'])
110
+ end
111
+ end
112
+ end
113
+ ```
114
+ For the above, I populate the `context` hash with values extracted from a product
115
+ web page using [buzzsaw](http://github.com/jonstokes/buzzsaw.git) and [nokogiri](http://nokogiri.org). Then I go back over with the script and
116
+ clean up the data in the context hash, which eventually becomes a JSON object
117
+ that the platform enqueues for a user to be able to get the results of a web scrape.
118
+
119
+ The methods `convert_dollars_to_cents` and `clean_up_image_url` are defined in
120
+ one of the extensions that the script loads under `globals/extensions/*`. Notice
121
+ that the gem uses globbing to find extensions in the total namespace of extensions.
122
+
123
+ ## Development
124
+
125
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
126
+
127
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
128
+
129
+ ## Contributing
130
+
131
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smelter.
132
+
133
+ ## License
134
+
135
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "smelter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/smelter.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "thread_safe"
2
+ require "smelter/version"
3
+ require "smelter/scriptable"
4
+ require "smelter/extendable"
5
+ require "smelter/definition_proxy"
6
+ require "smelter/script_runner"
7
+
8
+ module Smelter
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,53 @@
1
+ module Smelter
2
+ class DefinitionProxy
3
+ attr_reader :script_name
4
+ attr_reader :extensions
5
+
6
+ def initialize(script_name)
7
+ @script_name = script_name
8
+ end
9
+
10
+ def extensions(glob)
11
+ @extensions = glob
12
+ end
13
+
14
+ def script(&block)
15
+ runner = ScriptRunner.new
16
+
17
+ # Define all locally registered extensions on this runner instance
18
+ self.class.extension_class.registry.each_pair do |extension_name, block|
19
+ next unless matches_extensions_glob?(extension_name.to_s)
20
+ runner.instance_eval(&block)
21
+ end
22
+
23
+ # Set up runner instance for use
24
+ if block_given?
25
+ runner.instance_eval(&block)
26
+ end
27
+
28
+ runner
29
+ end
30
+
31
+ def extension(&block)
32
+ self.class.extension_class.register(script_name, &block)
33
+ end
34
+
35
+ def self.extension_class=(val)
36
+ @@extension_class = val
37
+ end
38
+
39
+ def self.extension_class
40
+ @@extension_class
41
+ end
42
+
43
+ private
44
+
45
+ def matches_extensions_glob?(extension_name)
46
+ if @extensions.is_a?(String)
47
+ File.fnmatch?(@extensions, extension_name)
48
+ else
49
+ @extensions.select { |ext| File.fnmatch?(ext, extension_name) }.any?
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,49 @@
1
+ module Smelter
2
+ module Extendable
3
+
4
+ # Extendable classes must support the following methods
5
+ # def self.all_names
6
+ # In an ActiveRecord model this could be just pluck(:name)
7
+ # end
8
+ #
9
+ # def name
10
+ # returns the name of the script
11
+ # end
12
+ #
13
+ # def source
14
+ # returns the source file for the script
15
+ # end
16
+
17
+ def self.included(base)
18
+ Smelter::DefinitionProxy.extension_class = base
19
+
20
+ base.class_eval do
21
+ extend ClassMethods
22
+ end
23
+ end
24
+
25
+ def register
26
+ instance_eval source, name, 1
27
+ end
28
+
29
+ module ClassMethods
30
+ def register_all
31
+ self.all_names.each do |name|
32
+ name = name.to_s
33
+ next if registry[name]
34
+ extension = find_by_name(name)
35
+ extension.register
36
+ end
37
+ end
38
+
39
+ def registry
40
+ @registry ||= ThreadSafe::Cache.new
41
+ end
42
+
43
+ def register(extension_name, &block)
44
+ @registry ||= ThreadSafe::Cache.new
45
+ @registry[extension_name.to_s] = block
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,47 @@
1
+ module Smelter
2
+ class ScriptRunner
3
+
4
+ attr_reader :attributes, :context
5
+ attr_accessor :user
6
+
7
+ def initialize
8
+ @attributes = {}
9
+ end
10
+
11
+ def set_context(context)
12
+ @context = context
13
+ attrs = @context.is_a?(Hash) ? @context.keys : @context.members
14
+ attrs.each do |attr|
15
+ self.define_singleton_method(attr) do
16
+ @context[attr]
17
+ end
18
+ end
19
+ end
20
+
21
+ def run(instance={})
22
+ attributes.each do |attribute_name, value|
23
+ result = value.is_a?(Proc) ? value.call(instance) : value
24
+ instance[attribute_name.to_s] = result
25
+ end
26
+ instance
27
+ end
28
+
29
+ def load_registration(opts)
30
+ opts[:klass].find_by(name: opts[:name])
31
+ end
32
+
33
+ def method_missing(name, *args, &block)
34
+ if block_given?
35
+ attributes[name.to_s] = block
36
+ else
37
+ attributes[name.to_s] = args[0]
38
+ end
39
+ rescue RuntimeError => e
40
+ if !!e.message[/add a new key into hash during iteration/]
41
+ super
42
+ else
43
+ raise e
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,53 @@
1
+ module Smelter
2
+ module Scriptable
3
+
4
+ # Scriptable classes must support the following methods
5
+ # class Script
6
+ # include Smelter::Scriptable
7
+ #
8
+ # runner_include Buzzsaw::DSL
9
+ #
10
+ # def self.find_by_name(name)
11
+ # returns a script object
12
+ # end
13
+ #
14
+ # def name
15
+ # returns the name of the script
16
+ # end
17
+ #
18
+ # def source
19
+ # returns the source file for the script
20
+ # end
21
+ # end
22
+
23
+ def self.included(base)
24
+ base.class_eval do
25
+ extend ClassMethods
26
+ end
27
+ end
28
+
29
+ def register
30
+ # NOTE: This returns a populated instance of ScriptRunner
31
+ # that has all extensions defined on it and contains
32
+ # Procs for the code defined in source
33
+ instance_eval source, name, 1
34
+ end
35
+
36
+ module ClassMethods
37
+ def runner_include(mod)
38
+ Smelter::ScriptRunner.include(mod)
39
+ end
40
+
41
+ def runner(name=nil)
42
+ return ScriptRunner.new unless name
43
+ script = find_by_name(name)
44
+ script.register
45
+ end
46
+
47
+ def define(name, &block)
48
+ definition_proxy = DefinitionProxy.new(name)
49
+ definition_proxy.instance_eval(&block)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Smelter
2
+ VERSION = "0.1.0"
3
+ end
data/smelter.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smelter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "smelter"
8
+ spec.version = Smelter::VERSION
9
+ spec.authors = ["Jon Stokes"]
10
+ spec.email = ["jon@jonstokes.com"]
11
+
12
+ spec.summary = %q{This is the code that stretched.io uses for dynamic scripting. As with anything that calls instance_eval on code from a data store, use cautiously.}
13
+ spec.homepage = "http://github.com/jonstokes/smelter.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thread_safe"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "redis-objects"
27
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smelter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Stokes
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thread_safe
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis-objects
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - jon@jonstokes.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/smelter.rb
99
+ - lib/smelter/definition_proxy.rb
100
+ - lib/smelter/extendable.rb
101
+ - lib/smelter/script_runner.rb
102
+ - lib/smelter/scriptable.rb
103
+ - lib/smelter/version.rb
104
+ - smelter.gemspec
105
+ homepage: http://github.com/jonstokes/smelter.git
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: This is the code that stretched.io uses for dynamic scripting. As with anything
129
+ that calls instance_eval on code from a data store, use cautiously.
130
+ test_files: []