Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 27cf5d4b63
Fetching contributors…

Cannot retrieve contributors at this time

file 43 lines (37 sloc) 1.26 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
module LetsFreckle
  # ClientResource should be extended by resource classes in order to gain
  # access to HTTP methods.
  module ClientResource

    def get(resource, options = {})
      response = client.get do |request|
        request.url relative_path_for(resource), options
      end
      response.body.map { |mash| new(mash) }
    end

    def post(resource, options = {})
      client.post do |request|
        request.url relative_path_for(resource)
        request.headers['Content-Type'] = 'application/xml'
        request.body = options
      end
    end

    def base_api_url
      "https://#{LetsFreckle.config.account_host}.letsfreckle.com"
    end

    def relative_path_for(resource, page = nil)
      "/api/#{resource}.xml?page=#{page.nil? ? 1 : page}&token=#{LetsFreckle.config.token}"
    end

    private

    def client
      Faraday.new(:url => base_api_url) do |builder|
        builder.use Faraday::Request::UrlEncoded
        builder.use Faraday::Request::XML
        builder.use Faraday::Response::FlattenBody
        builder.use Faraday::Response::Mashify
        builder.use Faraday::Response::ParseXml
        builder.use Faraday::Response::VerifyStatus
        builder.use Faraday::Adapter::NetHttp
      end
    end
  end
end
Something went wrong with that request. Please try again.