class Abrupt::Service::Base

base class

Constants

SERVICE_URI

TODO: outsource service uri to module Service

Attributes

abbr[RW]
options[RW]
response[RW]
url[RW]

Public Class Methods

available_options() click to toggle source
# File lib/abrupt/service/base.rb, line 29
def self.available_options
  []
end
keyname() click to toggle source
# File lib/abrupt/service/base.rb, line 33
def self.keyname
  name.split('::').last.downcase
end
new(html, options = {}) click to toggle source
# File lib/abrupt/service/base.rb, line 15
def initialize(html, options = {})
  @html = html
  @options = options
  query_params = if @options.count > 0
                   options_arr = @options.map { |k, v| "#{k}=#{v}" }
                   '?' + options_arr.reduce { |a, e| "#{a}&#{e}" }
                 else
                   ''
                 end
  @url = service_uri + query_params
  @abbr = self.class.name[0].downcase
  @options = []
end
transform_hash(hsh) click to toggle source
# File lib/abrupt/service/base.rb, line 56
def self.transform_hash(hsh)
  uri = Addressable::URI.parse(hsh.keys.first).normalize
  result = {
      website: {
          domain: "#{uri.scheme}://#{uri.host}",
          url: []
      }
  }
  hsh.each_with_index do |(key, value), _index|
    page = {
        name: key,
        state: value
    }
    result[:website][:url] << page
  end
  result.deep_symbolize_keys
end

Public Instance Methods

execute() click to toggle source

TODO: naming of interface execute

# File lib/abrupt/service/base.rb, line 38
def execute
  options = {
      method: :post,
      timeout: 6000,
      open_timeout: 6000,
      accept: :schema
  }
  options.merge!(url: @url, payload: @html)
  begin
    res = RestClient::Request.execute(options).to_str
    @response = JSON.parse(res)
  rescue => e
    puts "some problems with #{@url}"
    puts e
    nil
  end
end
service_uri() click to toggle source
# File lib/abrupt/service/base.rb, line 11
def service_uri
  SERVICE_URI
end