class FannyPack::Request

FannyPack::Request handles forming the XML request to be sent to the Fantastico API, and parsing the response.

Constants

API_URL

The URL for the Fantastico API

VALID_ACTIONS

The Fantastico API supports these methods

Attributes

params[R]

Parameters for the API request

@return [Hash]

response[R]

The parsed API response

@return [Hash]

Public Class Methods

new() click to toggle source
# File lib/fanny_pack/request.rb, line 27
def initialize
  @action   = :invalid
  @response = {}
  @params   = {}
end

Public Instance Methods

commit(action, params = {}) click to toggle source

Send this request to the Fantastico API

Returns a Hash or Array, depending on the response from Fantastico

@param [Symbol] action

The action to perform, one of +VALID_ACTIONS+

@param [Hash]

Parameters for the API method

@return [Hash, Array]

# File lib/fanny_pack/request.rb, line 44
def commit(action, params = {})
  unless VALID_ACTIONS.include? action.to_sym
    raise "Invalid action"
  end
  @action = action
  @params = params
  
  conn = Faraday.new(:url => API_URL) do |c|
    c.request :soap
    c.response :soap, "item"
    c.adapter :net_http
  end
  
  request = conn.post do |r|
    r.body = format_params_for_fantastico
    r.headers = {'Content-Type' => 'text/xml; charset=utf-8'}
  end
  
  response = request.body
  
  @success = !(response.is_a?(Hash) && response.has_key?("faultcode"))
  
  response
end
success?() click to toggle source

Returns true if a commit was successful

@return [Boolean]

# File lib/fanny_pack/request.rb, line 72
def success?
  @success
end

Private Instance Methods

format_params_for_fantastico() click to toggle source

Wraps each param value in array so XmlSimple interprets them as child elements rather than attributes

# File lib/fanny_pack/request.rb, line 79
def format_params_for_fantastico
  params = @params.inject({}) do |result, (k, v)|
    result[k] = [v]
    result
  end
  {@action => {'accountHASH' => [FannyPack.account_hash]}.merge(params)}
end