Class: Cafmal::Resource
- Inherits:
-
Object
- Object
- Cafmal::Resource
- Defined in:
- lib/cafmal/resource.rb
Instance Attribute Summary collapse
-
#cafmal_api_url ⇒ Object
readonly
Returns the value of attribute cafmal_api_url.
-
#decoded_token ⇒ Object
readonly
Returns the value of attribute decoded_token.
-
#query ⇒ Object
Returns the value of attribute query.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #create(params) ⇒ Object
-
#demodulize(path) ⇒ Object
helpers below.
- #destroy(params) ⇒ Object
-
#initialize(cafmal_api_url, token) ⇒ Resource
constructor
A new instance of Resource.
- #list(*options) ⇒ Object
- #new ⇒ Object
- #show(id) ⇒ Object
- #update(params) ⇒ Object
Constructor Details
#initialize(cafmal_api_url, token) ⇒ Resource
Returns a new instance of Resource.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cafmal/resource.rb', line 18 def initialize(cafmal_api_url, token) @cafmal_api_url = cafmal_api_url @token = token @decoded_token = {} @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0])) @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1])) @query = "" @headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{@token}"} @resourcename = (demodulize(self.class.name) + "s").downcase end |
Instance Attribute Details
#cafmal_api_url ⇒ Object (readonly)
Returns the value of attribute cafmal_api_url.
14 15 16 |
# File 'lib/cafmal/resource.rb', line 14 def cafmal_api_url @cafmal_api_url end |
#decoded_token ⇒ Object (readonly)
Returns the value of attribute decoded_token.
13 14 15 |
# File 'lib/cafmal/resource.rb', line 13 def decoded_token @decoded_token end |
#query ⇒ Object
Returns the value of attribute query.
15 16 17 |
# File 'lib/cafmal/resource.rb', line 15 def query @query end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
12 13 14 |
# File 'lib/cafmal/resource.rb', line 12 def token @token end |
Instance Method Details
#create(params) ⇒ Object
29 30 31 32 33 |
# File 'lib/cafmal/resource.rb', line 29 def create(params) request_create_resource = Cafmal::Request::Post.new(@cafmal_api_url + "/#{@resourcename}", params.to_json, @headers) return request_create_resource.response end |
#demodulize(path) ⇒ Object
helpers below
67 68 69 70 71 72 73 74 |
# File 'lib/cafmal/resource.rb', line 67 def demodulize(path) path = path.to_s if i = path.rindex('::') path[(i+2)..-1] else path end end |
#destroy(params) ⇒ Object
59 60 61 62 63 |
# File 'lib/cafmal/resource.rb', line 59 def destroy(params) request_destroy_resource = Cafmal::Request::Delete.new(@cafmal_api_url + "/#{@resourcename}/#{params['id']}", params.to_json, @headers) return request_destroy_resource.response end |
#list(*options) ⇒ Object
41 42 43 44 45 |
# File 'lib/cafmal/resource.rb', line 41 def list(*) request_list_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}" + @query, @headers) return request_list_resource.response end |
#new ⇒ Object
35 36 37 38 39 |
# File 'lib/cafmal/resource.rb', line 35 def new request_new_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}/new", @headers) return request_new_resource.response end |