Class: OdeskJobfetch

Inherits:
Object
  • Object
show all
Defined in:
lib/odesk_jobfetch.rb,
lib/odesk_jobfetch/version.rb

Constant Summary collapse

BASE_URL =
'https://www.odesk.com'
LOGIN_URL =
BASE_URL + '/login'
SEARCH_URL =
BASE_URL + '/jobs'
VERSION =
'0.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(proxy = {}) ⇒ OdeskJobfetch

Returns a new instance of OdeskJobfetch.



9
10
11
12
# File 'lib/odesk_jobfetch.rb', line 9

def initialize(proxy = {})
  @agent = Mechanize.new
  @agent.set_proxy(proxy[:server], proxy[:port]) unless proxy.empty?
end

Instance Method Details

#authorize(username, password) ⇒ Object



14
15
16
17
18
19
# File 'lib/odesk_jobfetch.rb', line 14

def authorize(username, password)
  page = @agent.get(LOGIN_URL)
  form = page.form_with(name: 'login')
  form.username, form.password = username, password
  form.submit
end

#fetch(params, simple_format = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/odesk_jobfetch.rb', line 21

def fetch(params, simple_format = false)
  page = @agent.get(
    SEARCH_URL, params, nil,
    { 'accept' => 'application/json', 'x-requested-with' => 'XMLHttpRequest' }
  )

  if page.response['content-type'] != 'application/json'
    fail 'Forgot to authorize?'
  end

  resp = JSON.parse(page.body)
  jobs = resp['jobs_raw_data']['jobs']
  simple_format ? simple_format(jobs) : jobs
end