class Stockboy::Providers::HTTP

Fetches data from an HTTP endpoint

Job template DSL

provider :http do
  get "http://example.com/api/things"
end

Public Class Methods

new(opts={}, &block) click to toggle source

Initialize an HTTP provider

Calls superclass method Stockboy::Provider::new
# File lib/stockboy/providers/http.rb, line 127
def initialize(opts={}, &block)
  super(opts, &block)
  self.uri      = opts[:uri]
  self.method   = opts[:method] || :get
  self.query    = opts[:query]  || Hash.new
  self.headers  = opts[:headers]  || Hash.new
  self.body     = opts[:body]
  self.username = opts[:username]
  self.password = opts[:password]
  DSL.new(self).instance_eval(&block) if block_given?
end

Public Instance Methods

client() { |req| ... } click to toggle source
# File lib/stockboy/providers/http.rb, line 139
def client
  orig_logger, HTTPI.logger = HTTPI.logger, logger
  req = HTTPI::Request.new.tap { |c| c.url = uri }
  req.auth.basic(username, password) if username && password
  req.headers = headers
  req.body = body
  block_given? ? yield(req) : req
ensure
  HTTPI.logger = orig_logger
end
get=(uri) click to toggle source
# File lib/stockboy/providers/http.rb, line 113
def get=(uri)
  @method = :get
  @uri = uri
end
method=(http_method) click to toggle source
# File lib/stockboy/providers/http.rb, line 108
def method=(http_method)
  return @method = nil unless %w(get post).include? http_method.to_s.downcase
  @method = http_method.to_s.downcase.to_sym
end
post=(uri) click to toggle source
# File lib/stockboy/providers/http.rb, line 118
def post=(uri)
  @method = :post
  @uri = uri
end
uri() click to toggle source
# File lib/stockboy/providers/http.rb, line 81
def uri
  return nil if @uri.nil? || @uri.to_s.empty?
  URI(@uri).tap { |u| u.query = URI.encode_www_form(@query) if @query }
end
uri=(uri) click to toggle source
# File lib/stockboy/providers/http.rb, line 86
def uri=(uri)
  @uri = uri
end

Private Instance Methods

fetch_data() click to toggle source
# File lib/stockboy/providers/http.rb, line 159
def fetch_data
  client do |request|
    response = HTTPI.request(method, request)
    if response.error?
      errors << "HTTP response error: #{response.code}"
    else
      @data = response.body
    end
  end
end
validate() click to toggle source
# File lib/stockboy/providers/http.rb, line 152
def validate
  errors << "uri must be specified" unless uri
  errors << "method (:get, :post) must be specified" unless method
  errors << "body must be specified" if [:post, :put, :patch].include?(method) && body.to_s.empty?
  errors.empty?
end