headless-rails 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/headless-rails.gemspec +29 -0
- data/lib/headless/api_client.rb +46 -0
- data/lib/headless/rails.rb +19 -0
- data/lib/headless/rails/escaped_fragment_extractor.rb +55 -0
- data/lib/headless/rails/version.rb +5 -0
- data/spec/headless/api_client_spec.rb +16 -0
- data/spec/headless/rails/escaped_fragment_extractor_spec.rb +38 -0
- data/spec/headless/rails_spec.rb +55 -0
- data/spec/spec_helper.rb +2 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21b0f92d77e15c80aa132104b24364189c1767e7
|
4
|
+
data.tar.gz: e1699ab35cbaabba74f93524a13bc4a3ee48952e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e08d9016ae46c03000e48464df75dd2a22c53d361eaf96bae8031914b23b57c823b98ca654c28ed7806603e43f9eb11a48e11f6a8e55f0d7445d62d2553a488
|
7
|
+
data.tar.gz: b4b5a0fb24e46b1eeaeecb52b5599b0a7a251d8e5c195a43212e4262efccec92d3601e5792aa55f18ada4f6c6fb6517bc2e9cddad99bf6663c0d5429848e69b9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Matthew Rudy Jacobs
|
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,42 @@
|
|
1
|
+
# Headless::Rails
|
2
|
+
|
3
|
+
API wrapper for the headlessapp.com service.
|
4
|
+
|
5
|
+
Seamlessly integrate AJAX webcrawler support into your javascript application.
|
6
|
+
|
7
|
+
Sign up at http://www.headlessapp.com and install the gem.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'headless-rails'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install headless-rails
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Open your ApplicationController,
|
26
|
+
and add the following;
|
27
|
+
|
28
|
+
class ApplicationController < ActionController::Base
|
29
|
+
include Headless::Rails
|
30
|
+
|
31
|
+
before_filter :respond_to_ajax_crawlers
|
32
|
+
end
|
33
|
+
|
34
|
+
That's it.
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'headless/rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "headless-rails"
|
8
|
+
spec.version = Headless::Rails::VERSION
|
9
|
+
spec.authors = ["Matthew Rudy Jacobs"]
|
10
|
+
spec.email = ["MatthewRudyJacobs@gmail.com"]
|
11
|
+
spec.summary = %q{ API wrapper for the HeadlessApp.com }
|
12
|
+
spec.description = %q{ Seamlessly integrate headlessapp.com into your Rails application }
|
13
|
+
|
14
|
+
spec.homepage = "http://www.headlessapp.com"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "rest-client", "~> 1.6.7"
|
23
|
+
spec.add_dependency "json"
|
24
|
+
spec.add_dependency "rack"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "rspec"
|
29
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "rest_client"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module Headless
|
5
|
+
class APIClient
|
6
|
+
|
7
|
+
def self.crawl(url)
|
8
|
+
new.crawl(url)
|
9
|
+
end
|
10
|
+
|
11
|
+
def crawl(url)
|
12
|
+
APIResponse.new(get("/api/crawl", :url => url))
|
13
|
+
end
|
14
|
+
|
15
|
+
class APIResponse < Struct.new(:response)
|
16
|
+
|
17
|
+
def content
|
18
|
+
parsed_response["content"]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def parsed_response
|
24
|
+
@parsed_response ||= ::JSON.parse(response)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def get(path, params)
|
31
|
+
::RestClient.get(url_for(path), :params => params)
|
32
|
+
end
|
33
|
+
|
34
|
+
def url_for(path)
|
35
|
+
"#{headless_protocol}://#{headless_host}#{path}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def headless_host
|
39
|
+
"api.headlessapp.com"
|
40
|
+
end
|
41
|
+
|
42
|
+
def headless_protocol
|
43
|
+
"http"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "headless/rails/version"
|
2
|
+
require "headless/rails/escaped_fragment_extractor"
|
3
|
+
require "headless/api_client"
|
4
|
+
|
5
|
+
module Headless
|
6
|
+
module Rails
|
7
|
+
|
8
|
+
ESCAPED_FRAGMENT_KEY = "_escaped_fragment_".freeze
|
9
|
+
|
10
|
+
def respond_to_ajax_crawlers
|
11
|
+
if params.has_key?(ESCAPED_FRAGMENT_KEY)
|
12
|
+
url = EscapedFragmentExtractor.call(request)
|
13
|
+
crawled = ::Headless::APIClient.crawl(url)
|
14
|
+
render text: crawled.content
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
|
3
|
+
module Headless
|
4
|
+
module Rails
|
5
|
+
class EscapedFragmentExtractor < Struct.new(:request)
|
6
|
+
|
7
|
+
def self.call(request)
|
8
|
+
new(request).call
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
url = "#{protocol}#{host_with_port}#{path}"
|
13
|
+
url << "?#{query_string}" unless query_string.empty?
|
14
|
+
url << "##{hash_string}" unless hash_string.empty?
|
15
|
+
url
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def protocol
|
21
|
+
request.protocol
|
22
|
+
end
|
23
|
+
|
24
|
+
def host_with_port
|
25
|
+
request.host_with_port
|
26
|
+
end
|
27
|
+
|
28
|
+
def path
|
29
|
+
request.path
|
30
|
+
end
|
31
|
+
|
32
|
+
def hash_string
|
33
|
+
escaped_fragment
|
34
|
+
end
|
35
|
+
|
36
|
+
def escaped_fragment
|
37
|
+
request.GET[escaped_fragment_key]
|
38
|
+
end
|
39
|
+
|
40
|
+
def params_without_fragment
|
41
|
+
without = request.GET.dup
|
42
|
+
without.delete(escaped_fragment_key)
|
43
|
+
without
|
44
|
+
end
|
45
|
+
|
46
|
+
def escaped_fragment_key
|
47
|
+
::Headless::Rails::ESCAPED_FRAGMENT_KEY
|
48
|
+
end
|
49
|
+
|
50
|
+
def query_string
|
51
|
+
::Rack::Utils.build_query(params_without_fragment)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe Headless::APIClient do
|
4
|
+
|
5
|
+
subject do
|
6
|
+
Headless::APIClient
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "crawl" do
|
10
|
+
it "returns a response" do
|
11
|
+
response = subject.crawl("http://matthewrudy.com")
|
12
|
+
expect(response.content).to include("Matthew Rudy Jacobs")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Headless::Rails::EscapedFragmentExtractor do
|
4
|
+
|
5
|
+
def request_with_fragment(escaped_fragment)
|
6
|
+
double(:request,
|
7
|
+
:protocol => "http://",
|
8
|
+
:host_with_port => "localhost:3000",
|
9
|
+
:path => "/user/matthewrudy",
|
10
|
+
:GET => {
|
11
|
+
"_escaped_fragment_" => escaped_fragment,
|
12
|
+
}
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "call" do
|
17
|
+
|
18
|
+
context "with an empty escaped_fragment" do
|
19
|
+
subject do
|
20
|
+
described_class.call(request_with_fragment(""))
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns a full url with the escaped fragment removed" do
|
24
|
+
expect(subject).to eq "http://localhost:3000/user/matthewrudy"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with an escaped_fragment" do
|
29
|
+
subject do
|
30
|
+
described_class.call(request_with_fragment("/show/twitter"))
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns the full url with an anchor appended" do
|
34
|
+
expect(subject).to eq "http://localhost:3000/user/matthewrudy#/show/twitter"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Headless::Rails do
|
4
|
+
it 'should have a version number' do
|
5
|
+
Headless::Rails::VERSION.should_not be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:controller) do
|
9
|
+
double(:controller,
|
10
|
+
:request => request,
|
11
|
+
:params => {},
|
12
|
+
:render => nil,
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:request) { double(:request) }
|
17
|
+
let(:headless_response) { double(:headless_response, :content => :headless_content) }
|
18
|
+
|
19
|
+
describe "respond_to_ajax_crawlers" do
|
20
|
+
before do
|
21
|
+
controller.extend(Headless::Rails)
|
22
|
+
end
|
23
|
+
|
24
|
+
context "with no escaped_fragment" do
|
25
|
+
before do
|
26
|
+
controller.stub(:params).and_return({})
|
27
|
+
end
|
28
|
+
|
29
|
+
it "doesn't call render" do
|
30
|
+
expect(controller).to_not receive(:render)
|
31
|
+
|
32
|
+
controller.respond_to_ajax_crawlers
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns nil" do
|
36
|
+
value = controller.respond_to_ajax_crawlers
|
37
|
+
expect(value).to be_nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with an escaped fragment" do
|
42
|
+
before do
|
43
|
+
controller.stub(:params).and_return({"_escaped_fragment_" => "something"})
|
44
|
+
end
|
45
|
+
|
46
|
+
it "extracts the fragment, fetches content from the api, and renders its" do
|
47
|
+
expect(Headless::Rails::EscapedFragmentExtractor).to receive(:call).with(request).and_return(:a_url)
|
48
|
+
expect(Headless::APIClient).to receive(:crawl).with(:a_url).and_return(headless_response)
|
49
|
+
expect(controller).to receive(:render).with(:text => :headless_content)
|
50
|
+
|
51
|
+
controller.respond_to_ajax_crawlers
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: headless-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Rudy Jacobs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.6.7
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.6.7
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ' Seamlessly integrate headlessapp.com into your Rails application '
|
98
|
+
email:
|
99
|
+
- MatthewRudyJacobs@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- .travis.yml
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- headless-rails.gemspec
|
112
|
+
- lib/headless/api_client.rb
|
113
|
+
- lib/headless/rails.rb
|
114
|
+
- lib/headless/rails/escaped_fragment_extractor.rb
|
115
|
+
- lib/headless/rails/version.rb
|
116
|
+
- spec/headless/api_client_spec.rb
|
117
|
+
- spec/headless/rails/escaped_fragment_extractor_spec.rb
|
118
|
+
- spec/headless/rails_spec.rb
|
119
|
+
- spec/spec_helper.rb
|
120
|
+
homepage: http://www.headlessapp.com
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.0.3
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: API wrapper for the HeadlessApp.com
|
144
|
+
test_files:
|
145
|
+
- spec/headless/api_client_spec.rb
|
146
|
+
- spec/headless/rails/escaped_fragment_extractor_spec.rb
|
147
|
+
- spec/headless/rails_spec.rb
|
148
|
+
- spec/spec_helper.rb
|