Skip to content

Commit

Permalink
Merge pull request #9 from nacyot/google
Browse files Browse the repository at this point in the history
Add Google link processor(for URL shortening)
  • Loading branch information
nacyot committed Sep 20, 2015
2 parents 738152a + 9ad819c commit b81c26c
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 6 deletions.
24 changes: 23 additions & 1 deletion Gemfile.lock
Expand Up @@ -2,6 +2,8 @@ PATH
remote: .
specs:
metalbird (0.0.1)
bitly
googl
oauth (~> 0.4)
tumblr_client (~> 0.8)
twitter (~> 5)
Expand All @@ -13,6 +15,10 @@ GEM
ast (2.1.0)
astrolabe (1.3.1)
parser (~> 2.2)
bitly (0.10.4)
httparty (>= 0.7.6)
multi_json (~> 1.3)
oauth2 (>= 0.5.0, < 2.0)
buftok (0.2.0)
coderay (1.1.0)
coveralls (0.8.2)
Expand All @@ -33,6 +39,9 @@ GEM
faraday (>= 0.7.4, < 0.10)
ffi (1.9.10)
formatador (0.2.5)
googl (0.7.1)
httparty (~> 0.10)
json (>= 1.4.6)
guard (2.13.0)
formatador (>= 0.2.4)
listen (>= 2.7, <= 4.0)
Expand All @@ -47,7 +56,7 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
http (0.9.6)
http (0.9.7)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 1.0.1)
Expand All @@ -56,7 +65,11 @@ GEM
domain_name (~> 0.5)
http-form_data (1.0.1)
http_parser.rb (0.6.0)
httparty (0.13.5)
json (~> 1.8)
multi_xml (>= 0.5.2)
json (1.8.3)
jwt (1.5.1)
listen (3.0.3)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
Expand All @@ -65,6 +78,8 @@ GEM
thread_safe (~> 0.3, >= 0.3.1)
method_source (0.8.2)
mime-types (2.6.2)
multi_json (1.11.2)
multi_xml (0.5.5)
multipart-post (2.0.0)
naught (1.1.0)
nenv (0.2.0)
Expand All @@ -73,13 +88,20 @@ GEM
nenv (~> 0.1)
shellany (~> 0.0)
oauth (0.4.7)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
parser (2.2.2.6)
ast (>= 1.1, < 3.0)
powerpack (0.1.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
rainbow (2.0.0)
rb-fsevent (0.9.6)
rb-inotify (0.9.5)
Expand Down
6 changes: 6 additions & 0 deletions bin/auth_google.rb
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require './lib/metalbird/authenticators/google.rb'

authenticator = Metalbird::Authenticator::Google.new
authenticator.authenticate
6 changes: 4 additions & 2 deletions bin/auth_twitter.rb
@@ -1,5 +1,7 @@
#!/usr/bin/env ruby

require './bin/authenticator'
require './lib/metalbird/authenticators/twitter.rb'

Metalbird::Authenticator::Twitter.authenticate
url = 'https://api.twitter.com'
authenticator = Metalbird::Authenticator::Twitter.new(url)
authenticator.authenticate
2 changes: 1 addition & 1 deletion bin/post_tweet.rb
@@ -1,7 +1,7 @@
require './lib/metalbird'
require 'pp'

Dotenv::Railtie.load
Dotenv.load

opts = {}
opts[:consumer_key] = ENV['TWITTER_CONSUMER_KEY']
Expand Down
9 changes: 9 additions & 0 deletions bin/shorten_url_bitly.rb
@@ -0,0 +1,9 @@
require 'dotenv'

require './lib/metalbird'

Dotenv.load

prosseor = Metalbird::UrlProcessor::Bitly.new(api_key: ENV['BITLY_API_KEY'])
puts prosseor.generate('https://github.com/nacyot')
# puts prosseor.expand('http://bit.ly/1NLH8zy')
8 changes: 8 additions & 0 deletions bin/shorten_url_google.rb
@@ -0,0 +1,8 @@
require 'dotenv'

require './lib/metalbird'

Dotenv.load

prosseor = Metalbird::UrlProcessor::Google.new(api_key: ENV['GOOGLE_API_KEY'])
puts prosseor.generate('https://github.com/nacyot')
1 change: 1 addition & 0 deletions lib/metalbird.rb
Expand Up @@ -5,6 +5,7 @@
module Metalbird
autoload :Twitter, 'metalbird/twitter'
autoload :Tumblr, 'metalbird/tumblr'
autoload :UrlProcessor, 'metalbird/url_processor'
autoload :Authenticator, 'metalbird/authenticator'

Logger = ::Logger.new(STDOUT)
Expand Down
10 changes: 8 additions & 2 deletions lib/metalbird/twitter/publish_args.rb
Expand Up @@ -12,13 +12,14 @@ class PublishArgs

attr_reader :links, :images, :errors

def initialize(data)
def initialize(data, link_processor = nil)
fail NoTweetError unless data[:tweet]

analyzed_tweet = analyze_tweet(data[:tweet])

@tweet = analyzed_tweet[:tweet]
@url_processor = link_processor || Metalbird::UrlProcessor::Default.new
@links = ((data[:links] || []) + analyzed_tweet[:links]).uniq
@links = process_links
@images = data[:images] || []
@errors = []
end
Expand Down Expand Up @@ -57,6 +58,10 @@ def link_count

private

def process_links
links.map { |link| @url_processor.generate(link) }
end

def validate_not_empty
is_valid = @tweet != ''
return true if is_valid
Expand Down Expand Up @@ -133,6 +138,7 @@ def max_length
end

class NoTweetError < StandardError; end

class EmptyTweetError < OpenStruct; end
class ExceedTweetLengthLimitError < OpenStruct; end
class NotValidImageError < OpenStruct; end
Expand Down
7 changes: 7 additions & 0 deletions lib/metalbird/url_processor.rb
@@ -0,0 +1,7 @@
module Metalbird
module UrlProcessor
autoload :Default, 'metalbird/url_processors/default'
autoload :Google, 'metalbird/url_processors/google'
autoload :Bitly, 'metalbird/url_processors/bitly'
end
end
45 changes: 45 additions & 0 deletions lib/metalbird/url_processors/bitly.rb
@@ -0,0 +1,45 @@
require 'bitly'

Bitly.use_api_version_3

module Metalbird
module UrlProcessor
class Bitly
RETRY_LIMIT = 3

attr_reader :client

def initialize(opts)
::Bitly.configure do |config|
config.api_version = 3
config.access_token = opts[:api_key]
end

@client = ::Bitly.client
end

def generate(link)
retry_counter ||= 0
@client.shorten(link).short_url
rescue => error
Metalbird::Logger.error(error)
retry_counter += 1
retry if retry_counter <= RETRY_LIMIT
raise URLShortenFailError
end

def expand(shorten_link)
retry_counter ||= 0
@client.expand(shorten_link).long_url
rescue => error
Metalbird::Logger.error(error)
retry_counter += 1
retry if retry_counter <= RETRY_LIMIT
raise URLExpandFailError
end
end

class URLShortenFailError < StandardError; end
class URLExpandFailError < StandardError; end
end
end
13 changes: 13 additions & 0 deletions lib/metalbird/url_processors/default.rb
@@ -0,0 +1,13 @@
module Metalbird
module UrlProcessor
class Default
def generate(link)
link
end

def expand(link)
link
end
end
end
end
39 changes: 39 additions & 0 deletions lib/metalbird/url_processors/google.rb
@@ -0,0 +1,39 @@
require 'googl'

module Metalbird
module UrlProcessor
class Google
RETRY_LIMIT = 3

attr_reader :client

def initialize(opts)
@client = Googl
@key = opts[:api_key]
end

def generate(link)
retry_counter ||= 0
Googl.shorten(link, nil, @key).short_url
rescue => error
Metalbird::Logger.error(error)
retry_counter += 1
retry if retry_counter <= RETRY_LIMIT
raise URLShortenFailError
end

def expand(shorten_link)
retry_counter ||= 0
Googl.expand(shorten_link, key: @key).long_url
rescue => error
Metalbird::Logger.error(error)
retry_counter += 1
retry if retry_counter <= RETRY_LIMIT
raise URLExpandFailError
end
end

class URLShortenFailError < StandardError; end
class URLExpandFailError < StandardError; end
end
end
2 changes: 2 additions & 0 deletions metalbird.gemspec
Expand Up @@ -32,6 +32,8 @@ Gem::Specification.new do |spec|
spec.add_dependency('twitter', '~> 5')
spec.add_dependency('oauth', '~> 0.4')
spec.add_dependency('tumblr_client', '~> 0.8')
spec.add_dependency('googl')
spec.add_dependency('bitly')

# Development Dependency
spec.add_development_dependency('guard')
Expand Down
Empty file.

0 comments on commit b81c26c

Please sign in to comment.