class PX

Constants

METHOD_NAMES

Public Class Methods

new(args) click to toggle source

Set up the authroized url and parameters

# File lib/6px/client.rb, line 13
def initialize(args)
  user_id = args[:user_id]
  api_key = args[:api_key]
  api_secret = args[:api_secret]
  self.class.base_uri "https://api.6px.io/v1/users/#{user_id}"
  self.class.default_params key: api_key, secret: api_secret
  @inputs = {}
  @outputs = []
  @output = {}
  @methods = []
  @data = {}
  @callback = ''
end

Public Instance Methods

callback(callback) click to toggle source

Sets callbacks on job

# File lib/6px/client.rb, line 84
def callback(callback)
  @callback = callback
  self
end
data(data) click to toggle source

Sets additional data on job

# File lib/6px/client.rb, line 78
def data(data)
  @data = data
  self
end
inputs(inputs) click to toggle source

Sets images to that need to be processed

# File lib/6px/client.rb, line 52
def inputs(inputs)
  inputs.each do |k,v|
    @inputs[k] = encode_image(v)
  end

  self
end
jobs(*query) click to toggle source

For querying what jobs have been submitted

# File lib/6px/client.rb, line 28
def jobs(*query)
  query = query.first
  job_id = query.delete(:job_id) if query
  url = job_id ? "/jobs/#{job_id}" : "/jobs"
  query = clean_up_search_params(query) if query
  response = self.class.get(url, query: query).body
  JSON.parse(response)
end
output(images) click to toggle source
# File lib/6px/client.rb, line 37
def output(images)
  @outputs << @output unless @output.empty?

  @output = {
    ref: images,
    type: 'image/jpeg',
    url: '',
    methods: [],
    tag: ''
  }

  self
end
save() click to toggle source

Builds and sends the payload to 6px

# File lib/6px/client.rb, line 98
def save
  payload = build_payload.to_json
  empty_variables
  response = self.class.post("/jobs", body: payload)
  JSON.parse(response.body)
end
tag(tag) click to toggle source
# File lib/6px/client.rb, line 72
def tag(tag)
  @output[:tag] = tag
  self
end
type(type) click to toggle source

Sets what type the output should be in. Default is jpeg.

# File lib/6px/client.rb, line 61
def type(type)
  @output[:type] = type
  self
end
url(url) click to toggle source

Sets output url

# File lib/6px/client.rb, line 67
def url(url)
  @output[:url] = url
  self
end

Private Instance Methods

add_method(method, options) click to toggle source

Add methods to method hash

# File lib/6px/client.rb, line 137
def add_method(method, options)
  @output[:methods] << {
    'method' => method,
    'options' => options
  }
end
build_payload() click to toggle source

Builds the final payload to save

# File lib/6px/client.rb, line 145
def build_payload
  @outputs << @output

  payload = {
    input: @inputs,
    data: @data,
    callback: {
      url: @callback
    },
    output: @outputs
  }

  payload
end
clean_up_search_params(params) click to toggle source

Replaces underscores with periods in hash

# File lib/6px/client.rb, line 108
def clean_up_search_params(params)
  parsed_hash = {}
  params.each {|i,k| parsed_hash[i.to_s.gsub('_', '.')] = k }
  parsed_hash
end
empty_variables() click to toggle source

Resets instance variables after request is sent

# File lib/6px/client.rb, line 161
def empty_variables
  @inputs = {}
  @methods = []
  @data = {}
  @callback = ''
  @outputs = []
  @output = {}
end
encode_image(url) click to toggle source

Encodes the images in Base64 if they are not hosted

# File lib/6px/client.rb, line 115
def encode_image(url)
  return url if URI.parse(url).scheme

  raw_image = open(url, 'r').read
  image_type = image_type(url)
  encoded_image = Base64.encode64(raw_image)

  "data:#{image_type};base64,#{encoded_image}"
end
image_type(path) click to toggle source
# File lib/6px/client.rb, line 125
def image_type(path)
  png = Regexp.new("\x89PNG".force_encoding("binary"))
  jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
  jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
  case IO.read(path, 10)
  when /^GIF8/ then 'image/gif'
  when /^#{png}/ then 'image/png'
  when /^#{jpg}/, /^#{jpg2}/ then 'image/jpg'
  end
end