bitly_exporter 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95a9394ccb7997614591c04f4cbbcab2a0de051c
4
+ data.tar.gz: abda5797e29b1964f2d9253a4e7bc3af3419cc59
5
+ SHA512:
6
+ metadata.gz: b1098b6ce86fa57019114c8587d5b46c29905c23600e79c57a56ddbce8a96d7c7013a3eeab2ff86fd760884bd6172c141634a7ce71189e82e9702117e0436700
7
+ data.tar.gz: 18287adf2a886dd353a888a0ab0fb4efb950f45bc0ebabb8b279ba12fd19c112edf592be7ea9575377074523e3ca1eb6a4b05fdecc673eede43c4cdf3f49c828
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ pkg/
3
+ Gemfile.lock
4
+ coverage/
5
+ .coveralls.yml
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Aaron Ortbals
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ Bitly Exporter
2
+ ==============
3
+
4
+ [![Code Climate](https://codeclimate.com/github/aortbals/bitly_exporter.png)](https://codeclimate.com/github/aortbals/bitly_exporter) [![Coverage Status](https://coveralls.io/repos/aortbals/bitly_exporter/badge.png?branch=master)](https://coveralls.io/r/aortbals/bitly_exporter?branch=master)
5
+
6
+ This gem exports a user's links from the Bitly V3 API. It requires a generic OAuth access token that you can [obtain from Bitly](https://bitly.com/a/oauth_apps). It makes use of the [User Link History API](http://dev.bitly.com/user_info.html#v3_user_link_history).
7
+
8
+
9
+ Installation
10
+ ------------
11
+
12
+ Add this line to your applications Gemfile:
13
+
14
+ gem 'bitly_exporter', github: 'aortbals/bitly_exporter'
15
+
16
+ Or install it directly:
17
+
18
+ gem install bitly_exporter
19
+
20
+
21
+ Usage
22
+ -----
23
+
24
+ First, create a user and an exporter:
25
+
26
+ user = BitlyExporter::User.new('youroauthaccesstoken')
27
+ exporter = BitlyExporter::Exporter.new(user)
28
+
29
+ The `export` method triggers the export. There are two ways to consume the results.
30
+
31
+ Using a block:
32
+
33
+ ```ruby
34
+ exporter.export do |link|
35
+ # Create a record, store the link, etc
36
+ end
37
+ ```
38
+
39
+ As a traditional method:
40
+
41
+ ```ruby
42
+ results = exporter.export
43
+
44
+ results.each_with_index do |link, index|
45
+ puts "Link Index: #{index}, Link: #{link.link}"
46
+ end
47
+ ```
48
+
49
+ `export` takes two optional arguments: `progress` & `max`. Set `progress` to `true` to see progress while exporting. Setting `max` will import (in increments of 100) up to the maximum specified. This is a good way to see a sample response.
50
+
51
+ Show progress:
52
+
53
+ ```ruby
54
+ results = exporter.export(true)
55
+ ```
56
+
57
+ With max:
58
+
59
+ ```ruby
60
+ exporter.export(false, 200) do |link|
61
+ # Create a record, store the link, etc
62
+ end
63
+ ```
64
+
65
+ #### Link Objects ####
66
+
67
+ See the [API docs](http://dev.bitly.com/user_info.html#v3_user_link_history) for more information. A link object has the following methods:
68
+
69
+ - link (short_link)
70
+ - long_url
71
+ - aggregate\_link (global\_bitly\_identifier)
72
+ - archived
73
+ - title
74
+ - private
75
+ - client_id
76
+ - created_at
77
+ - modified_at
78
+ - user_ts
79
+
80
+
81
+ Contributing
82
+ ------------
83
+
84
+ 1. Fork it
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Write Tests
88
+ 5. Push to the branch (`git push origin my-new-feature`)
89
+ 6. Create new Pull Request
90
+
91
+
92
+ License
93
+ -------
94
+
95
+ This code is provided under the MIT license. See LICENSE for more details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bitly_exporter/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "bitly_exporter"
8
+ gem.version = BitlyExporter::VERSION
9
+ gem.authors = ["Aaron Ortbals"]
10
+ gem.email = ["me@aaronortbals.com"]
11
+ gem.description = %q{Export a user's links from Bitly.}
12
+ gem.summary = %q{Supports Bitly API V3}
13
+ gem.homepage = "https://github.com/aortbals/bitly_exporter"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'vcr'
23
+ gem.add_development_dependency 'webmock'
24
+ gem.add_development_dependency 'coveralls'
25
+
26
+ gem.add_dependency 'httparty'
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ module BitlyExporter
5
+ class Client
6
+ include HTTParty
7
+
8
+ API_URL = "https://api-ssl.bitly.com/v3"
9
+ API_VERSION = 3
10
+
11
+ attr_reader :oauth_token, :params
12
+ def initialize(oauth_token)
13
+ @oauth_token = oauth_token
14
+ @params = { access_token: oauth_token }
15
+ end
16
+
17
+ protected
18
+
19
+ def make_request(endpoint, options={})
20
+ response = HTTParty.get("#{API_URL}#{endpoint}", query: params.merge(options))
21
+ if response.code == 200
22
+ JSON.parse(response.body)
23
+ else
24
+ raise StandardError, "The request to bitly failed with a HTTP #{response.code}\n#{response.message}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module BitlyExporter
2
+ class Exporter
3
+ LIMIT = 100
4
+ NO_MAX = Float::INFINITY
5
+
6
+ attr_reader :user
7
+ def initialize(user)
8
+ @user = user
9
+ end
10
+
11
+ def export(progress=false, max=NO_MAX)
12
+ results = []
13
+ offset = 0
14
+ begin
15
+ links, result_count = user.link_history(limit: LIMIT, offset: offset)
16
+ links.each do |link|
17
+ results << link
18
+ yield link if block_given?
19
+ end
20
+ offset = offset + LIMIT
21
+ sleep(0.5) # Let's not bull rush the API
22
+ print "#{offset} links retrieved...\r" if progress
23
+ end while links.count > 0 && offset < max
24
+ print "Finished. #{results.count} links retrieved." if progress
25
+ results
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module BitlyExporter
2
+ class Link
3
+ attr_reader :link, :long_url, :aggregate_link, :title, :private, :created_at, :modified_at
4
+ alias_method :short_link, :link
5
+ alias_method :global_bitly_identifier, :aggregate_link
6
+
7
+ def initialize(data={})
8
+ if data
9
+ @link = data["link"]
10
+ @long_url = data["long_url"]
11
+ @aggregate_link = data["aggregate_link"]
12
+ @archived = data["archived"]
13
+ @title = data["title"]
14
+ @private = data["private"]
15
+ @client_id = data["client_id"]
16
+ @created_at = Time.at(data["created_at"]) if data["created_at"]
17
+ @modified_at = Time.at(data["modified_at"]) if data["modified_at"]
18
+ @user_ts = Time.at(data["user_ts"]) if data["user_ts"]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module BitlyExporter
2
+ class User < Client
3
+ def link_history(options={})
4
+ result = make_request("/user/link_history", options)
5
+ result_count = result["data"]["result_count"]
6
+ result_links = result["data"]["link_history"]
7
+
8
+ links = result_links.collect do |link|
9
+ Link.new(link)
10
+ end
11
+
12
+ return links, result_count
13
+ end
14
+
15
+ def info(options={})
16
+ make_request("/user/info")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module BitlyExporter
2
+ VERSION = "0.0.1.pre"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "bitly_exporter/version"
2
+ require "bitly_exporter/link"
3
+ require "bitly_exporter/client"
4
+ require "bitly_exporter/user"
5
+ require "bitly_exporter/exporter"
6
+
7
+ module BitlyExporter
8
+ end