Class: Spore

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {} )

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,options = {})
  # Don't load gems that are not needed
  # Only When it requires json, then json is loaded
  parser = self.class.load_parser(spec, options)
  @config = options[: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

#authorObject

Returns the value of attribute author.



21
22
23
# File 'lib/spore.rb', line 21

def author
  @author
end

#base_urlObject

Returns the value of attribute base_url.



22
23
24
# File 'lib/spore.rb', line 22

def base_url
  @base_url
end

#configObject (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/spore.rb', line 25

def config
  @config
end

#formatObject

Returns the value of attribute format.



22
23
24
# File 'lib/spore.rb', line 22

def format
  @format
end

#methodsObject

Returns the value of attribute methods.



23
24
25
# File 'lib/spore.rb', line 23

def methods
  @methods
end

#middlewaresObject

Returns the value of attribute middlewares.



24
25
26
# File 'lib/spore.rb', line 24

def middlewares
  @middlewares
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/spore.rb', line 21

def name
  @name
end

#specsObject (readonly)

Returns the value of attribute specs.



25
26
27
# File 'lib/spore.rb', line 25

def specs
  @specs
end

#versionObject

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, options = {})

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, options = {})
  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 options.has_key?(:require)
      require options[:require]
      if options.has_key?(:parser)
        Object.const_get(options[:parser])
      else
        Object.const_get(options[: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