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, body, headers) ⇒ 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
31 32 33 |
# File 'lib/cs/session.rb', line 31 def api_key auth_proxy.api_key end |
#api_key=(api_key) ⇒ Object
40 41 42 43 |
# File 'lib/cs/session.rb', line 40 def api_key=(api_key) @api_key = api_key @auth_proxy = CS::Auth::HTTP.new(@base_uri, api_key) end |
#auth_proxy ⇒ Object
45 46 47 48 |
# File 'lib/cs/session.rb', line 45 def auth_proxy raise 'The session is not logged in' unless @auth_proxy @auth_proxy end |
#delete(path, body = '', headers = {}) ⇒ Object
108 109 110 111 112 |
# File 'lib/cs/session.rb', line 108 def delete(path, body='', headers = {}) execute("DELETE", path, body, headers) do auth_proxy.delete(path, body, headers) end end |
#dump_to_text(path) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cs/session.rb', line 141 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
132 133 134 |
# File 'lib/cs/session.rb', line 132 def errors auth_proxy.errors end |
#execute(type, path, body, headers, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cs/session.rb', line 79 def execute(type, path, body, headers, &block) start_time = Time.now log_request(type, path, body, headers) if logger response = retry_on_509 { yield } elapsed = (Time.now - start_time) * 1000.0 log_response(elapsed) if logger response end |
#get(path, body = '', headers = {}) ⇒ Object
90 91 92 93 94 |
# File 'lib/cs/session.rb', line 90 def get(path, body = '', headers = {}) execute("GET", path, body, headers) do auth_proxy.get(path, body, headers) end end |
#head(path, headers = {}) ⇒ Object
114 115 116 117 118 |
# File 'lib/cs/session.rb', line 114 def head(path, headers = {}) execute("HEAD", path, nil, headers) do auth_proxy.head(path, headers) end end |
#inspect ⇒ Object
174 175 176 |
# File 'lib/cs/session.rb', line 174 def inspect auth_proxy.kind_of?(CS::Auth::HTTP) ? to_s : "OAuth" end |
#log_request(type, path, body, headers) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cs/session.rb', line 63 def log_request(type, path, body, headers) logger.info("") logger.info("#{type} #{path}") logger.debug("headers: #{headers.inspect}") if ["POST", "PUT"].include?(type) logger.debug("request: #{body.inspect}") else logger.info("request: #{body.inspect}") end end |
#log_response(elapsed) ⇒ Object
74 75 76 77 |
# File 'lib/cs/session.rb', line 74 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 |
# File 'lib/cs/session.rb', line 16 def login(username, password, digest=true) @auth_proxy = CS::Auth::HTTP.new(@base_uri) @auth_proxy.login(username, password, digest) end |
#oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object
21 22 23 24 25 |
# File 'lib/cs/session.rb', line 21 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) end |
#open_in_browser(path = nil) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/cs/session.rb', line 157 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
96 97 98 99 100 |
# File 'lib/cs/session.rb', line 96 def post(path, body = '', headers = {}) execute("POST", path, body, headers) do auth_proxy.post(path, body, headers = {}) end end |
#put(path, body = '', headers = {}) ⇒ Object
102 103 104 105 106 |
# File 'lib/cs/session.rb', line 102 def put(path, body = '', headers = {}) execute("PUT", path, body, headers) do auth_proxy.put(path, body, headers) end end |
#response_body ⇒ Object
124 125 126 |
# File 'lib/cs/session.rb', line 124 def response_body auth_proxy.response_body end |
#response_code ⇒ Object
120 121 122 |
# File 'lib/cs/session.rb', line 120 def response_code auth_proxy.response_code end |
#response_headers ⇒ Object
128 129 130 |
# File 'lib/cs/session.rb', line 128 def response_headers auth_proxy.response_headers end |
#retry_on_509(&block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cs/session.rb', line 50 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
27 28 29 |
# File 'lib/cs/session.rb', line 27 def session_id auth_proxy.session_id end |
#session_id=(session_id) ⇒ Object
35 36 37 38 |
# File 'lib/cs/session.rb', line 35 def session_id=(session_id) @auth_proxy = CS::Auth::HTTP.new(@base_uri) @auth_proxy.session_id = session_id end |
#to_s ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/cs/session.rb', line 165 def to_s if session_id return "SESSION_ID \"#{session_id}\"" elsif api_key return "API_KEY \"#{api_key}\"" end return "" end |