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_size, #data_time, #errors, #logger

Instance Method Summary collapse

Methods inherited from Stockboy::Provider

#clear, #data?, #inspect, #reload, #valid?

Constructor Details

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

Initialize an HTTP provider



127
128
129
130
131
132
133
134
135
136
137
# 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

Instance Attribute Details

#bodyString

HTTP request body

Examples:

body "<getData></getData>"

Returns:

  • (String)


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

dsl_attr :body

#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

#headersString

HTTP request headers

Examples:

headers content_type: "text/json"

Returns:

  • (String)


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

dsl_attr :headers

#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

#passwordString

Password for basic auth connection credentials

Examples:

password "424242"

Returns:

  • (String)


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

dsl_attr :password

#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)


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

dsl_attr :query

#uriString

HTTP host and path to the data resource

Examples:

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

Returns:

  • (String)


97
98
99
100
# File 'lib/stockboy/providers/http.rb', line 97

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

#usernameString

User name for basic auth connection credentials

Examples:

username "arthur"

Returns:

  • (String)


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

dsl_attr :username

Instance Method Details

#clientObject



139
140
141
142
143
144
145
146
147
148
# 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