Skip to content

Commit

Permalink
Merge pull request #104 from jekyll/behance-importer
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Dec 17, 2013
2 parents 3f5144b + 0874485 commit 42510f5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions jekyll-import.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('mysql', "~> 2.8")
s.add_development_dependency('pg', "~> 0.12")
s.add_development_dependency('mysql2', "~> 0.3")
s.add_development_dependency('behance', "~> 0.3.0")

# = MANIFEST =
s.files = %w[
Expand Down
78 changes: 78 additions & 0 deletions lib/jekyll-import/importers/behance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module JekyllImport
module Importers
class Behance < Importer
def self.require_deps
JekyllImport.require_with_fallback(%w[
fileutils
safe_yaml
date
time
behance
])
end

def self.specify_options(c)
c.option 'user', '--user NAME', 'The username of the account'
c.option 'api_token', '--api_token TOKEN', 'The API access token for the account'
end

def self.validate(options)
%w[user api_token].each do |option|
if options[option].nil?
abort "Missing mandatory option --#{option}."
end
end
end

# Process the import.
#
# user - the behance user to retrieve projects (ID or username)
# api_token - your developer API Token
#
# Returns nothing.
def self.process(options)
user = options.fetch('user')
token = options.fetch('api_token')

projects = fetch_projects(token, user)

puts "#{projects.length} project(s) found. Importing now..."

projects.each do |project|

details = client.project(project['id'])
title = project['name'].to_s
formatted_date = Time.at(project['published_on'].to_i).to_date.to_s

post_name = title.split(%r{ |!|/|:|&|-|$|,}).map do |character|
character.downcase unless character.empty?
end.compact.join('-')

name = "#{formatted_date}-#{post_name}"

header = {
"layout" => "post",
"title" => title,
"project" => details
}

FileUtils.mkdir_p("_posts")

File.open("_posts/#{name}.md", "w") do |f|
f.puts header.to_yaml
f.puts "---\n\n"
f.puts details['description']
end
end

puts "Finished importing."
end

private

def self.fetch_projects(token, user)
Behance::Client.new(access_token: token).user_projects(user)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/jekyll/commands/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Jekyll
module Commands
class Import < Command
IMPORTERS = {
:behance => 'Behance',
:csv => 'CSV',
:drupal6 => 'Drupal6',
:drupal7 => 'Drupal7',
Expand All @@ -24,7 +25,7 @@ class Import < Command
:tumblr => 'Tumblr',
:typo => 'Typo',
:wordpress => 'WordPress',
:wordpressdotcom => 'WordpressDotCom',
:wordpressdotcom => 'WordpressDotCom'
}

def self.abort_on_invalid_migrator(migrator)
Expand Down

0 comments on commit 42510f5

Please sign in to comment.