class Sigimera::Client
The main class that could be used to access the REST API
Attributes
The authentication token that is used for the API calls.
Public Class Methods
This method returns current API version. For this purpose the X-API-Version HTTP header is read out.
@return [String] The first found version string of the API
# File lib/sigimera/client.rb, line 20 def self.get_api_version client = Sigimera::Client.new response = client.head("/public/crises.json") response.get_fields("X-API-Version").first.to_s if response.key?("X-API-Version") end
This method returns an authentication token. If the auth_token
exists it is returned, otherwise a new one is created.
@param [String] username The username (email) that is used for the basic authentication @param [String] password The password that is used for the basic authentication @return [String] The authentication token as string
# File lib/sigimera/client.rb, line 53 def self.get_auth_token(username, password) basic_hash = Base64.strict_encode64("#{username}:#{password}") client = Sigimera::Client.new response = client.post("/v1/tokens.json", basic_hash) json = JSON.parse response.body if response json['auth_token'].to_s if json and json['auth_token'] end
This method returns the latest 10 crises, but needs no authentication token.
@return [Array] Returns an array of crises objects in JSON
# File lib/sigimera/client.rb, line 30 def self.get_public_crises client = Sigimera::Client.new response = client.get("/public/crises.json") Client.json_array_to_crisis_array(JSON.parse(response.body)) if response end
This method returns the latest 10 crises as RSS feed, but needs no authentication token.
@return [Nokogiri::XML::Document] Returns an a nokogiri xml document
# File lib/sigimera/client.rb, line 40 def self.get_public_rss_feed client = Sigimera::Client.new response = client.get("/public/crises.rss") Nokogiri::XML(response.body) if response end
# File lib/sigimera/client.rb, line 11 def initialize(auth_token = nil, username = nil, password = nil) @auth_token = auth_token if auth_token @auth_token = self.get_auth_token(username, password) if username and password end
Private Class Methods
Converts a JSON array into an array of #{Crisis} objects
# File lib/sigimera/client.rb, line 105 def self.json_array_to_crisis_array json_array crises_array = Array.new json_array.each do |crisis| crises_array << Sigimera::Crisis.new(crisis) end end
Public Instance Methods
This method returns statistic information about the crises.
@return [Array] Returns the crises statistic as JSON object
# File lib/sigimera/client.rb, line 87 def get_crises_stat response = self.get("/v1/stats/crises.json?auth_token=#{@auth_token}") JSON.parse response.body if response and response.body end
This method returns a single crisis.
@param [String] identifier A unique crisis identifier @return [Hash] The single crisis as JSON object
# File lib/sigimera/client.rb, line 76 def get_crisis(identifier, params = nil) return nil if identifier.nil? or identifier.empty? endpoint = "/v1/crises/#{identifier}.json?auth_token=#{@auth_token}" endpoint += "&#{URI.encode_www_form params}" if params response = self.get(endpoint) Sigimera::Crisis.new JSON.parse response.body if response and response.body end
This method returns the latest 10 crises.
@param [String] params A hash that contains the http parameters for the call, e.g. { :type => “earthquake”, :level => “red”, :output => “short” } @return [Array] Returns an array of crises objects in JSON
# File lib/sigimera/client.rb, line 65 def get_latest_crises(params = nil) endpoint = "/v1/crises.json?auth_token=#{@auth_token}" endpoint += "&#{URI.encode_www_form params}" if params response = self.get(endpoint.to_s) Client.json_array_to_crisis_array(JSON.parse(response.body)) if response and response.body end
This method returns statistic information about user.
@return [Array] Returns the user statistic as JSON object
# File lib/sigimera/client.rb, line 95 def get_user_stat response = self.get("/v1/stats/users.json?auth_token=#{@auth_token}") JSON.parse response.body if response and response.body end