Class: APDM::DesignElements::API

Inherits:
Object
  • Object
show all
Defined in:
lib/apdm/design_elements/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ API

Returns a new instance of API.



9
10
11
12
13
# File 'lib/apdm/design_elements/api.rb', line 9

def initialize(url, options = {})
  @url = url
  @api_key = options[:api_key]
  @version = DesignElements::VERSION
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/apdm/design_elements/api.rb', line 8

def api_key
  @api_key
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/apdm/design_elements/api.rb', line 8

def url
  @url
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/apdm/design_elements/api.rb', line 8

def version
  @version
end

Instance Method Details

#fetch_cssObject



25
26
27
28
29
30
31
# File 'lib/apdm/design_elements/api.rb', line 25

def fetch_css
  css = ''
  fetch_css_files.each do |href|
    css << resolve_css(href) if include_css?(href)
  end
  css.force_encoding(Encoding::UTF_8)
end

#fetch_css_filesObject



20
21
22
23
# File 'lib/apdm/design_elements/api.rb', line 20

def fetch_css_files
  path = '//head/link[@rel = "stylesheet"]/@href'
  Nokogiri::HTML(fetch_html("head")).xpath(path).map(&:to_s)
end

#fetch_html(element) ⇒ Object



15
16
17
18
# File 'lib/apdm/design_elements/api.rb', line 15

def fetch_html(element)
  html = Curl::Easy.http_get(endpoint(element)).body_str
  valid_fragment?(html) ? html.force_encoding(Encoding::UTF_8) : nil
end

#fetch_site_stat(category) ⇒ Object



33
34
35
# File 'lib/apdm/design_elements/api.rb', line 33

def fetch_site_stat(category)
  fetch_html("traffic&counterName=#{category}").force_encoding(Encoding::UTF_8)
end