Module: Prestashopper
- Defined in:
- lib/prestashopper.rb,
lib/prestashopper/api.rb,
lib/prestashopper/product.rb,
lib/prestashopper/version.rb,
lib/prestashopper/uri_handler.rb
Overview
Ruby gem for interacting with the Prestashop API.
Defined Under Namespace
Classes: API, Product, UriHandler
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
-
.api_enabled?(url) ⇒ boolean
Check if there is an enabled API in a Prestashop instance.
-
.valid_key?(url, key) ⇒ boolean
Check if an API key is valid.
Class Method Details
.api_enabled?(url) ⇒ boolean
Check if there is an enabled API in a Prestashop instance.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/prestashopper.rb', line 20 def self.api_enabled?(url) api_uri = UriHandler.api_uri url res = RestClient::Resource.new api_uri, user: '', password: '' begin response = res.get # We don't send an API key, so an HTTP error code should be returned. Execution shouldn't reach here normally. raise StandardError.new "Expected 401 or 503 response from Prestashop API #{api_uri} without key, instead received <#{response.code}> #{response.body}" rescue RestClient::Exception => e if e.response.code == 401 return true # "Unauthorized" response means API is enabled elsif e.response.code == 503 return false # "Service Unavailable" response means API is disabled else raise e # Any other HTTP error code means something is wrong end end end |
.valid_key?(url, key) ⇒ boolean
Check if an API key is valid.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/prestashopper.rb', line 46 def self.valid_key?(url, key) api_uri = UriHandler.api_uri url res = RestClient::Resource.new api_uri, user: key, password: '' begin response = res.get if response.code == 200 # OK response means API key is valid return true else raise StandardError.new "Expected 200 or 401 response from Prestashop API #{api_uri} with key #{key}, instead received <#{response.code}> #{response.body}" end rescue RestClient::Exception => e if e.response.code == 401 return false # "Unauthorized" response means API key is invalid else raise e # Any other HTTP error code means something is wrong end end end |