class Elibri::ApiClient::ApiAdapters::V1
Adapter dla 1 wersji API. Instancje odpowiedniego adaptera tworzy klasa Elibri::ApiClient
- nie robimy tego recznie.
Adapter dla pierwszej wersji API.
Constants
- EXCEPTION_CLASSES
Klasy wyjatkow rzucanych, gdy elibri zwroci okreslony blad. Np. gdy dostaniemy:
<error id="1001"> <message>Queue does not exist</message> </error>
Biblioteka rzuca wyjatkiem Elibri::ApiClient::ApiAdapters::V1::Exceptions::QueueDoesNotExists.
- URI_PREFIX
Public Instance Methods
# File lib/elibri_api_client/api_adapters/v1.rb, line 83 def add_to_queue(queue_name, record_reference) resp = post "/queues/#{queue_name}/add/#{record_reference}", :body => ' ' return true end
# File lib/elibri_api_client/api_adapters/v1.rb, line 149 def get_onix_message_with_product(product_or_rr) Elibri::ONIX::Release_3_0::ONIXMessage.new(onix_xml_for_product(product_or_rr, true)) end
# File lib/elibri_api_client/api_adapters/v1.rb, line 145 def get_product(product_or_rr) Elibri::ONIX::Release_3_0::Product.new(onix_xml_for_product(product_or_rr, false)) end
# File lib/elibri_api_client/api_adapters/v1.rb, line 104 def last_pop_from_queue(queue_name) response = get "/queues/#{queue_name}/last_pop", :headers => {"X-eLibri-API-ONIX-dialect" => @onix_dialect} Elibri::ApiClient::ApiAdapters::V1::QueuePop.new( :queue_name => response.headers["x-elibri-api-pop-queue-name"], :popped_products_count => response.headers["x-elibri-api-pop-products-count"], :created_at => response.headers["x-elibri-api-pop-created-at"], :xml => response.body ) rescue Exceptions::NoRecentlyPoppedData # Ignoruj bledy o braku ostatnich POPow. return nil end
# File lib/elibri_api_client/api_adapters/v1.rb, line 73 def olesiejuk_publishers get_publishers_from_url('/publishers/olesiejuk') end
# File lib/elibri_api_client/api_adapters/v1.rb, line 69 def pdw_publishers get_publishers_from_url('/publishers/pdw') end
Czy sa jakies oczekujace dane w Elibri
?
# File lib/elibri_api_client/api_adapters/v1.rb, line 56 def pending_data? !pending_queues.empty? end
Zwroc liste kolejek z oczekujacymi danymi - instancje Elibri::ApiClient::ApiAdapters::V1::Queue
# File lib/elibri_api_client/api_adapters/v1.rb, line 40 def pending_queues resp = get '/queues' Array.new.tap do |pending_queues| resp.parsed_response.css('queue').each do |queue_xml| queue = Elibri::ApiClient::ApiAdapters::V1::Queue.build_from_xml(self, queue_xml) pending_queues << queue if queue.products_count.nonzero? end end end
params moze przyjac {:testing => 1, :count => 100, :offset => 100 (tylko przy testing=1)}
# File lib/elibri_api_client/api_adapters/v1.rb, line 89 def pop_from_queue(queue_name, params = {}) params[:testing] = 1 if params[:testing] params = ' ' if params.empty? response = post "/queues/#{queue_name}/pop", :body => params, :headers => {"X-eLibri-API-ONIX-dialect" => @onix_dialect} return nil unless response.headers["x-elibri-api-pop-products-count"].to_i > 0 Elibri::ApiClient::ApiAdapters::V1::QueuePop.new( :queue_name => response.headers["x-elibri-api-pop-queue-name"], :popped_products_count => response.headers["x-elibri-api-pop-products-count"], :created_at => response.headers["x-elibri-api-pop-created-at"], :xml => response.body ) end
Zwroc liste dostepnych wydawnictw - instancje Elibri::ApiClient::ApiAdapters::V1::Publisher
# File lib/elibri_api_client/api_adapters/v1.rb, line 65 def publishers get_publishers_from_url('/publishers') end
Wypelnij wszystkie kolejki oczekujace, wszystkimi dostepnymi danymi. Przydatne przy wykonywaniu pelnej synchronizacji pomiedzy nasza aplikacja a Elibri
.
# File lib/elibri_api_client/api_adapters/v1.rb, line 29 def refill_all_queues! # Dla POST musi byc jakies 'body' requestu, bo serwery czesto rzucaja wyjatkami (WEBrick w szczegolnosci). post '/queues/refill_all', :body => ' ' true end
# File lib/elibri_api_client/api_adapters/v1.rb, line 78 def remove_from_queue(queue_name, record_reference) resp = post "/queues/#{queue_name}/remove/#{record_reference}", :body => ' ' return true end
Private Instance Methods
# File lib/elibri_api_client/api_adapters/v1.rb, line 157 def full_api_uri @host_uri + URI_PREFIX end
# File lib/elibri_api_client/api_adapters/v1.rb, line 173 def get(request_uri, options = {}) options.merge!({:digest_auth => @auth}) request_uri = normalise_request_uri(request_uri) response = self.class.get(request_uri, options) raise_if_error_present_in response response end
# File lib/elibri_api_client/api_adapters/v1.rb, line 161 def get_publishers_from_url(url) resp = get(url) Array.new.tap do |publishers| resp.parsed_response.css('publisher').each do |publisher_xml| publisher = Elibri::ApiClient::ApiAdapters::V1::Publisher.build_from_xml(self, publisher_xml) publishers << publisher end end end
# File lib/elibri_api_client/api_adapters/v1.rb, line 193 def normalise_request_uri(request_uri) if request_uri.start_with? 'http://' request_uri elsif !request_uri.start_with? '/' full_api_uri + '/' + request_uri else full_api_uri + request_uri end end
# File lib/elibri_api_client/api_adapters/v1.rb, line 183 def post(request_uri, options = {}) options.merge!({:digest_auth => @auth}) request_uri = normalise_request_uri(request_uri) response = self.class.post(request_uri, options) raise_if_error_present_in response response end
Jesli Elibri
zwoci jakis blad, to rzucamy odpowiednim wyjatkiem.
# File lib/elibri_api_client/api_adapters/v1.rb, line 205 def raise_if_error_present_in(response) response_xml = response.parsed_response if response_xml && !response_xml.css('error').empty? error_id = response_xml.css('error').first['id'] error_message = response_xml.css('error message').first.text # Rozpoznajemy ten kod bledu i mozemy rzucic okreslona klasa wyjatku: if exception_class = EXCEPTION_CLASSES[error_id.to_s] raise exception_class, error_message else # Jakis nieznany blad - rzucamy chociaz stringiem raise Exceptions::UnknownError, "ELIBRI_API ERROR #{error_id}: #{error_message}" end end end