Class: CS::Auth::HTTP

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cs/auth/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri = nil, api_key = nil) ⇒ HTTP

Returns a new instance of HTTP.


12
13
14
15
16
17
# File 'lib/cs/auth/http.rb', line 12

def initialize(base_uri = nil, api_key = nil)
  @base_uri = base_uri
  @api_key = api_key
  @session_id = nil
  reset
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key


10
11
12
# File 'lib/cs/auth/http.rb', line 10

def api_key
  @api_key
end

#base_uriObject

Returns the value of attribute base_uri


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def base_uri
  @base_uri
end

#errorsObject

Returns the value of attribute errors


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def errors
  @errors
end

#loggerObject

Returns the value of attribute logger


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def logger
  @logger
end

#request_bodyObject

Returns the value of attribute request_body


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def request_body
  @request_body
end

#request_headersObject

Returns the value of attribute request_headers


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def request_headers
  @request_headers
end

#response_bodyObject

Returns the value of attribute response_body


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def response_body
  @response_body
end

#response_codeObject

Returns the value of attribute response_code


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def response_code
  @response_code
end

#response_headersObject

Returns the value of attribute response_headers


9
10
11
# File 'lib/cs/auth/http.rb', line 9

def response_headers
  @response_headers
end

#session_idObject

Returns the value of attribute session_id


10
11
12
# File 'lib/cs/auth/http.rb', line 10

def session_id
  @session_id
end

Instance Method Details

#default_headersObject


81
82
83
84
85
86
87
88
# File 'lib/cs/auth/http.rb', line 81

def default_headers
  header = self.class.default_options[:headers] || {}
  header.merge!({"Content-Type" => "application/json"})
  if @session_id
    header = header.merge('X-SESSION_ID' => self.session_id)
  end
  header
end

#default_headers=(header_hash) ⇒ Object


90
91
92
# File 'lib/cs/auth/http.rb', line 90

def default_headers=(header_hash)
  self.class.default_options[:headers] = header_hash
end

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


63
64
65
66
67
68
69
# File 'lib/cs/auth/http.rb', line 63

def delete(path, query={}, headers = {})
  execute do
    @request_headers = default_headers.merge(headers)
    options = {query: query, headers: @request_headers}
    self.class.delete(process_path(path), options)
  end
end

#execute(&block) ⇒ Object


19
20
21
22
23
24
# File 'lib/cs/auth/http.rb', line 19

def execute(&block)
  reset
  @response_body = yield
  parse_response
  @response_body
end

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


42
43
44
45
46
47
48
49
# File 'lib/cs/auth/http.rb', line 42

def get(path, query={}, headers = {})
  execute do
    @request_headers = default_headers.merge(headers)
    options = {query: query, headers: @request_headers}
    path = process_api_key(process_path(path))
    self.class.get(path, options)
  end
end

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


71
72
73
74
75
# File 'lib/cs/auth/http.rb', line 71

def head(path, headers = {})
  execute do
    self.class.head(process_path(path), prepare(nil, headers))
  end
end

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

login to commonsense

Returns:

  • (String)

    session_id


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cs/auth/http.rb', line 100

def (username, password, digest=true)
  if digest
    password = Digest::MD5.hexdigest password
  end

  post('/login.json', {:username => username, :password => password})

  if @response_code == 200
    self.session_id = response_body['session_id']
  else
    self.session_id = false
    @errors = [response_body['error']]
  end

  session_id
end

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


51
52
53
54
55
# File 'lib/cs/auth/http.rb', line 51

def post(path, body = '', headers = {})
  execute do
    self.class.post(process_path(path), prepare(body, headers))
  end
end

#process_api_key(path) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cs/auth/http.rb', line 26

def process_api_key(path)
  return path if @api_key.nil?

  if URI(path).query.nil?
    path += "?API_KEY=#{@api_key}"
  else
    path += "&API_KEY=#{@api_key}"
  end

  path
end

#process_path(path) ⇒ Object


38
39
40
# File 'lib/cs/auth/http.rb', line 38

def process_path(path)
  return base_uri + path
end

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


57
58
59
60
61
# File 'lib/cs/auth/http.rb', line 57

def put(path, body = '', headers = {})
  execute do
    self.class.put(process_path(path), prepare(body, headers))
  end
end