Class: SiteMandala::SiteMandala
- Inherits:
-
Object
- Object
- SiteMandala::SiteMandala
- Defined in:
- lib/sitemandala/sitemandala.rb
Class Attribute Summary collapse
-
.token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
- .delete(endpoint) ⇒ Object
- .generate_url(endpoint) ⇒ Object
- .get(endpoint) ⇒ Object
- .handle_response(response) ⇒ Object
- .patch(endpoint, data) ⇒ Object
- .post(endpoint, data) ⇒ Object
- .put(endpoint, data) ⇒ Object
Class Attribute Details
.token ⇒ Object
Returns the value of attribute token.
25 26 27 |
# File 'lib/sitemandala/sitemandala.rb', line 25 def token @token end |
Class Method Details
.delete(endpoint) ⇒ Object
49 50 51 52 |
# File 'lib/sitemandala/sitemandala.rb', line 49 def self.delete(endpoint) url = generate_url(endpoint) handle_response HTTParty.delete(url, :format => :json) end |
.generate_url(endpoint) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/sitemandala/sitemandala.rb', line 54 def self.generate_url(endpoint) Configuration.ensure! :base_url Configuration.ensure! :token token = Configuration.token base_url = Configuration.base_url base_url+endpoint+"?token="+token end |
.get(endpoint) ⇒ Object
29 30 31 32 |
# File 'lib/sitemandala/sitemandala.rb', line 29 def self.get(endpoint) url = generate_url(endpoint) handle_response HTTParty.get(url, :format => :json) end |
.handle_response(response) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sitemandala/sitemandala.rb', line 62 def self.handle_response(response) case response.code when 400 raise BadRequest.new(Hashie::Mash.new response) when 401 data = Hashie::Mash.new(response) when 404 raise NotFound.new when 400...500 raise ClientError.new when 500...600 raise ServerError.new else records = response.parsed_response if records.is_a?(Array) records.map{ |item| Hashie::Mash.new(item) } elsif records.is_a?(Hash) Hashie::Mash.new(records) end end end |
.patch(endpoint, data) ⇒ Object
44 45 46 47 |
# File 'lib/sitemandala/sitemandala.rb', line 44 def self.patch(endpoint, data) url = generate_url(endpoint) handle_response HTTParty.patch(url, :body => data.to_json, :format => :json) end |
.post(endpoint, data) ⇒ Object
34 35 36 37 |
# File 'lib/sitemandala/sitemandala.rb', line 34 def self.post(endpoint, data) url = generate_url(endpoint) handle_response HTTParty.post(url, :body => data.to_json, :format => :json) end |
.put(endpoint, data) ⇒ Object
39 40 41 42 |
# File 'lib/sitemandala/sitemandala.rb', line 39 def self.put(endpoint, data) url = generate_url(endpoint) handle_response HTTParty.put(url, :body => data.to_json, :format => :json) end |