class Freemle::Client::Resource

Public Instance Methods

create(payload, &block) click to toggle source

Persists a resource on freemle.com, given a payload.

@example

customers.create(
  company_name: "Zilverline B.V.",
  address: {
    street: "Cruquiusweg 109 F",
    postal_code: "1019 AG",
    city: "Amsterdam",
    country_code: "NL"
  }
)

@example

customers.create(
  company_name: "Zilverline B.V.",
  address: {
    street: "Cruquiusweg 109 F",
    postal_code: "1019 AG",
    city: "Amsterdam",
    country_code: "NL"
  }
) do |response|
  # Roll your own response handler
end

@param [ Hash ] payload A hash containing the fields to set. @param [ Proc ] block A custom response handler.

@return [ Hash ] By default, a JSON parsed response body.

@since 1.0.0

# File lib/freemle/client/resource.rb, line 60
def create(payload, &block)
  block = default_handler unless block_given?
  request.post(json.generate({singular => payload}), &block)
end

Private Instance Methods

default_handler() click to toggle source

Returns a response handler, which parses the response body as JSON.

@return [ Proc ] Default response handler.

@since 1.0.0

# File lib/freemle/client/resource.rb, line 72
def default_handler
  Proc.new { |response| json.parse(response.body) }
end
json() click to toggle source

Returns the JSON library used in request and default response handling.

@return [ Class ] A JSON library.

@since 1.0.0

# File lib/freemle/client/resource.rb, line 94
def json
  return @json if @json
  require 'json'
  @json = JSON
end
request() click to toggle source

Returns a new request handler for this resource.

@return [ RestClient::Resource ] A request handler.

@since 1.0.0

# File lib/freemle/client/resource.rb, line 81
def request
  RestClient::Resource.new(
    "#{config.base_url}/#{plural}",
    user: config.app_name,
    password: config.api_key
  )
end