Class: Shoper
- Inherits:
-
Object
- Object
- Shoper
- Defined in:
- lib/shoper.rb
Overview
Shoper REST API main class
Instance Method Summary collapse
- #get_token ⇒ Object
-
#initialize(shoper_url, shoper_username, shoper_password) ⇒ Shoper
constructor
A new instance of Shoper.
-
#resource(resource, action, data = { :id => nil, :params => nil, :data => nil }) ⇒ Object
Shoper.resource(resource, action, params, id, data) * get -> /aboutpages/<id> * update -> /aboutpages/<id> * delete -> /aboutpages/<id> * insert -> /aboutpages * list -> /aboutpages.
Constructor Details
#initialize(shoper_url, shoper_username, shoper_password) ⇒ Shoper
Returns a new instance of Shoper.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/shoper.rb', line 7 def initialize(shoper_url, shoper_username, shoper_password) @configuration = { :shoper => {}, } @configuration[:shoper][:url] = shoper_url @configuration[:shoper][:api] = @configuration[:shoper][:url] + "/webapi/rest/" @configuration[:shoper][:username] = shoper_username @configuration[:shoper][:password] = shoper_password self.get_token end |
Instance Method Details
#get_token ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/shoper.rb', line 18 def get_token r = RestClient::Request.execute( method: :post, url: @configuration[:shoper][:url] + "/webapi/rest/auth", headers: { "Authorization" => "Basic " + Base64::encode64( @configuration[:shoper][:username] + ":" + @configuration[:shoper][:password] ), }, ) j = JSON.parse(r) @configuration[:shoper][:token] = j["access_token"] @configuration[:shoper][:token_expires] = Time.now + j["expires_in"] @configuration[:shoper][:token_type] = j["token_type"] end |
#resource(resource, action, data = { :id => nil, :params => nil, :data => nil }) ⇒ Object
Shoper.resource(resource, action, params, id, data)
* get -> /aboutpages/<id>
* update -> /aboutpages/<id>
* delete -> /aboutpages/<id>
* insert -> /aboutpages
* list -> /aboutpages
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/shoper.rb', line 40 def resource(resource, action, data = { :id => nil, :params => nil, :data => nil }) case action when "get", "update", "delete" endpoint = resource + "/" + data[:id] when "insert", "list" endpoint = resource else return { :error => true } end # Check if Shoper Token is still valid self.get_token if Time.now > @configuration[:shoper][:token_expires] r = RestClient::Resource.new(@configuration[:shoper][:api] + endpoint, :headers => { :authorization => "Bearer " + @configuration[:shoper][:token], }) case action when "get" res = r.get :params => data[:params] when "update" res = r.put data[:data].to_json, :content_type => :json when "delete" res = r.delete when "insert" res = r.post data[:data].to_json, :content_type => :json when "list" res = r.get :params => data[:params] else return { :error => true } end end |