Class: ExUA::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ex_ua/client.rb

Overview

Client for ExUA

Examples:

Usage

client = ExUA::Client.new
categories = client.base_categories('ru')

Constant Summary collapse

KNOWN_BASE_CATEGORIES =
%w[video audio images texts games software]

Instance Method Summary collapse

Instance Method Details

#available_languagesArray<String>

List of available languages

Returns:

  • (Array<String>)

34
35
36
# File 'lib/ex_ua/client.rb', line 34

def available_languages
  @available_langauges ||= get('/').search('select[name=lang] option').inject({}){|acc,el| acc[el.attributes["value"].value]=el.text;acc}
end

#base_categories(lang) ⇒ Array<ExUA::Category>

List of base categories for a given language @param lang Language

Examples:

Usage

client.base_categories('ru')

Returns:


43
44
45
# File 'lib/ex_ua/client.rb', line 43

def base_categories(lang)
  base_categories_names.map{|cat| Category.new(url: "/#{lang}/#{cat}")}
end

#get(path) ⇒ Object


63
64
65
# File 'lib/ex_ua/client.rb', line 63

def get(path)
  Nokogiri.parse(HTTParty.get(Addressable::URI.join(ExUA::BASE_URL,path).to_s).body)
end

#search(text, page = 0, per = 20) ⇒ Object

Search for a given @text @param text A term to search for @param page Page number, starting from 0 @param per Items per page. Defaults to 20 @


53
54
55
56
57
58
59
60
61
# File 'lib/ex_ua/client.rb', line 53

def search(text, page=0, per=20)
  uri = Addressable::URI.parse("/search?#{Addressable::URI.form_encode(s: text, p: page, per: per)}")
  page = get(uri)
  page.search('table.panel tr td').map do |s|
    s.search('a')[1]
  end.compact.map do |link|
    ExUA::Category.new(url: link.attributes['href'], name: link.text)
  end
end