Class: Abrupt::Service::Base

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

Overview

base class

Direct Known Subclasses

AbsoluteUrl, Complexity, Input, Link, Picture, Readability, Subject

Constant Summary collapse

SERVICE_URI =

TODO: outsource service uri to module Service

'http://wba.cs.hs-rm.de/AbRUPt/service/complexity/public/index.php/api/v1/complexity'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html, options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#abbrObject

Returns the value of attribute abbr.



7
8
9
# File 'lib/abrupt/service/base.rb', line 7

def abbr
  @abbr
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/abrupt/service/base.rb', line 7

def options
  @options
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/abrupt/service/base.rb', line 7

def response
  @response
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/abrupt/service/base.rb', line 7

def url
  @url
end

Class Method Details

.available_optionsObject



29
30
31
# File 'lib/abrupt/service/base.rb', line 29

def self.available_options
  []
end

.keynameObject



33
34
35
# File 'lib/abrupt/service/base.rb', line 33

def self.keyname
  name.split('::').last.downcase
end

.transform_hash(hsh) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

Instance Method Details

#executeObject

TODO: naming of interface execute



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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_uriObject



11
12
13
# File 'lib/abrupt/service/base.rb', line 11

def service_uri
  SERVICE_URI
end