Skip to content

Commit

Permalink
Lets Roll
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid Akram committed Nov 22, 2012
0 parents commit 098bf78
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in expedia.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Zaid Akram

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Expedia

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'expedia'

And then execute:

$ bundle

Or install it yourself as:

$ gem install expedia

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
26 changes: 26 additions & 0 deletions expedia.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'expedia/version'

Gem::Specification.new do |gem|
gem.name = "expedia"
gem.version = Expedia::VERSION
gem.authors = ["Zaid Akram"]
gem.email = ["[email protected]"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_runtime_dependency(%q<multi_json>, ["~> 1.3"])
gem.add_runtime_dependency(%q<faraday>, ["~> 0.8"])
gem.add_runtime_dependency(%q<addressable>, ["~> 2.2"])
gem.add_development_dependency(%q<rspec>, ["~> 2.8"])
gem.add_development_dependency(%q<rake>, ["~> 0.8"])

end
27 changes: 27 additions & 0 deletions lib/expedia.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# useful tools
require 'digest/md5'
require 'multi_json'

# include koala modules
require 'expedia/errors'


# HTTP module so we can communicate with Expedia
require 'expedia/http_service'

# miscellaneous
require 'expedia/utils'
require 'expedia/version'

module Expedia

mattr_accessor :cid, :api_key, :shared_secret, :format, :locale,
:currency_code

# Default way to setup Devise. Run generator to create
# a fresh initializer with all configuration values.
def self.setup
yield self
end

end
7 changes: 7 additions & 0 deletions lib/expedia/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Expedia

module Api

end

end
46 changes: 46 additions & 0 deletions lib/expedia/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Expedia

class ExpediaError < StandardError; end

# Expedia responded with status of 200 even if there is and exception (most of the time)
class APIError < ::Expedia::ExpediaError
attr_accessor :category, :presentation_message, :verbose_message,
:status, :error_body, :handling

# Create a new API Error
#
# @param status [Integer] The HTTP status code of the response
# @param error_body The parsed response body
# @param error_info One of the following:
# [Hash] The error information extracted from the request
# ("type", "code", "error_subcode", "message")
# [String] The error description
# If error_info is nil or not provided, the method will attempt to extract
# the error info from the response_body
#
# @return the newly created APIError
def initialize(status, body)
@error_body = body
@status = status

begin
@error_body = @error_body[@error_body.keys[0]]['EanWsError']
rescue
end

unless @error_body.blank?
@category = error_body['category']
@presentation_message = error_body['presentationMessage']
@verbose_message = error_body['verboseMessage']
@handling = error_body['handling']
error_array = []
end

super(@verbose_message)

end
end

class AuthCredentialsError < ::Expedia::ExpediaError; end

end
104 changes: 104 additions & 0 deletions lib/expedia/http_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require 'faraday'
require 'expedia/http_service/response'

module Expedia
module HTTPService

class << self

API_SERVER = 'api.ean.com'
RESERVATION_SERVER = 'book.api.ean.com'

# The address of the appropriate Expedia server.
#
# @param options various flags to indicate which server to use.
# @option options :reservation_api use the RESERVATION API instead of the REGULAR API
# @option options :use_ssl force https, even if not needed
#
# @return a complete server address with protocol
def server(options = {})
server = "#{options[:reservation_api] ? RESERVATION_SERVER : API_SERVER}"
"#{options[:use_ssl] ? "https" : "http"}://#{server}"
end

# Makes a request directly to Expedia.
# @note You'll rarely need to call this method directly.
#
# @see Expedia::API#api
#
# @param path the server path for this request
# @param args (see Expedia::API#api)
# @param verb the HTTP method to use.
# If not get or post, this will be turned into a POST request with the appropriate :method
# specified in the arguments.
# @param options (see Expedia::API#api)
#
# @raise an appropriate connection error if unable to make the request to Expedia
#
# @return [Expedia::HTTPService::Response] a response object representing the results from Facebook
def make_request(path, args, verb, options = {})
args.merge!(add_common_parameters)
# figure out our options for this request
request_options = {:params => (verb == "get" ? args : {})}
request_options[:use_ssl] = true if options[:reservation_api] # require https if there's a token
if request_options[:use_ssl]
ssl = (request_options[:ssl] ||= {})
ssl[:verify] = true unless ssl.has_key?(:verify)
end

# set up our Faraday connection
# we have to manually assign params to the URL or the
conn = Faraday.new(server(options), request_options)

response = conn.send(verb, path, (verb == "post" ? args : {}))

# Log URL information
Expedia::Utils.debug "# Expedia [{verb.upcase}] - #{path} params: #{args.inspect} : #{response.status}"
response = Expedia::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
# TODO: Handle 403 Error
if response.exception?
raise Expedia::APIError.new(response.status, response.body)
else
response
end
end

# Encodes a given hash into a query string.
#
# @param params_hash a hash of values to CGI-encode and appropriately join
#
# @example
# Expedia.http_service.encode_params({:a => 2, :b => "My String"})
# => "a=2&b=My+String"
#
# @return the appropriately-encoded string
def encode_params(param_hash)
((param_hash || {}).sort_by{|k, v| k.to_s}.collect do |key_and_value|
key_and_value[1] = MultiJson.dump(key_and_value[1]) unless key_and_value[1].is_a? String
"#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
end).join("&")
end


# Creates a Signature for Expedia using MD5 Checksum Auth.
# Shared and Api keys are required for Signature along with the current utc time.
def signature
if Expedia.cid && Expedia.api_key && Expedia.shared_secret
Digest::MD5.hexdigest(Expedia.api_key+Expedia.shared_secret+Time.now.utc.to_i.to_s)
else
raise Expedia::AuthCredentialsError, "cid, api_key and shared_secret are required for Expedia Authentication."
end
end

# Common Parameters required for every Call to Expedia Server.
def add_common_parameters
{ :cid => Expedia.cid, :sig => signature, :apiKey => Expedia.api_key,
:_type => 'json', :locale => Expedia.locale, :currencyCode => Expedia.currency_code }
end

end



end
end
24 changes: 24 additions & 0 deletions lib/expedia/http_service/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Expedia

module HTTPService

class Response

attr_reader :status, :body, :headers

# Creates a new Response object, which standardizes the response received From Expedia.
def initialize(status, body, headers)
@status = status
@body = MultiJson.load(body) rescue ''
@headers = headers
end

def exception?
@body && @body[@body.keys[0]]['EanWsError'] ? true : false
end

end

end

end
33 changes: 33 additions & 0 deletions lib/expedia/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Expedia
module Utils

# Utility methods used by Expedia.
require 'logger'
require 'forwardable'

extend Forwardable
extend self

def_delegators :logger, :debug, :info, :warn, :error, :fatal, :level, :level=

# The Expedia logger, an instance of the standard Ruby logger, pointing to STDOUT by default.
# In Rails projects, you can set this to Rails.logger.
attr_accessor :logger
self.logger = Logger.new(STDOUT)
self.logger.level = Logger::ERROR

# @private
DEPRECATION_PREFIX = "EXPEDIA: Deprecation warning: "

# Prints a deprecation message.
# Each individual message will only be printed once to avoid spamming.
def deprecate(message)
@posted_deprecations ||= []
unless @posted_deprecations.include?(message)
# only include each message once
Kernel.warn("#{DEPRECATION_PREFIX}#{message}")
@posted_deprecations << message
end
end
end
end
3 changes: 3 additions & 0 deletions lib/expedia/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Expedia
VERSION = "0.0.1"
end

0 comments on commit 098bf78

Please sign in to comment.