Class: Stockboy::Providers::HTTP

Inherits:
Stockboy::Provider show all
Defined in:
lib/stockboy/providers/http.rb

Overview

Fetches data from an HTTP endpoint

Job template DSL

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

Options collapse

Attributes inherited from Stockboy::Provider

#data, #data_time, #errors, #logger

Instance Method Summary collapse

Methods inherited from Stockboy::Provider

#clear, #inspect, logger, #reload, #valid?

Constructor Details

#initialize(opts = {}, &block) ⇒ HTTP

Initialize an HTTP provider



91
92
93
94
95
96
97
# File 'lib/stockboy/providers/http.rb', line 91

def initialize(opts={}, &block)
  super(opts, &block)
  self.uri    = opts[:uri]
  self.method = opts[:method] || :get
  self.query  = opts[:query]  || Hash.new
  DSL.new(self).instance_eval(&block) if block_given?
end

Instance Attribute Details

#getString

Shorthand for :method and :uri using HTTP GET

Examples:

get 'http://example.com/api/things'

Returns:

  • (String)


25
# File 'lib/stockboy/providers/http.rb', line 25

dsl_attr :get, attr_writer: false

#methodSymbol

HTTP method: :get or :post

Examples:

method :post

Returns:

  • (Symbol)


43
# File 'lib/stockboy/providers/http.rb', line 43

dsl_attr :method, attr_writer: false

#postString

Shorthand for :method and :uri using HTTP POST

Examples:

post 'http://example.com/api/search'

Returns:

  • (String)


34
# File 'lib/stockboy/providers/http.rb', line 34

dsl_attr :post, attr_writer: false

#queryHash

Hash of query options

Examples:

query start: 1, limit: 100

Returns:

  • (Hash)


70
# File 'lib/stockboy/providers/http.rb', line 70

dsl_attr :query

#uriString

HTTP host and path to the data resource

Examples:

uri 'http://example.com/api/things'

Returns:

  • (String)


61
62
63
64
# File 'lib/stockboy/providers/http.rb', line 61

def uri
  return nil if @uri.nil? || @uri.empty?
  URI(@uri).tap { |u| u.query = URI.encode_www_form(@query) }
end

Instance Method Details

#client {|HTTPI| ... } ⇒ Object

Yields:

  • (HTTPI)


99
100
101
102
# File 'lib/stockboy/providers/http.rb', line 99

def client
  return HTTPI unless block_given?
  yield HTTPI
end