~tim/wuparty

d11fe78e83317c10a6dbf09cefea87bc3f65ba1a — Aaron Oman 10 years ago 4724ac3
Add webhook put method and DRY-up the HTTP verbs.
1 files changed, 23 insertions(+), 20 deletions(-)

M lib/wuparty.rb
M lib/wuparty.rb => lib/wuparty.rb +23 -20
@@ 128,6 128,10 @@ class WuParty
    end
  end

  def webhook(form_id, url)
    WuParty.put("forms/#{form_id}/webhook", :party => self, :body => {'url' => url})
  end

  # Returns details about the specified report.
  def report(report_id)
    if r = get("reports/#{report_id}")['Reports']


@@ 136,29 140,15 @@ class WuParty
  end

  def get(method, options={}) # :nodoc:
    options.merge!(:basic_auth => {:username => @api_key})
    url = "#{base_url}/#{method}.json"
    result = self.class.get(url, options)
    if result.is_a?(String)
      raise ConnectionError, result
    elsif result['HTTPCode']
      raise HTTPError.new(result['HTTPCode'], result['Text'])
    else
      result
    end
    handle_http_verb(:get, method, options)
  end

  def post(method, options={}) # :nodoc:
    options.merge!(:basic_auth => {:username => @api_key})
    url = "#{base_url}/#{method}.json"
    result = self.class.post(url, options)
    if result.is_a?(String)
      raise ConnectionError, result
    elsif result['HTTPCode']
      raise HTTPError.new(result['HTTPCode'], result['Text'])
    else
      result
    end
    handle_http_verb(:post, method, options)
  end

  def put(method, options={}) # :nodoc:
    handle_http_verb(:put, method, options)
  end

  private


@@ 167,6 157,19 @@ class WuParty
      ENDPOINT % @account
    end

    def handle_http_verb(verb, action, options={})
      options.merge!(:basic_auth => {:username => @api_key})
      url = "#{base_url}/#{action}.json"
      result = self.class.send(verb, url, options)
      if result.is_a?(String)
        raise ConnectionError, result
      elsif result['HTTPCode']
        raise HTTPError.new(result['HTTPCode'], result['Text'])
      else
        result
      end
    end

  public

  # ----------