Class: OX3APIClient

Inherits:
OAuth::Consumer
  • Object
show all
Defined in:
lib/helpers/ox3client.rb

Instance Method Summary collapse

Constructor Details

#initialize(email, password, site_url, consumer_key, consumer_secret, realm, version = 'v2', sso_domain = 'sso.openx.com', callback = 'oob', scheme = 'https', debug = false) ⇒ OX3APIClient

Returns a new instance of OX3APIClient.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/helpers/ox3client.rb', line 8

def initialize(email, password, site_url, consumer_key, consumer_secret, realm, 
  version='v2', sso_domain='sso.openx.com', callback='oob', scheme='https', debug=false)
  
  @version, @callback, @site, @debug = version, callback, site_url, debug
  @site = @site.end_with?('/') ? @site.chop : @site
  
  super(consumer_key, consumer_secret, {
    :http_method => :post,
    :scheme => :header,
    :oauth_version => "1.0",
    :signature_method => "HMAC-SHA1",
    :site => @site,
    :request_token_url => scheme + '://' + sso_domain + '/api/index/initiate',
    :access_token_url => scheme + '://' + sso_domain + '/api/index/token',
    :authorize_path => scheme + '://' + sso_domain + '/login/process',
    :realm => realm
  })
  
  # Step 1. Fetch temporary request token.
  fetch_request_token
  
  # Step 2. Log in to SSO server and authorize token.
  authorize_token(email, password)
  
  # Step 3. Swap temporary request token for permanent access token.
  fetch_access_token
  
  # Step 4. Validate your access token.
  validate_session
end

Instance Method Details

#delete(path) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/helpers/ox3client.rb', line 90

def delete(path)
  perform_request do |prefix, params|
    self.create_http_request(
      :delete, 
      prefix + path, 
      params
    )
  end
end

#get(path) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/helpers/ox3client.rb', line 52

def get(path)
  perform_request do |prefix, params|
    self.create_http_request(
      :get, 
      prefix + path, 
      params
    )
  end
end

#logoffObject



100
101
102
# File 'lib/helpers/ox3client.rb', line 100

def logoff
  get "/login/logout"
end

#perform_requestObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/helpers/ox3client.rb', line 40

def perform_request
  http = self.create_http(@site)
  http.open_timeout =  5 * 60
  http.read_timeout = 15 * 60
  params = Hash.new
  params['Content-Type'] = 'application/json'
  params['Cookie'] = 'openx3_access_token=' + @acccess_token.token + '; domain=' + get_domain + '; path=/'
  prefix = "/ox/4.0"
  response = http.request yield prefix, params
  response.body
end

#post(path, body = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/helpers/ox3client.rb', line 62

def post(path, body = {})
  if body.is_a?(Hash)
    body = JSON.dump(body)
  end
  perform_request do |prefix, params|
    self.create_http_request(
      :post, 
      prefix + path,
      body,
      params
    )
  end
end

#put(path, body = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/helpers/ox3client.rb', line 76

def put(path, body = {})
  if body.is_a?(Hash)
    body = JSON.dump(body)
  end
  perform_request do |prefix, params|
    self.create_http_request(
      :put, 
      prefix + path,
      body,
      params
    )
  end
end