Module: TiendaNube::Resource
- Defined in:
- lib/tienda_nube/resource.rb
Defined Under Namespace
Classes: Base
Class Attribute Summary collapse
-
.access_token ⇒ Object
Returns the value of attribute access_token.
-
.api_url ⇒ Object
Returns the value of attribute api_url.
-
.api_version ⇒ Object
Returns the value of attribute api_version.
-
.store_id ⇒ Object
Returns the value of attribute store_id.
Class Method Summary collapse
- .base_url ⇒ Object
- .config {|_self| ... } ⇒ Object
- .delete(path) ⇒ Object
- .get(path, query = {}) ⇒ Object
- .post(path, data) ⇒ Object
- .put(path, data) ⇒ Object
- .send_request(uri, request) ⇒ Object
- .uri(path) ⇒ Object
Class Attribute Details
.access_token ⇒ Object
Returns the value of attribute access_token.
4 5 6 |
# File 'lib/tienda_nube/resource.rb', line 4 def access_token @access_token end |
.api_url ⇒ Object
Returns the value of attribute api_url.
4 5 6 |
# File 'lib/tienda_nube/resource.rb', line 4 def api_url @api_url end |
.api_version ⇒ Object
Returns the value of attribute api_version.
4 5 6 |
# File 'lib/tienda_nube/resource.rb', line 4 def api_version @api_version end |
.store_id ⇒ Object
Returns the value of attribute store_id.
4 5 6 |
# File 'lib/tienda_nube/resource.rb', line 4 def store_id @store_id end |
Class Method Details
.base_url ⇒ Object
11 12 13 |
# File 'lib/tienda_nube/resource.rb', line 11 def self.base_url "#{self.api_url}/#{self.api_version}/#{self.store_id}" end |
.config {|_self| ... } ⇒ Object
8 9 10 |
# File 'lib/tienda_nube/resource.rb', line 8 def self.config yield self end |
.delete(path) ⇒ Object
48 49 50 51 52 |
# File 'lib/tienda_nube/resource.rb', line 48 def self.delete(path) uri = self.uri(path) request = Net::HTTP::Delete.new uri self.send_request(uri, request) end |
.get(path, query = {}) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/tienda_nube/resource.rb', line 42 def self.get(path, query={}) uri = self.uri(path) uri.query = URI.encode_www_form(query) unless query.empty? request = Net::HTTP::Get.new uri self.send_request(uri, request) end |
.post(path, data) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/tienda_nube/resource.rb', line 53 def self.post(path, data) uri = self.uri(path) request = Net::HTTP::Post.new uri request.body = data.to_json self.send_request(uri, request) end |
.put(path, data) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/tienda_nube/resource.rb', line 59 def self.put(path, data) uri = self.uri(path) request = Net::HTTP::Put.new uri request.body = data.to_json self.send_request(uri, request) end |
.send_request(uri, request) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tienda_nube/resource.rb', line 14 def self.send_request(uri, request) request['Authentication'] = "bearer #{self.access_token}" request['Content-Type'] = "application/json" response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| http.request request end result = { :status => [response.code, response.], :headers => response.to_hash, :body => response.body && !response.body.empty? ? JSON.parse(response.body) : nil } if links = response["link"] result[:pages] = links.split(", ").inject({}) do |hash, link| array = link.split("; ") hash[$1] = array[0].gsub(/^<(.*)>$/, '\1') if array[1].match(/rel=\"([a-z]+)\"/) hash end end return result end |
.uri(path) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/tienda_nube/resource.rb', line 35 def self.uri(path) unless (uri=URI(path)).absolute? uri = URI(self.base_url) uri.path = "/#{self.api_version}/#{self.store_id}/#{path}" end return uri end |