lib/uatu/connection.rb in uatu-marvel-0.0.2 vs lib/uatu/connection.rb in uatu-marvel-0.1.0

- old
+ new

@@ -1,75 +1,82 @@ -require 'faraday' -require 'uatu/response' - module Uatu - module Connection - - def request(method, options, conn_options) - conn = build_connection(conn_options) - conn_params = prepare_options(options).merge(mandatory_params(conn_options)) - conn_route = build_route(method, options) + class Connection - conn.get conn_route, conn_params + BASE_URL = "http://gateway.marvel.com" + + attr_reader :resource, :options + + def initialize resource, options + @resource = resource + @options = options end - def build_connection(conn_options) - Faraday.new(url: conn_options.base_url) do |faraday| - faraday.use Uatu::Response::RaiseMarvelError + def request + connection.get path, params + end - faraday.request :url_encoded - faraday.adapter Faraday.default_adapter + private + + def connection + @connection ||= Faraday.new(url: BASE_URL) do |faraday| + faraday.use Uatu::Response::RaiseMarvelError + faraday.request :url_encoded + faraday.adapter Faraday.default_adapter end end - def build_route(method, options={}) - route = "/v1/public/#{valid_method(method)}" - if resource_id = options["#{valid_method(method).singularize}_id".to_sym] + def path + route = "/v1/public/#{resource_path}" + if resource_id = options["#{resource_path.singularize}_id".to_sym] route += "/#{resource_id}" end - # If it is combined, it comes afet the '_' - if method.split('_').count>1 && combined_path = method.split('_').last + # If it is combined, it comes after the '_' + if resource.split('_').count>1 && combined_path = resource.split('_').last route += "/#{combined_path}" end route end - def prepare_options(options) - valid_opts = {} - - # We remove innecessary keys that should go on the route - _options = options.reject{|key, value| key.to_s.match(/.*_id/)} + def params + request_params = {} - # We change the names, so 'format_type' becomes 'formatType' - _options.each{|key, value| valid_opts[key.to_s.camelize(:lower).to_sym] = value } + # We remove unnecessary keys that should go on the route + temp_params = options.reject{|key, value| key.to_s.match(/.*_id/)} + # We change the names, so 'format_type' becomes 'formatType' + temp_params.each{|key, value| request_params[key.to_s.camelize(:lower).to_sym] = value } + # An array should become a string with comma separated values - valid_opts.each{|key, value| valid_opts[key] = value.join(',') if value.is_a?(Array) } + request_params.each{|key, value| request_params[key] = value.join(',') if value.is_a?(Array) } - valid_opts + request_params.merge(mandatory_params) end # character => characters # characters => characters # character_comics => characters - def valid_method(method) - _method = method.split('_').first.pluralize - raise Uatu::Error.new('InvalidMethod') unless Uatu::Base::RESOURCES.map(&:pluralize).include?(_method) - _method + def resource_path + @resource_path ||= resource.split('_').first.pluralize.tap do |path| + raise Uatu::Error.new('InvalidMethod') unless Uatu::RESOURCES.map(&:pluralize).include?(path) + end end + def mandatory_params + { + :apikey => Uatu.public_key, + :ts => current_timestamp, + :hash => hash(current_timestamp) + } + end + def current_timestamp @ts ||= DateTime.now.to_s end - def hash(timestamp, conn_options) - Digest::MD5.hexdigest("#{timestamp}#{conn_options.private_key}#{conn_options.public_key}") + def hash timestamp + Digest::MD5.hexdigest("#{timestamp}#{Uatu.private_key}#{Uatu.public_key}") end - def mandatory_params(conn_options) - {apikey: conn_options.public_key, ts: current_timestamp, hash: hash(current_timestamp, conn_options)} - end - end -end \ No newline at end of file +end