Module: SAPOCI::Connect

Defined in:
lib/sapoci/connect.rb,
lib/sapoci/connect/middleware/pass_cookies.rb,
lib/sapoci/connect/middleware/follow_redirects.rb,
lib/sapoci/connect/middleware/background_search.rb

Defined Under Namespace

Modules: Middleware

Class Method Summary collapse

Class Method Details

.search(method, connection, keywords, hook_url, extra_params = nil) ⇒ Object

Perform an OCI background search.

If you need to follow redirects and pass cookies along, you should initialize and use Faraday with this pattern:

conn = Faraday.new("http://shop.com/path", :params => {"optional" => "value"}) do |builder| 
  builder.use SAPOCI::Connect::Middleware::FollowRedirects
  builder.use SAPOCI::Connect::Middleware::PassCookies
  builder.use SAPOCI::Connect::Middleware::BackgroundSearch
  builder.adapter :net_http
end
conn.options[:timeout] = 3
conn.options[:open_timeout] = 5
resp = SAPOCI::Connect.search(:get, conn, "toner", "http://return.to/me")
puts resp.status # => 200
puts resp.body   # => <SAPOCI::Document>


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sapoci/connect.rb', line 27

def self.search(method, connection, keywords, hook_url, extra_params = nil)
  params = {
    "FUNCTION" => "BACKGROUND_SEARCH",
    "SEARCHSTRING" => keywords,
    "HOOK_URL" => hook_url
  }
  params.update(extra_params) if extra_params

  unless connection.builder.handlers.include?(SAPOCI::Connect::Middleware::BackgroundSearch)
    connection.use SAPOCI::Connect::Middleware::BackgroundSearch
  end

  case method.to_sym
  when :get
    connection.get do |req|
      req.params = params
    end
  when :post
    connection.post do |req|
      req.body = Faraday::Utils.build_nested_query params
    end
  else
    raise "SAPOCI::Connect.search only allows :get or :post requests"
  end
end