Skip to content

Commit

Permalink
Merge pull request #28 from open-ndc/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
programisti committed Jan 26, 2016
2 parents d4b46c7 + a114d58 commit bed2d10
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
35 changes: 15 additions & 20 deletions lib/ndc_ex.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule NDCEx do
require HTTPotion
require Logger
import SweetXml
@acceptable_ndc_methods [ AirShopping: ["AirShoppingRQ", "AirShoppingRS"],
FlightPrice: ["FlightPriceRQ", "FlightPriceRS"],
SeatAvailability: ["SeatAvailabilityRQ", "SeatAvailabilityRS"],
Expand All @@ -15,39 +14,35 @@ defmodule NDCEx do
]

def request(method, params) do
try do
[request_name, response_name] = @acceptable_ndc_methods[method]

:ndc
|> get_mix_config
|> NDCEx.Message.Base.build_document(request_name, params)
|> rest_call_with_message(get_mix_config(:rest))
rescue
e -> {:error, error_message(e.message)}
end
end

def rest_call_with_message(xml, rest_config) do
Logger.debug xml
try do
case HTTPotion.post rest_config[:url], [body: xml, headers: rest_config[:headers]] do
%HTTPotion.Response{body: body, headers: _headers, status_code: 200 } ->
{:ok, Exmerl.parse(body)}
%HTTPotion.Response{body: body, headers: _headers, status_code: _status_code } ->
{:error, Exmerl.parse(body)}
apply(NDCEx.Response.Error, :convert, [body])
%HTTPotion.Response{status_code: 404} ->
{:error, error_message("Request does not exist")}
_ ->
{:error, error_message("Unknown Error")}
end
rescue
e -> {:error, error_message(e.message)}
case HTTPotion.post rest_config[:url], [body: xml, headers: rest_config[:headers]] do
%HTTPotion.Response{body: body, headers: _headers, status_code: 200 } ->
{:ok, scan body}
%HTTPotion.Response{body: body, headers: _headers, status_code: _status_code } ->
{:error, scan body}
%HTTPotion.Response{status_code: 404} ->
{:error, error_message("Request does not exist")}
_ ->
{:error, error_message("Unknown Error")}
end
end

def scan(doc) do
{ response, _ } = doc |> String.to_char_list |> :xmerl_scan.string
response
end

defp error_message(message) do
Exmerl.parse "<error><message>#{message}</message></error>"
scan "<error><message>#{message}</message></error>"
end

def get_mix_config(key) when is_atom(key) do
Expand Down
2 changes: 0 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ defmodule NDCExSdk.Mixfile do
defp deps do
[
{:xml_builder, "0.0.8"},
{:exmerl, "~> 0.1.1"},
{:pipe, "~> 0.0.2"},
{:ibrowse, github: "cmullaparthi/ibrowse", tag: "v4.1.2"},
{:httpotion, "~> 2.1.0"},
{:sweet_xml, "~> 0.5.0"},
{:credo, "~> 0.2", only: [:dev, :test]}
]
end
Expand Down
1 change: 0 additions & 1 deletion test/seat_availability_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule SeatAvailabilityTest do
require Logger
use ExUnit.Case
require HTTPotion
doctest NDCEx

@seat_params [
Expand Down
12 changes: 7 additions & 5 deletions test/service_list_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule ServiceListTest do
require Logger
require Record
use ExUnit.Case
require HTTPotion
doctest NDCEx
import SweetXml
Record.defrecord :xmlElement, Record.extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")

@valid_core_query_params [
CoreQuery: [
Expand All @@ -23,15 +24,16 @@ defmodule ServiceListTest do

test "Call ServiceList request" do
{_, xml} = NDCEx.request(:AirShopping, @valid_core_query_params)
response_id = xml |> xpath(~x"//AirShoppingRS/ShoppingResponseIDs/ResponseID/text()")
[element] = :xmerl_xpath.string('/AirShoppingRS/ShoppingResponseIDs/ResponseID', xml)
[text] = xmlElement(element, :content)
response_id = xmlText(text, :value)
params = [
ShoppingResponseIDs: [
ResponseID: response_id
ResponseID: to_string(response_id)
]
]

{status, body} = NDCEx.request(:ServiceList, params)
IO.inspect params
IO.inspect body
assert status == :ok
end
Expand Down

0 comments on commit bed2d10

Please sign in to comment.