Class: CS::Session
- Inherits:
-
Object
- Object
- CS::Session
- Defined in:
- lib/cs/session.rb
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #api_key ⇒ Object
- #api_key=(api_key) ⇒ Object
- #auth_proxy ⇒ Object
- #delete(path, body = '', headers = {}) ⇒ Object
- #dump_to_text(path) ⇒ Object
- #errors ⇒ Object
- #execute(type, path, body, headers, &block) ⇒ Object
- #get(path, body = '', headers = {}) ⇒ Object
- #head(path, headers = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Session
constructor
A new instance of Session.
- #inspect ⇒ Object
- #log_request(type, path) ⇒ Object
- #log_response(elapsed) ⇒ Object
-
#login(username, password, digest = true) ⇒ String
login to commonsense.
- #oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object
- #open_in_browser(path = nil) ⇒ Object
- #post(path, body = '', headers = {}) ⇒ Object
- #put(path, body = '', headers = {}) ⇒ Object
- #response_body ⇒ Object
- #response_code ⇒ Object
- #response_headers ⇒ Object
- #retry_on_509(&block) ⇒ Object
- #session_id ⇒ Object
- #session_id=(session_id) ⇒ Object
- #to_s ⇒ Object
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={}) = { base_uri: 'https://api.sense-os.nl', authentication: true }.merge(opts) @base_uri = [:base_uri] @auth_proxy = [:authentication] ? nil : CS::Auth::HTTP.new(@base_uri) end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
3 4 5 |
# File 'lib/cs/session.rb', line 3 def base_uri @base_uri end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/cs/session.rb', line 3 def logger @logger end |
Instance Method Details
#api_key ⇒ Object
34 35 36 |
# File 'lib/cs/session.rb', line 34 def api_key auth_proxy.api_key end |
#api_key=(api_key) ⇒ Object
43 44 45 46 |
# File 'lib/cs/session.rb', line 43 def api_key=(api_key) @api_key = api_key @auth_proxy = CS::Auth::HTTP.new(@base_uri, api_key) end |
#auth_proxy ⇒ Object
48 49 50 51 |
# File 'lib/cs/session.rb', line 48 def auth_proxy raise 'The session is not logged in' unless @auth_proxy @auth_proxy end |
#delete(path, body = '', headers = {}) ⇒ Object
114 115 116 117 118 |
# File 'lib/cs/session.rb', line 114 def delete(path, body='', headers = {}) execute("DELETE", path, body, headers) do auth_proxy.delete(path, body, headers) end end |
#dump_to_text(path) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cs/session.rb', line 147 def dump_to_text(path) begin body = response_body.to_s rescue Exception => e body = e. 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 |
#errors ⇒ Object
138 139 140 |
# File 'lib/cs/session.rb', line 138 def errors auth_proxy.errors end |
#execute(type, path, body, headers, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cs/session.rb', line 82 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) if logger response end |
#get(path, body = '', headers = {}) ⇒ Object
96 97 98 99 100 |
# File 'lib/cs/session.rb', line 96 def get(path, body = '', headers = {}) execute("GET", path, body, headers) do auth_proxy.get(path, body, headers) end end |
#head(path, headers = {}) ⇒ Object
120 121 122 123 124 |
# File 'lib/cs/session.rb', line 120 def head(path, headers = {}) execute("HEAD", path, nil, headers) do auth_proxy.head(path, headers) end end |
#inspect ⇒ Object
180 181 182 |
# File 'lib/cs/session.rb', line 180 def inspect auth_proxy.kind_of?(CS::Auth::HTTP) ? to_s : "OAuth" end |
#log_request(type, path) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cs/session.rb', line 66 def log_request(type, path) logger.info("") logger.info("#{type} #{path}") logger.debug("headers: #{@auth_proxy.request_headers.inspect}") if ["POST", "PUT"].include?(type) logger.debug("request: #{@auth_proxy.request_body.inspect}") else logger.info("request: #{@auth_proxy.request_body.inspect}") end end |
#log_response(elapsed) ⇒ Object
77 78 79 80 |
# File 'lib/cs/session.rb', line 77 def log_response(elapsed) 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
16 17 18 19 20 |
# File 'lib/cs/session.rb', line 16 def login(username, password, digest=true) @auth_proxy = CS::Auth::HTTP.new(@base_uri) @auth_proxy.logger = self.logger @auth_proxy.login(username, password, digest) end |
#oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/cs/session.rb', line 22 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
163 164 165 166 167 168 169 |
# File 'lib/cs/session.rb', line 163 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
102 103 104 105 106 |
# File 'lib/cs/session.rb', line 102 def post(path, body = '', headers = {}) execute("POST", path, body, headers) do auth_proxy.post(path, body, headers = {}) end end |
#put(path, body = '', headers = {}) ⇒ Object
108 109 110 111 112 |
# File 'lib/cs/session.rb', line 108 def put(path, body = '', headers = {}) execute("PUT", path, body, headers) do auth_proxy.put(path, body, headers) end end |
#response_body ⇒ Object
130 131 132 |
# File 'lib/cs/session.rb', line 130 def response_body auth_proxy.response_body end |
#response_code ⇒ Object
126 127 128 |
# File 'lib/cs/session.rb', line 126 def response_code auth_proxy.response_code end |
#response_headers ⇒ Object
134 135 136 |
# File 'lib/cs/session.rb', line 134 def response_headers auth_proxy.response_headers end |
#retry_on_509(&block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cs/session.rb', line 53 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_id ⇒ Object
30 31 32 |
# File 'lib/cs/session.rb', line 30 def session_id auth_proxy.session_id end |
#session_id=(session_id) ⇒ Object
38 39 40 41 |
# File 'lib/cs/session.rb', line 38 def session_id=(session_id) @auth_proxy = CS::Auth::HTTP.new(@base_uri) @auth_proxy.session_id = session_id end |
#to_s ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/cs/session.rb', line 171 def to_s if session_id return "SESSION_ID \"#{session_id}\"" elsif api_key return "API_KEY \"#{api_key}\"" end return "" end |