class Pin::API::Client

Constants

BASE_URL

Attributes

http[RW]

Public Class Methods

new() click to toggle source
# File lib/pin/api/client.rb, line 24
def initialize
  @url = URI.parse(BASE_URL)
  @http = Net::HTTP.new(@url.host, @url.port)
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end

Private Instance Methods

api_call(method, endpoint, params={}, format=:json) click to toggle source
# File lib/pin/api/client.rb, line 38
def api_call(method, endpoint, params={}, format=:json)

  # dispatch to the right method, with the full path (/api/v2 + endpoint)
  request = self.send("format_#{method}", "#{@url.path}/#{endpoint}", params)
  response = @http.request(request)

  unless response.code =~ /^2\d\d$/
    raise Pin::Error.new response.code, response.body
  end

  if format == :json
    return JSON.parse(response.body)
  end

  response.body
end
format_get(path, params) click to toggle source
# File lib/pin/api/client.rb, line 55
def format_get(path, params)
  unless params.nil?
    query = params.map { |k, v| "#{k}=#{URI::escape(v.to_s)}" }.join("&")
  end

  request = Net::HTTP::Get.new("#{path}?#{query}")
  request.basic_auth(Pin.config.secret_key, '')

  request
end
format_post(path, params) click to toggle source
# File lib/pin/api/client.rb, line 66
def format_post(path, params)
  request = Net::HTTP::Post.new(path)

  request.set_form_data(to_form_data(params))
  request.basic_auth(Pin.config.secret_key, '')

  request
end
format_put(path, params) click to toggle source
# File lib/pin/api/client.rb, line 75
def format_put(path, params)
  request = Net::HTTP::Put.new(path)

  request.set_form_data(to_form_data(params))
  request.basic_auth(Pin.config.secret_key, '')

  request
end
get(klass, params={}) click to toggle source
# File lib/pin/api/client.rb, line 33
def get(klass, params={})
  raw_response = api_call(:get, klass.name.demodulize.downcase.pluralize, params)
  pin_response(raw_response, raw_response['response'].map { |e| klass.new(e) })
end
pin_response(raw_body, objects) click to toggle source
# File lib/pin/api/client.rb, line 105
def pin_response(raw_body, objects)
  if raw_body['pagination'].nil?
    if objects.is_a? Array
      {:response => objects}
    else
      objects
    end
  else
    {
        :pagination => {
            :current => raw_body['pagination']['current'],
            :previous => raw_body['pagination']['previous'],
            :next => raw_body['pagination']['next'],
            :per_page => raw_body['pagination']['per_page'],
            :pages => raw_body['pagination']['pages'],
            :count => raw_body['pagination']['count']
        },
        :response => objects
    }
  end
end
require_field(field, message=nil) click to toggle source
# File lib/pin/api/client.rb, line 127
def require_field(field, message=nil)
  return unless field.nil?

  if field.respond_to?(:empty?)
    return unless field.empty?
  end

  if message.blank?
    raise 'Required field is missing'
  else
    raise message.is_a?(Symbol) ? "Required field #{message.to_s} is missing" : message
  end
end
to_form_data(params) click to toggle source

This is by no means a generic method of flattening a hash into form params and will work for 1 level depth - specifically for providing a ‘card’ hash for a number of POSTS specific to Pin.

# File lib/pin/api/client.rb, line 87
def to_form_data(params)
  form_data = {}

  params.each do |key, value|
    if value.is_a? Hash
      prefix = key.to_s

      value.each do |key, value|
        form_data["#{prefix}[#{key.to_s}]"] = value
      end
    else
      form_data[key.to_s] = value
    end
  end

  form_data
end