Class: OTX::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/otx_ruby/base.rb

Overview

Base Class for API access, provides initial wrapper around Faraday performs base request

Direct Known Subclasses

Events, Pulses, Subscribed

Instance Method Summary collapse

Constructor Details

#initialize(key, server = "https://otx.alienvault.com") ⇒ Base

Initialise object

Parameters:

  • key (String)

    API Key for AlienVault OTX API

  • server (String) (defaults to: "https://otx.alienvault.com")

    Base URL for API, defaults to 'otx.alienvault.com'


13
14
15
16
17
18
19
20
21
# File 'lib/otx_ruby/base.rb', line 13

def initialize(key, server="https://otx.alienvault.com")
  @key = key
  @server = server

  @conn = Faraday.new(:url => @server) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Method Details

#get(url, params = {}) ⇒ Object

Get the provided URL

Parameters:

  • url (String)

    URL to make API request to

  • params (Hash) (defaults to: {})

    Additional parameters to be added to the requests


29
30
31
32
33
34
35
36
37
38
# File 'lib/otx_ruby/base.rb', line 29

def get(url, params={})
  response = @conn.get do |req|
    req.url url
    req.headers['X-OTX-API-KEY'] = @key
    req.params = params
  end

  # Parse and return JSON object as hash to caller
  return Oj.load(response.body)
end