Class: Fudok::Client

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

Overview

Fudok client

Author

cchantep

Class Method Summary collapse

Class Method Details

._prepared_http(ssl) ⇒ Object

(For internal use) Returns a prepared HTTP client.

  • ssl (boolean): Whether should use SSL (true) or not (false)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fudok.rb', line 63

def self._prepared_http(ssl)
  if ssl == true
    require 'openssl'
  end

  scheme = (ssl == true) ? "https" : "http"
  
  # Prepare HTTP client for Fudok API
  uri = URI.parse(scheme + "://go.fudok.com/api/merge")
  http = Net::HTTP.new(uri.host, uri.port)

  if ssl == true
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  return http
end

.auth(email, password, ssl) ⇒ Object

Authenticates as Fudok manager, and returns an admin token (string) (not an application token, used for features such as merge). Arguments are the following.

  • email (string): Fudok user email (username)

  • password (string): Password of specified user (clear text)

  • ssl (boolean): Whether should use SSL (true; recommanded) or not (false)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fudok.rb', line 45

def self.auth(email, password, ssl)
  http = self._prepared_http(ssl)

  # Prepare parameters
  params = { "email" => email, "password" => password }

  # Prepare request to merge feature
  req = Net::HTTP::Post.new("/api/auth")
  req.set_form_data(params)

  res = http.request(req).body
  obj = JSON.parse(res)

  return obj["token"]
end

.merge(token, template, values, ssl, &block) ⇒ Object

Merges values with specified the specified Fudok template. Arguments are the following.

  • token (string): Fudok application token

  • template (string): ID of Fudok template

  • values (hash): Map of one string value per area name

  • ssl (boolean): Whether should use SSL (true) or not (false)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fudok.rb', line 17

def self.merge(token, template, values, ssl, &block)
  http = self._prepared_http(ssl)
  
  # Prepare parameters
  params = {
    "fudok_token" => token,
    "fudok_template" => template
  }

  values.each {|key, value| params[key] = value}

  # Prepare request to merge feature
  req = Net::HTTP::Post.new("/api/merge")
  req.set_form_data(params)

  # Send request, process response (there save it as file)
  http.request req do |response|
    yield response
  end
end