Sha256: 4d6f578a8494905edf34e03f4c0b83f3118875d0090f6bbb040cc4165da4b53d

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

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)
      "/api/#{resource}.xml?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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
letsfreckle-client-0.2.1 lib/letsfreckle/client_resource.rb
letsfreckle-client-0.2.0 lib/letsfreckle/client_resource.rb