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)
  self.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

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
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

#base_uri=(uri = nil) ⇒ Object



75
76
77
# File 'lib/cs/auth/http.rb', line 75

def base_uri=(uri = nil)
  self.class.base_uri uri
end

#default_headersObject



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

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



88
89
90
# File 'lib/cs/auth/http.rb', line 88

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

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



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

def delete(path, query={}, headers = {})
  execute do
    headers = default_headers.merge(headers)
    options = {query: query, headers: headers}
    self.class.delete(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



38
39
40
41
42
43
44
45
# File 'lib/cs/auth/http.rb', line 38

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

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



69
70
71
72
73
# File 'lib/cs/auth/http.rb', line 69

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

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

login to commonsense

Returns:

  • (String)

    session_id



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

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



47
48
49
50
51
52
# File 'lib/cs/auth/http.rb', line 47

def post(path, body = '', headers = {})
  execute do
    headers = default_headers.merge(headers)
    self.class.post(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

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



54
55
56
57
58
59
# File 'lib/cs/auth/http.rb', line 54

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