filepreviews 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/Changelog.md +11 -1
- data/README.md +64 -10
- data/lib/filepreviews.rb +4 -1
- data/lib/filepreviews/cli.rb +5 -0
- data/lib/filepreviews/config.rb +27 -0
- data/lib/filepreviews/http.rb +21 -8
- data/lib/filepreviews/version.rb +1 -1
- data/rubocop.yml +4 -1
- data/spec/filepreviews/config_spec.rb +24 -0
- data/spec/filepreviews/filepreviews_spec.rb +37 -7
- data/spec/filepreviews/http_spec.rb +19 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6eb1b65fbd4cf5f20021e4950f0d4df73d015591
|
4
|
+
data.tar.gz: 227b55edc016cbbf490beb865ac33268acfbbaf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a30644834d46ef94d4f769202cf466442c8e7089fba78407836ae791c7ee14ac9b875d2396b510639d14ffb28d81bb3a59b1f2ef32a45f5ff8d90da1cb66052
|
7
|
+
data.tar.gz: 74645e901e21768c4f4f9c060e86cb1220d356896fe3828d97d0f667eed7a55c3395cb9902cbd302587eca225027fe0f07d596a1cfdc12a7fbca45fcae03d7b6
|
data/.travis.yml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
|
4
|
-
|
3
|
+
- 2.1.1
|
4
|
+
- 2.0.0
|
5
5
|
script: bundle exec rake test
|
6
6
|
notifications:
|
7
7
|
email: false
|
@@ -10,3 +10,6 @@ notifications:
|
|
10
10
|
on_failure: always
|
11
11
|
channels:
|
12
12
|
- irc.freenode.org#rubyonadhd
|
13
|
+
env:
|
14
|
+
global:
|
15
|
+
secure: ifm8+ChcdXB9BkuzordGulaZbkhhJzZpiqh8JnzgAg+KRPr/riEMQOi+wKAaJMyHobmQjPWbkUoeTaR4vMi/Sdf1sH2rc4NZHHInrfq6fjhNPI8cpykInhTb7hymBCZCQgXHzJKlnULWwJat7JNXrftq/PXQgWRVGiWmFbbNTw8=
|
data/Changelog.md
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
# Filepreviews Changelog
|
2
2
|
|
3
|
+
## 1.1.0
|
4
|
+
|
5
|
+
Released Jun 16, 2014 ([1.1.0](https://github.com/jonahoffline/filepreviews-ruby/tree/v1.1.0)).
|
6
|
+
|
7
|
+
* Update API url to use new endpoint
|
8
|
+
* Add `Filepreviews::Config` module for passing `api_key` and other future options
|
9
|
+
* Add `-k, --api-key` arguments and description for setting `api_key` in the `CLI`
|
10
|
+
* Update README with documentation for API key configuration, usage and CLI examples
|
11
|
+
* Update version to 1.1.0
|
12
|
+
|
3
13
|
## 1.0.0
|
4
14
|
|
5
15
|
Released May 17, 2014 ([1.0.0](https://github.com/jonahoffline/filepreviews-ruby/tree/v1.0.0)).
|
6
16
|
|
7
|
-
* Initial Release
|
17
|
+
* Initial Release
|
data/README.md
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
# FilePreviews.io (Ruby client)
|
2
|
-
|
2
|
+
[![Build Status](https://travis-ci.org/jonahoffline/filepreviews-ruby.svg)](https://travis-ci.org/jonahoffline/filepreviews-ruby)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/filepreviews.svg)](http://badge.fury.io/rb/filepreviews)
|
4
|
+
[![Dependency Status](https://gemnasium.com/jonahoffline/filepreviews-ruby.svg)](https://gemnasium.com/jonahoffline/filepreviews-ruby)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/jonahoffline/filepreviews-ruby.png)](https://codeclimate.com/github/jonahoffline/filepreviews-ruby)
|
6
|
+
[![Inline docs](http://inch-pages.github.io/github/jonahoffline/filepreviews-ruby.png)](http://inch-pages.github.io/github/jonahoffline/filepreviews-ruby)
|
7
|
+
[![Gitter chat](https://badges.gitter.im/jonahoffline/filepreviews-ruby.png)](https://gitter.im/jonahoffline/filepreviews-ruby)
|
3
8
|
|
4
|
-
|
9
|
+
This is the ruby client library for the **Demo API** of [FilePreviews.io](http://filepreviews.io) service. A lot more to come very soon.
|
5
10
|
|
6
11
|
## Installation
|
7
12
|
|
@@ -18,10 +23,31 @@ Or install it yourself as:
|
|
18
23
|
$ gem install filepreviews
|
19
24
|
|
20
25
|
## Usage
|
26
|
+
You can currently use the Filepreviews.io API through this gem without registering your application. However, this may change anytime in the future.
|
27
|
+
|
28
|
+
For additional features and greater customization, register your application for an API key at [Filepreviews.io](http://bit.ly/filepreviews-signup)
|
21
29
|
|
22
|
-
###
|
30
|
+
### Configuration
|
31
|
+
To configure the gem to use your newly-registered `api_key`, you can use one of the two configuration styles:
|
32
|
+
|
33
|
+
Block style:
|
23
34
|
```ruby
|
35
|
+
require 'filepreviews'
|
24
36
|
|
37
|
+
Filepreviews.configure do |config|
|
38
|
+
config.api_key = 'YOUR_API_KEY'
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Simpler style:
|
43
|
+
```ruby
|
44
|
+
require 'filepreviews'
|
45
|
+
|
46
|
+
Filepreviews.api_key = 'YOUR_API_KEY'
|
47
|
+
```
|
48
|
+
|
49
|
+
### Basic Example Code
|
50
|
+
```ruby
|
25
51
|
require 'filepreviews'
|
26
52
|
|
27
53
|
url = 'http://pixelhipsters.com/images/pixelhipster_cat.png'
|
@@ -32,13 +58,21 @@ result.metadata_url
|
|
32
58
|
result.metadata
|
33
59
|
```
|
34
60
|
|
35
|
-
####
|
36
|
-
You can optinally send an options object.
|
37
|
-
|
61
|
+
#### Web Page Screencaptures
|
38
62
|
```ruby
|
63
|
+
url = 'http://pixelhipsters.com'
|
64
|
+
result = Filepreviews.generate(url)
|
39
65
|
|
40
|
-
|
66
|
+
result.preview_url
|
67
|
+
result.metadata_url
|
68
|
+
result.metadata
|
69
|
+
```
|
41
70
|
|
71
|
+
|
72
|
+
#### Options
|
73
|
+
You can optionally send an options object (per request).
|
74
|
+
|
75
|
+
```ruby
|
42
76
|
options = {
|
43
77
|
size: {
|
44
78
|
width: 100,
|
@@ -57,13 +91,33 @@ result.metadata
|
|
57
91
|
### Command-Line Application
|
58
92
|
Options:
|
59
93
|
|
60
|
-
* -
|
61
|
-
* -
|
62
|
-
* -
|
94
|
+
* -k, --api_key [key] - use API key from Filepreviews.io
|
95
|
+
* -m, --metadata - load metadata response
|
96
|
+
* -v, --version - display the version
|
97
|
+
* -h, --help - print help
|
98
|
+
|
99
|
+
### Command-Line usage examples
|
100
|
+
|
101
|
+
#### Basic use
|
102
|
+
$ filepreviews http://www.pixelhipsters.com
|
103
|
+
|
104
|
+
#### With an API Key
|
105
|
+
$ filepreviews --api_key YOUR_API_KEY_HERE http://www.pixelhipsters.com
|
106
|
+
|
107
|
+
#### Autoload Full (metadata) Response
|
108
|
+
$ filepreviews -m http://pixelhipsters.com/images/pixelhipster_cat.png
|
109
|
+
|
110
|
+
**Note**: This will return a full metadata response, instead of the API's original one that returns the `metadata_url` and `preview_url` urls.
|
111
|
+
|
63
112
|
|
64
113
|
## Author
|
65
114
|
* [Jonah Ruiz](http://www.pixelhipsters.com)
|
66
115
|
|
116
|
+
## Discussion
|
117
|
+
If you have any questions, ideas or jokes:
|
118
|
+
|
119
|
+
[![Gitter chat](https://badges.gitter.im/jonahoffline/filepreviews-ruby.png)](https://gitter.im/jonahoffline/filepreviews-ruby)
|
120
|
+
|
67
121
|
## Contributing
|
68
122
|
|
69
123
|
Is it worth it? let me fork it
|
data/lib/filepreviews.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'filepreviews/version'
|
3
|
+
require 'filepreviews/config'
|
3
4
|
require 'filepreviews/utils'
|
4
5
|
require 'filepreviews/http'
|
5
6
|
require 'filepreviews/response'
|
@@ -8,6 +9,8 @@ require 'ostruct'
|
|
8
9
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
9
10
|
# Main module for FilePreviews.io library
|
10
11
|
module Filepreviews
|
12
|
+
include Filepreviews::Config
|
13
|
+
|
11
14
|
# Facade method to call API response
|
12
15
|
# @param url [String] image url to convert
|
13
16
|
# @param options [Hash<Symbol>] :metada and :image options
|
@@ -33,7 +36,7 @@ module Filepreviews
|
|
33
36
|
# Default options to be used in API request
|
34
37
|
# @return [Hash<symbol>] default options
|
35
38
|
def self.default_options
|
36
|
-
{ debug:
|
39
|
+
{ debug: false, metadata: ['all'] }
|
37
40
|
end
|
38
41
|
|
39
42
|
# Merges metadata options with supported formats
|
data/lib/filepreviews/cli.rb
CHANGED
@@ -24,6 +24,11 @@ MSG
|
|
24
24
|
opts.banner = BANNER
|
25
25
|
opts.set_program_name 'Filepreviews.io'
|
26
26
|
|
27
|
+
opts.on('-k', '--api_key [key]', String,
|
28
|
+
'use API key from Filepreviews.io') do |key|
|
29
|
+
Filepreviews.api_key = key
|
30
|
+
end
|
31
|
+
|
27
32
|
opts.on('-m', '--metadata', 'load metadata response') do
|
28
33
|
@metadata = true
|
29
34
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Filepreviews
|
2
|
+
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
3
|
+
# Configurable module for API key and options
|
4
|
+
module Config
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
# @!attribute api_key
|
10
|
+
# @return [String] API key to be used
|
11
|
+
attr_accessor :api_key
|
12
|
+
|
13
|
+
# Configures api_key and options
|
14
|
+
# Usage example:
|
15
|
+
# Filepreviews.configure do |config|
|
16
|
+
# config.api_key = 'your_api_key_here'
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Alternate way:
|
20
|
+
# Filepreviews.api_key = ENV['YOUR_API_KEY']
|
21
|
+
#
|
22
|
+
# @param <api_key> [String] api key to use
|
23
|
+
def configure
|
24
|
+
yield self if block_given?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/filepreviews/http.rb
CHANGED
@@ -7,28 +7,41 @@ module Filepreviews
|
|
7
7
|
# @author Jonah Ruiz <jonah@pixelhipsters.com>
|
8
8
|
# Contains http helper module
|
9
9
|
module HTTP
|
10
|
-
API_URL = 'https://
|
10
|
+
API_URL = 'https://api.filepreviews.io/v1/'
|
11
11
|
USER_AGENT = "Filepreviews-Rubygem/#{Filepreviews::VERSION}"
|
12
12
|
|
13
13
|
include Filepreviews::Utils
|
14
14
|
|
15
15
|
module_function
|
16
16
|
|
17
|
-
# TODO: Fix logger
|
18
17
|
# Returns custom Typhoeus connection configuration
|
19
18
|
# @param url [String] API url to be used as base
|
20
|
-
# @param
|
21
|
-
# @return [Typhoeus::Connection] http client for requests to API
|
22
|
-
def default_connection(url = API_URL,
|
23
|
-
# _logger = debug ? :logger : false
|
24
|
-
|
19
|
+
# @param debug [Boolean] flag to log responses into STDOUT
|
20
|
+
# @return [Typhoeus::Connection] configured http client for requests to API
|
21
|
+
def default_connection(url = API_URL, debug = false)
|
25
22
|
Faraday.new(url: url) do |conn|
|
26
23
|
conn.adapter :typhoeus
|
27
|
-
# conn.response _logger
|
28
24
|
conn.headers[:user_agent] = USER_AGENT
|
25
|
+
configure_api_auth_header(conn.headers)
|
26
|
+
configure_logger(conn) if debug
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Configures API Authentication header
|
31
|
+
# @param connection_headers [Faraday::Connection] header block
|
32
|
+
# @return [Faraday::Connection] 'X-API-KEY' header
|
33
|
+
def configure_api_auth_header(connection_headers)
|
34
|
+
if (api_key = Filepreviews.api_key)
|
35
|
+
connection_headers['X-API-KEY'] = api_key
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|
39
|
+
# Configures logger
|
40
|
+
# @param connection [Faraday::Connection] connection block
|
41
|
+
def configure_logger(connection)
|
42
|
+
connection.response :logger
|
43
|
+
end
|
44
|
+
|
32
45
|
# Returns processed metadata, and image attributes params
|
33
46
|
# @param params [Hash<Symbol>] metadata and image attributes
|
34
47
|
# @return [Hash<Symbol>] processed parameters
|
data/lib/filepreviews/version.rb
CHANGED
data/rubocop.yml
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Dummy class for testing ::Config module
|
4
|
+
class Kawaii
|
5
|
+
extend Filepreviews::Config
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Filepreviews::Config do
|
9
|
+
let(:config) { Kawaii }
|
10
|
+
|
11
|
+
describe '.api_key' do
|
12
|
+
it 'sets api key' do
|
13
|
+
config.api_key = '666'
|
14
|
+
expect(config.api_key).to eq('666')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.configure' do
|
19
|
+
it 'sets the api_key when used with a block' do
|
20
|
+
config.configure { |conf| conf.api_key = 'kawaii!' }
|
21
|
+
expect(config.api_key).to eq('kawaii!')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -4,18 +4,48 @@ describe Filepreviews do
|
|
4
4
|
let(:file_previews) { Filepreviews }
|
5
5
|
let(:sample_img) { 'http://pixelhipsters.com/images/pixelhipster_cat.png' }
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
expect(file_previews.generate(sample_img))
|
10
|
-
.to be_an_instance_of(Filepreviews::Response)
|
11
|
-
end
|
7
|
+
it 'keeps it real, you feels me dawg?' do
|
8
|
+
expect(true).to eq(true)
|
12
9
|
end
|
13
10
|
|
14
11
|
it 'has a version number' do
|
15
12
|
expect(Filepreviews::VERSION).not_to be nil
|
16
13
|
end
|
17
14
|
|
18
|
-
it '
|
19
|
-
expect(
|
15
|
+
it 'includes configurable methods from Filepreviews::Config' do
|
16
|
+
expect(file_previews.methods).to include(:api_key, :configure)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '.generate' do
|
20
|
+
before(:each) { Filepreviews.api_key = nil }
|
21
|
+
|
22
|
+
context 'when used without an api key' do
|
23
|
+
it 'returns a Filepreviews::Response instance' do
|
24
|
+
expect(file_previews.generate(sample_img))
|
25
|
+
.to be_an_instance_of(Filepreviews::Response)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when used with an api key' do
|
30
|
+
it 'returns a Filepreviews::Response instance' do
|
31
|
+
Filepreviews.api_key = ENV['FILEPREVIEWS_API_KEY']
|
32
|
+
response = file_previews.generate(sample_img)
|
33
|
+
|
34
|
+
expect(response.metadata_url).to_not be_nil
|
35
|
+
expect(response.preview_url).to_not be_nil
|
36
|
+
expect(response).to be_an_instance_of(Filepreviews::Response)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when used with an incorrect api key' do
|
41
|
+
it 'returns a Filepreviews::Response instance with an error msg' do
|
42
|
+
Filepreviews.api_key = '666'
|
43
|
+
response = file_previews.generate(sample_img)
|
44
|
+
|
45
|
+
expect(response).to respond_to(:error)
|
46
|
+
expect(response.error).to respond_to(:api_key)
|
47
|
+
expect(response.error.api_key).to include('Invalid API Key.')
|
48
|
+
end
|
49
|
+
end
|
20
50
|
end
|
21
51
|
end
|
@@ -20,4 +20,23 @@ describe Filepreviews::HTTP do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
describe 'configure_api_auth_header' do
|
25
|
+
before(:each) { Filepreviews.api_key = nil }
|
26
|
+
header = { 'User-Agent' => "Filepreviews-Rubygem/#{Filepreviews::VERSION}" }
|
27
|
+
|
28
|
+
context 'when api_key is set' do
|
29
|
+
it 'configures the X-API-KEY header' do
|
30
|
+
Filepreviews.api_key = '666'
|
31
|
+
expect(http.default_connection.headers['X-API-KEY']).to eq('666')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when api_key is not present' do
|
36
|
+
it 'does not add the X-API-KEY header' do
|
37
|
+
expect(http.default_connection.headers['X-API-KEY']).to be_nil
|
38
|
+
expect(http.default_connection.headers).to eq(header)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
23
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filepreviews
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonah Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -102,11 +102,13 @@ files:
|
|
102
102
|
- filepreviews.gemspec
|
103
103
|
- lib/filepreviews.rb
|
104
104
|
- lib/filepreviews/cli.rb
|
105
|
+
- lib/filepreviews/config.rb
|
105
106
|
- lib/filepreviews/http.rb
|
106
107
|
- lib/filepreviews/response.rb
|
107
108
|
- lib/filepreviews/utils.rb
|
108
109
|
- lib/filepreviews/version.rb
|
109
110
|
- rubocop.yml
|
111
|
+
- spec/filepreviews/config_spec.rb
|
110
112
|
- spec/filepreviews/filepreviews_spec.rb
|
111
113
|
- spec/filepreviews/http_spec.rb
|
112
114
|
- spec/filepreviews/utils_spec.rb
|
@@ -136,6 +138,7 @@ signing_key:
|
|
136
138
|
specification_version: 4
|
137
139
|
summary: FilePreviews.io Ruby library and CLI for the service
|
138
140
|
test_files:
|
141
|
+
- spec/filepreviews/config_spec.rb
|
139
142
|
- spec/filepreviews/filepreviews_spec.rb
|
140
143
|
- spec/filepreviews/http_spec.rb
|
141
144
|
- spec/filepreviews/utils_spec.rb
|