Class: Spore
- Inherits:
-
Object
- Object
- Spore
- Defined in:
- lib/spore.rb,
lib/spore/version.rb,
lib/spore/middleware.rb,
lib/spore/spec_parser/json.rb,
lib/spore/spec_parser/yaml.rb,
lib/spore/middleware/format.rb,
lib/spore/middleware/runtime.rb
Overview
SPORE
Defined Under Namespace
Modules: SpecParser Classes: InvalidHeaders, Middleware, RequiredParameterExptected, UnexpectedResponse, UnsupportedSpec
Constant Summary collapse
- VERSION =
"0.0.9"
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#format ⇒ Object
Returns the value of attribute format.
-
#methods ⇒ Object
Returns the value of attribute methods.
-
#middlewares ⇒ Object
Returns the value of attribute middlewares.
-
#name ⇒ Object
Returns the value of attribute name.
-
#specs ⇒ Object
readonly
Returns the value of attribute specs.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.load_parser(spec, options = {}) ⇒ Object
:call-seq: load_parser(spec_file, options = {}).
Instance Method Summary collapse
-
#enable(middleware, args = {}) ⇒ Object
:call-seq: spore.enable(Spore::Middleware::SomeThing, :optionX => 42).
-
#enable_if(middleware, args = {}, &block) ⇒ Object
:call-seq: spore.enable_if(Spore::Middleware::SomeThing, :optionX => 42) do |env| { # return true or false, depending on env to say if the # middleware should be enabled or not for this request }.
-
#initialize(spec, options = {}) ⇒ Spore
constructor
Initialize a Spore instance with a specification file<br/> Optionally a file to require the parser from and the custom bound Parser class .
Constructor Details
#initialize(spec, options = {}) ⇒ Spore
Initialize a Spore instance with a specification file<br/> Optionally a file to require the parser from and the custom bound Parser class
:call-seq:
new(file_path, = {} )
Spore.new('/tmp/github.json')
or
Spore.new('/tmp/spec.dot', :require => 'my_custom_lib', :parser => 'DotParser')
DotParser must implement a class method load_file
class DotParser
def self.load_file(f)
str = ""
File.open(f) do |f|
str = ...
# Do what you have to here
end
end
end
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/spore.rb', line 64 def initialize(spec, = {}) # Don't load gems that are not needed # Only When it requires json, then json is loaded parser = self.class.load_parser(spec, ) @config = [:client_config] || {:ssl_verify_mode => OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT } specs = parser.load_file(spec) inititliaze_api_attrs(specs) construct_client_class(self.methods) self.middlewares = [] end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
21 22 23 |
# File 'lib/spore.rb', line 21 def @author end |
#base_url ⇒ Object
Returns the value of attribute base_url.
22 23 24 |
# File 'lib/spore.rb', line 22 def base_url @base_url end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
25 26 27 |
# File 'lib/spore.rb', line 25 def config @config end |
#format ⇒ Object
Returns the value of attribute format.
22 23 24 |
# File 'lib/spore.rb', line 22 def format @format end |
#methods ⇒ Object
Returns the value of attribute methods.
23 24 25 |
# File 'lib/spore.rb', line 23 def methods @methods end |
#middlewares ⇒ Object
Returns the value of attribute middlewares.
24 25 26 |
# File 'lib/spore.rb', line 24 def middlewares @middlewares end |
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/spore.rb', line 21 def name @name end |
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
25 26 27 |
# File 'lib/spore.rb', line 25 def specs @specs end |
#version ⇒ Object
Returns the value of attribute version.
22 23 24 |
# File 'lib/spore.rb', line 22 def version @version end |
Class Method Details
.load_parser(spec, options = {}) ⇒ Object
:call-seq:
load_parser(spec_file, = {})
This method takes two arguments spec_file and options<br/> If spec is a yml or json file options is skipped<br/> Else options is used for requiring and loading the correct parser<br/><br/> options is a Hash with :require and :parser keys.
-
:require is a file to require
-
:parser is a String to pass in Object.const_get
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/spore.rb', line 132 def self.load_parser(spec, = {}) case spec when /\.ya?ml/ require('spore/spec_parser/yaml') Spore::SpecParser::Yaml when /\.json/ require 'spore/spec_parser/json' Spore::SpecParser::Json else if .has_key?(:require) require [:require] if .has_key?(:parser) Object.const_get([:parser]) else Object.const_get([:require].to_s.capitalize) end else raise UnsupportedSpec, "don't know how to parse '#{spec}'" end end end |
Instance Method Details
#enable(middleware, args = {}) ⇒ Object
:call-seq:
spore.enable(Spore::Middleware::SomeThing, :optionX => 42)
This method takes a middleware class as its first argument. Options to pass to the middleware can be given as a second argument. Once this method is called, the Spore client will pass the request and the response objects to the middleware whenever appropriate. The order in which middlewares are enabled is respected by Spore. (same order for processing the request before it’s sent, reverse order for processing response, as described in the Spore specification).
92 93 94 95 96 97 98 |
# File 'lib/spore.rb', line 92 def enable(middleware, args={}) m = middleware.new(args) self.middlewares.push({ :condition => Proc.new { true }, :middleware => m }) end |
#enable_if(middleware, args = {}, &block) ⇒ Object
:call-seq:
spore.enable_if(Spore::Middleware::SomeThing, :optionX => 42) do |env| {
# return true or false, depending on env to say if the
# middleware should be enabled or not for this request
}
Same as the enable method, but is enabled dynamically, depending of the current request env. A block is given and is passed the env stack, it should return a boolean value to tell if the middleware should be enabled or not.
112 113 114 115 116 117 118 |
# File 'lib/spore.rb', line 112 def enable_if(middleware, args={}, &block) m = middleware.new(args) self.middlewares.push({ :condition => block, :middleware => m }) end |