Class: CS::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Session

Returns a new instance of Session.


5
6
7
8
9
10
11
12
# File 'lib/cs/session.rb', line 5

def initialize(opts={})
  options = {
    base_uri: 'https://api.sense-os.nl',
    authentication: true
  }.merge(opts)
  @base_uri = options[:base_uri]
  @auth_proxy = options[:authentication] ? nil : CS::Auth::HTTP.new(@base_uri)
end

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri


3
4
5
# File 'lib/cs/session.rb', line 3

def base_uri
  @base_uri
end

#loggerObject

Returns the value of attribute logger


3
4
5
# File 'lib/cs/session.rb', line 3

def logger
  @logger
end

Instance Method Details

#api_keyObject


41
42
43
# File 'lib/cs/session.rb', line 41

def api_key
  auth_proxy.api_key
end

#api_key=(api_key) ⇒ Object


50
51
52
53
# File 'lib/cs/session.rb', line 50

def api_key=(api_key)
  @api_key = api_key
  @auth_proxy = CS::Auth::HTTP.new(@base_uri, api_key)
end

#auth_proxyObject


55
56
57
58
# File 'lib/cs/session.rb', line 55

def auth_proxy
  raise 'The session is not logged in' unless @auth_proxy
  @auth_proxy
end

#delete(path, body = '', headers = {}) ⇒ Object


124
125
126
127
128
# File 'lib/cs/session.rb', line 124

def delete(path, body='', headers = {})
  execute("DELETE", path, body, headers) do
    auth_proxy.delete(path, body, headers)
  end
end

#dump_to_text(path) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cs/session.rb', line 157

def dump_to_text(path)
  begin
    body = response_body.to_s
  rescue Exception => e
    body = e.message
  end

  File.open(path, 'w') do |f|
    f.write("Response Code: #{response_code}\n")
    f.write("Response Headers: #{response_headers}\n")
    f.write("Errors: #{errors}\n")
    f.write("\n")
    f.write(body)
  end
end

#errorsObject


148
149
150
# File 'lib/cs/session.rb', line 148

def errors
  auth_proxy.errors
end

#execute(type, path, body, headers, &block) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cs/session.rb', line 92

def execute(type, path, body, headers, &block)
  start_time = Time.now
  response = retry_on_509 do
    value = yield
    log_request(type, path) if logger
    value
  end

  elapsed = (Time.now - start_time) * 1000.0
  log_response(elapsed)

  response
end

#get(path, body = '', headers = {}) ⇒ Object


106
107
108
109
110
# File 'lib/cs/session.rb', line 106

def get(path, body = '', headers = {})
  execute("GET", path, body, headers) do
    auth_proxy.get(path, body, headers)
  end
end

#head(path, headers = {}) ⇒ Object


130
131
132
133
134
# File 'lib/cs/session.rb', line 130

def head(path, headers = {})
  execute("HEAD", path, nil, headers) do
    auth_proxy.head(path, headers)
  end
end

#inspectObject


190
191
192
# File 'lib/cs/session.rb', line 190

def inspect
  auth_proxy.kind_of?(CS::Auth::HTTP) ? to_s : "OAuth"
end

#log_request(type, path) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cs/session.rb', line 73

def log_request(type, path)
  return if logger.nil?

  logger.info("")
  logger.info("#{type} #{base_uri}#{path}")
  logger.debug("headers: #{@auth_proxy.request_headers.inspect}")
  if ["POST", "PUT"].include?(type)
    logger.debug("request: #{@auth_proxy.request_body.inspect}")
  else
    logger.debug("request: #{@auth_proxy.request_body.inspect}")
  end
end

#log_response(elapsed) ⇒ Object


86
87
88
89
90
# File 'lib/cs/session.rb', line 86

def log_response(elapsed)
  return if logger.nil?
  logger.info("result: #{self.response_code} in #{elapsed}ms")
  logger.debug("response: #{self.response_body}")
end

#login(username, password, digest = true) ⇒ String

login to commonsense

Returns:

  • (String)

    session_id


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cs/session.rb', line 16

def (username, password, digest=true)
  @auth_proxy = CS::Auth::HTTP.new(@base_uri)
  @auth_proxy.logger = self.logger

  start_time = Time.now
  result = @auth_proxy.(username, password, digest)
  elapsed = (Time.now - start_time) * 1000.0
  log_request("Logging in", "")
  log_response(elapsed)

  result
end

#oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/cs/session.rb', line 29

def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
  @auth_proxy = CS::Auth::OAuth.new(consumer_key, consumer_secret,
                                             access_token, access_token_secret,
                                             @base_uri)
  @auth_proxy.logger = self.logger if @auth_proxy
  @auth_proxy
end

#open_in_browser(path = nil) ⇒ Object


173
174
175
176
177
178
179
# File 'lib/cs/session.rb', line 173

def open_in_browser(path=nil)
  require 'launchy'

  path ||= "/tmp/common-sense-ruby-#{Time.now.to_i}.html"
  dump_to_text(path)
  ::Launchy::Browser.run(path)
end

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


112
113
114
115
116
# File 'lib/cs/session.rb', line 112

def post(path, body = '', headers = {})
  execute("POST", path, body, headers) do
    auth_proxy.post(path, body, headers = {})
  end
end

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


118
119
120
121
122
# File 'lib/cs/session.rb', line 118

def put(path, body = '', headers = {})
  execute("PUT", path, body, headers) do
    auth_proxy.put(path, body, headers)
  end
end

#response_bodyObject


140
141
142
# File 'lib/cs/session.rb', line 140

def response_body
  auth_proxy.response_body
end

#response_codeObject


136
137
138
# File 'lib/cs/session.rb', line 136

def response_code
  auth_proxy.response_code
end

#response_headersObject


144
145
146
# File 'lib/cs/session.rb', line 144

def response_headers
  auth_proxy.response_headers
end

#retry_on_509(&block) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cs/session.rb', line 60

def retry_on_509(&block)
  while true
    response = yield
    if response_code == 509
      waitfor = Random.new.rand(30..45)
      logger.info "limit reached. waiting for #{waitfor} before retrying" if logger
      sleep(waitfor)
    else
      return response
    end
  end
end

#session_idObject


37
38
39
# File 'lib/cs/session.rb', line 37

def session_id
  auth_proxy.session_id
end

#session_id=(session_id) ⇒ Object


45
46
47
48
# File 'lib/cs/session.rb', line 45

def session_id=(session_id)
  @auth_proxy = CS::Auth::HTTP.new(@base_uri)
  @auth_proxy.session_id = session_id
end

#to_sObject


181
182
183
184
185
186
187
188
# File 'lib/cs/session.rb', line 181

def to_s
  if session_id
    return "SESSION_ID \"#{session_id}\""
  elsif api_key
    return "API_KEY \"#{api_key}\""
  end
  return ""
end