Class: Openfoodfacts::User

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/openfoodfacts/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.login(user_id, password, locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN) ⇒ Object

Login WARNING SECURITY: It is not made throught HTTPS Secure so password can be read on the way.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/openfoodfacts/user.rb', line 11

def (user_id, password, locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
  path = 'cgi/session.pl'
  uri = URI("http://#{locale}.#{domain}/#{path}")
  params = {
    "jqm" => "1",
    "user_id" => user_id,
    "password" => password
  }

  response = Net::HTTP.post_form(uri, params)
  data = JSON.parse(response.body)

  if data['user_id']
    data.merge!(password: password)
    new(data)
  end
end

Instance Method Details

#login(locale: DEFAULT_LOCALE) ⇒ Object

Login


33
34
35
36
37
38
# File 'lib/openfoodfacts/user.rb', line 33

def (locale: DEFAULT_LOCALE)
  if user = self.class.(self.user_id, self.password, locale: locale)
    self.name = user.name
    self
  end
end