class JsonClient::Base

Constants

J
RS

Attributes

client_id[R]
secret_key[R]
uri_builder[R]

Public Class Methods

new(uri_builder, config) click to toggle source
# File lib/json_client/base.rb, line 37
def initialize(uri_builder, config)
  @client_id = config[:client_id]
  @secret_key = config[:secret_key]
  @uri_builder = uri_builder
  validate_variables
end

Public Instance Methods

create(model) click to toggle source
# File lib/json_client/base.rb, line 52
def create(model)
  result(:create, model)
end
destroy(id) click to toggle source
# File lib/json_client/base.rb, line 60
def destroy(id)
  result(:destroy, nil, id)
end
index() click to toggle source
# File lib/json_client/base.rb, line 44
def index
  result(:index, nil)
end
show(id) click to toggle source
# File lib/json_client/base.rb, line 48
def show(id)
  result(:show, nil, id)
end
update(id, model) click to toggle source
# File lib/json_client/base.rb, line 56
def update(id, model)
  result(:update, model, id)
end

Protected Instance Methods

auth_params() click to toggle source
# File lib/json_client/base.rb, line 93
def auth_params
  {
    client_id: client_id,
    secret_key: secret_key
  }
end
build_request(protocol, model, id = nil) click to toggle source
# File lib/json_client/base.rb, line 76
def build_request(protocol, model, id = nil)
  ## Get URL path
  uri = uri_builder.uri(id)
  requester = requestors.for(protocol)

  ## Find serializer for model
  serializer = serializers.for(protocol)
  params = serializer.serialize(model)

  ## Build the request object
  requester.new(uri, auth_params, params)
end
fetch_response(protocol, model, id = nil) click to toggle source
# File lib/json_client/base.rb, line 89
def fetch_response(protocol, model, id = nil)
  build_request(protocol, model, id).fetch
end
result(protocol, model, id = nil) click to toggle source
# File lib/json_client/base.rb, line 66
def result(protocol, model, id = nil)
  responder = responders.for(protocol)

  ## Fetch the response for protocol
  response = fetch_response(protocol, model, id)

  ## Create new response
  responder.new(response)
end

Private Instance Methods

validate_variables() click to toggle source
# File lib/json_client/base.rb, line 102
def validate_variables
  %w(uri_builder client_id secret_key).each do |name|
    fail "#{name} must be set" if (send name).nil?
  end
end