Skip to content

Instantly share code, notes, and snippets.

@axsuul
Created September 12, 2014 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axsuul/754b8fe46af88cb073f3 to your computer and use it in GitHub Desktop.
Save axsuul/754b8fe46af88cb073f3 to your computer and use it in GitHub Desktop.
class Front
def initialize
@username = Rails.application.secrets.front_login
@password = Rails.application.secrets.front_password
end
def update_contact(attributes = {})
attributes.symbolize_keys!
attributes.reverse_merge!(
name: nil,
description: "",
emails: [],
phones: [],
links: []
)
params = {
name: attributes[:name],
description: attributes[:description],
fields: [],
link: []
}
build_field = ->(source, handle) do
params[:fields] << {
source: source.to_s,
handle: handle
}
end
attributes[:emails].each { |e| build_field.call(:email, e) }
attributes[:phones].each { |e| build_field.call(:phone, e) }
attributes[:links].each do |link|
params[:link] << { url: link }
end
request(:post, "contacts", params)
end
private
def request(method, endpoint, params = {})
JSON.parse(resource(endpoint).send(method, params.to_json, content_type: :json, accept: :json))
end
def resource(endpoint)
RestClient::Resource.new("https://#{@username}:#{@password}@api.frontapp.com/api/1/#{endpoint}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment