class Square::Connect::Node

Attributes

access_token[RW]
endpoint[RW]
identifier[RW]

Public Class Methods

new(*args) { |attributes| ... } click to toggle source
# File lib/square/connect/node.rb, line 6
def initialize(*args)
  attributes = args.extract_options!
  self.identifier = attributes[:id] || attributes[:identifier] || args.first
  self.access_token = tokenize attributes[:access_token] || args.second
  yield attributes if block_given?
  self.endpoint ||= endpoint_for identifier
end

Public Instance Methods

fetch() click to toggle source
# File lib/square/connect/node.rb, line 14
def fetch
  access_token_required!
  attributes = handle_response do
    access_token.get endpoint
  end
  self.class.new attributes.merge(
    access_token: access_token
  )
end

Private Instance Methods

access_token_required!() click to toggle source
# File lib/square/connect/node.rb, line 26
def access_token_required!
  raise Exception.new('access_token required') unless access_token
end
endpoint_for(*path_elements) click to toggle source
# File lib/square/connect/node.rb, line 30
def endpoint_for(*path_elements)
  File.join ROOT_URL, *path_elements.collect(&:to_s)
end
handle_error_response(response) click to toggle source
# File lib/square/connect/node.rb, line 57
def handle_error_response(response)
  error = parse_json response.body
  raise Error.new(response.status, error)
end
handle_response() { || ... } click to toggle source
# File lib/square/connect/node.rb, line 43
def handle_response
  response = yield
  case response.status
  when 200..201
    handle_success_response response
  else
    handle_error_response response
  end
end
handle_success_response(response) click to toggle source
# File lib/square/connect/node.rb, line 53
def handle_success_response(response)
  parse_json response.body
end
parse_json(raw_json) click to toggle source
# File lib/square/connect/node.rb, line 62
def parse_json(raw_json)
  json = MultiJson.load(raw_json)
  case json
  when Hash
    json.with_indifferent_access
  when Array
    json.collect(&:with_indifferent_access)
  else
    {}
  end
end
tokenize(access_token) click to toggle source
# File lib/square/connect/node.rb, line 34
def tokenize(access_token)
  case access_token
  when OAuth2::AccessToken
    access_token
  when String
    OAuth2::AccessToken.new access_token
  end
end