require 'multi_json' require 'ostruct' require 'capgun/base' require 'capgun/account' require 'capgun/estimate' require 'capgun/job' require 'capgun/order' require 'capgun/config' require 'capgun/connection' require 'capgun/request' module Capgun # Wrapper for the Capgun REST API class Client include Capgun::Connection include Capgun::Request attr_accessor *Config::VALID_OPTIONS_KEYS # Initializes a new API object. Accessor attributes are set based # on Capgun::Config::VALID_OPTIONS_KEYS # # @param [Hash] attrs Addition client attributes. # @return [Capgun::Client] def initialize(attrs={}) attrs = Capgun.options.merge(attrs) Config::VALID_OPTIONS_KEYS.each do |key| instance_variable_set("@#{key}".to_sym, attrs[key]) end end # Estimates a capgun job cost / availability. # # @param [String] url A url that will be captured. # @param [Hash] options Additional options to the capture request. # @return [Capgun::Estimate] The estimate for the capture request. def estimate(url, options = {}, &block) options = with_options(options, &block) estimate = post("/v1/orders/estimate.json", options.merge(:url => url)) Capgun::Estimate.new(estimate['order']) end # Creates a capgun job # # @param [String] url A url that will be captured. # @param [Hash] options Additional options to the capture request. # @return [Capgun::Order] The capture request order. def capture(url, options = {}, &block) options = with_options(options, &block) order = post("/v1/orders.json", options.merge(:url => url)) Capgun::Order.new(order['order']) end # Query the order # # @param [String] order_id The id of a previously submitted order. # @return [Capgun::Order] The capture order. def order(order_id) order = get("/v1/orders/#{order_id}.json") Capgun::Order.new(order['order']) end # Query the status of a capture job. # # @param [String] job_id The id of a previously submitted capture job. # @return [Capgun::Job] The capture request is a job that encapsulates status. def status(job_id) job = get("/v1/jobs/#{job_id}.json") Capgun::Job.new(job['job']) end # Fetch account information # # @return [Capgun::Account] The account def account account = get("/v1/account.json") Capgun::Account.new(account['account']) end private def with_options(options, &block) if block_given? options = OpenStruct.new(options) yield(options) options = options.marshal_dump end options end end end