Class: LbcApi::Classified

Inherits:
Object
  • Object
show all
Defined in:
lib/lbc_api/classified.rb

Class Method Summary collapse

Class Method Details

.activate(body) ⇒ Object



47
48
49
# File 'lib/lbc_api/classified.rb', line 47

def activate(body)
  Agent.mecha.get(URI.extract(body, 'http').first)
end

.create(email, password, data, files = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lbc_api/classified.rb', line 4

def create(email, password, data, files = nil)
  agent = Agent.mecha
  page = agent.get('http://www2.leboncoin.fr/ai')
  sleep 3
  data = Sanitize.params(data).merge({'email' => email})
  if files && files.count
    files.each_with_index do |img, i|
      page = page.form_with(name: 'formular') do |f|
        f.file_uploads[i].file_name = img
        f['extra_images'] = 1
      end.submit
      agent.get(page.search('.photo.waiting').at('span')['title'])
      sleep 3
    end
  end
  page = page.form_with(name: 'formular') do |f|
    data.each do |k, v|
      f[k] = v
    end
  end.submit
  sleep 3
  page.form_with(name: 'formular') do |f|
    f['passwd'] = password
    f['passwd_ver'] = password
  end.submit
  sleep 3
  agent.shutdown
end

.delete(id, password) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/lbc_api/classified.rb', line 33

def delete(id, password)
  page = Agent.mecha.get("http://www2.leboncoin.fr/ai?id=#{id}&cmd=delete")
  page = page.forms[page.forms.count - 2].submit
  page = page.form_with(action: '/ai/delete/0') do |f|
    f['passwd'] = password
  end.submit
  !!page.at('h1:contains("Votre annonce sera supprimée")')
end

.exists?(title) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/lbc_api/classified.rb', line 42

def exists?(title)
  page = Agent.mecha.get("http://www.leboncoin.fr/annonces/?f=a&th=1&q=#{CGI::escape(title)}&it=1")
  !page.search('.content-border').at("h2:contains('Aucune annonce trouvée!')")
end

.get_id(body) ⇒ Object



51
52
53
# File 'lib/lbc_api/classified.rb', line 51

def get_id(body)
  URI.extract(body, 'http').first.scan(/\d/).join('')
end