mudfly 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89572b3458ff921f07a379fc668085b7059ef63c
4
+ data.tar.gz: a54118dd543323fc06fca86d2326dd0e58c0f337
5
+ SHA512:
6
+ metadata.gz: 686cc60ceec62fc0739e2ee0655227655fb1c37c9497552c96c1bb80614ab0149bec33955a79d273c1523d2b74214702b96cfdd01d6732e13fb937b01c6591d4
7
+ data.tar.gz: da1dc934c36f113786b436c8af8d682c4005f21ed544a5010906d61119c56e252cb61540ff2e6ac4000df58ae06f7e2e4a9888613ea7b95563e9ae3c1011bb56
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ build.sh
2
+ spike.rb
3
+ *.gem
4
+ spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mudfly (0.0.1)
5
+ faraday
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.1)
11
+ faraday (0.8.6)
12
+ multipart-post (~> 1.1)
13
+ multipart-post (1.2.0)
14
+ rake (10.0.3)
15
+ rspec (2.13.0)
16
+ rspec-core (~> 2.13.0)
17
+ rspec-expectations (~> 2.13.0)
18
+ rspec-mocks (~> 2.13.0)
19
+ rspec-core (2.13.1)
20
+ rspec-expectations (2.13.0)
21
+ diff-lcs (>= 1.1.3, < 2.0)
22
+ rspec-mocks (2.13.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.3)
29
+ mudfly!
30
+ rake
31
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pablo Elices
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,112 @@
1
+ # Mudfly 0.0.1
2
+
3
+ A Ruby wrapper for the PageSpeed Insights API
4
+
5
+ ## Requirements
6
+
7
+ - Ruby >= 2.0.0
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'mudfly'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mudfly
22
+
23
+ ## Configuration
24
+
25
+ require 'mudfly'
26
+
27
+ # Defaults
28
+
29
+ Mudfly.api_key # => nil
30
+ Mudfly.user_agent # => "Mudfly Ruby Gem 0.0.1"
31
+ Mudfly.endpoint # => "https://www.googleapis.com/pagespeedonline/v1/"
32
+ Mudfly.locale # => "en_US"
33
+
34
+ # Block Configuration
35
+
36
+ Mudfly.configure do |config|
37
+ config.api_key = 'Your API Key Here.'
38
+ config.user_agent = 'New User Agent'
39
+ config.endpoint = 'Custom Endpoint'
40
+ config.locale = 'Your Locale Here'
41
+ end
42
+
43
+ # Single Configuration
44
+
45
+ Mudfly.api_key = 'Your API Key Here.'
46
+ Mudfly.user_agent = 'New User Agent'
47
+ Mudfly.endpoint = 'Custom Endpoint'
48
+ Mudfly.locale = 'Your Locale Here'
49
+
50
+ See [this](https://developers.google.com/speed/docs/insights/languages) for a valid list of locales.
51
+
52
+ ## Usage
53
+
54
+ require 'mudfly'
55
+
56
+ # Configure Mudfly Here
57
+
58
+ Mudfly.analyze('https://github.com') # Desktop report for github.com
59
+ Mudfly.analyze('https://github.com', strategy: :desktop) # Same as above
60
+ Mudfly.analyze('https://github.com', strategy: :mobile) # Mobile report for github.com
61
+
62
+ ## Example
63
+
64
+ require 'mudfly'
65
+
66
+ Mudfly.api_key = 'Your API Key Here'
67
+
68
+ report = Mudfly.analyze('http://pabloelic.es', strategy: :desktop)
69
+
70
+
71
+ # Report Info
72
+
73
+ puts "Kind: #{report.kind}"
74
+ puts "ID: #{report.id}"
75
+ puts "Response Code: #{report.response_code}"
76
+ puts "Title: #{report.title}"
77
+ puts "Score: #{report.score}"
78
+
79
+
80
+ # Page Stats
81
+
82
+ puts "Resources Number: #{report.stats.resources_number}"
83
+ puts "Hosts Number: #{report.stats.hosts_number}"
84
+ puts "Total Request Bytes: #{report.stats.total_request_bytes}"
85
+ puts "Static Resources Number: #{report.stats.static_resources_number}"
86
+ puts "HTML Response Bytes: #{report.stats.html_response_bytes}"
87
+ puts "CSS Response Bytes: #{report.stats.css_response_bytes}"
88
+ puts "JavaScript Response Bytes: #{report.stats.javascript_response_bytes}"
89
+ puts "Image Response Bytes: #{report.stats.image_response_bytes}"
90
+ puts "JavaScript Resources Number: #{report.stats.javascript_resources_number}"
91
+ puts "CSS Resources Number: #{report.stats.css_resources_number}"
92
+
93
+
94
+ # Rules
95
+
96
+ report.rules.each do |rule|
97
+
98
+ puts "Rule Name: #{rule.name}"
99
+ puts "Rule Score: #{rule.score}"
100
+ puts "Rule Impact: #{rule.impact}"
101
+
102
+ end
103
+
104
+
105
+ # Report API Version
106
+
107
+ puts "Report API Version: #{report.version.major}.#{report.version.minor}"
108
+
109
+
110
+ ## Copyright
111
+
112
+ Copyright &copy; 2013 Pablo Elices. See [license](https://github.com/pabloelices/mudfly/blob/master/LICENSE.txt) for details.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:specs)
4
+
5
+ task :default do
6
+
7
+ puts `rake -T`
8
+
9
+ end
data/lib/mudfly.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'mudfly/configuration'
2
+ require 'mudfly/client'
3
+
4
+ module Mudfly extend Configuration
5
+
6
+ # Delegate to Mudfly::Client
7
+
8
+ def self.method_missing(symbol, *args)
9
+
10
+ if Client.respond_to?(symbol)
11
+
12
+ return Client.send(symbol, *args)
13
+
14
+ else
15
+
16
+ return super
17
+
18
+ end
19
+
20
+ end
21
+
22
+ # Delegate to Mudfly::Client
23
+
24
+ def self.respond_to?(symbol, include_all = false)
25
+
26
+ return Client.respond_to?(symbol, include_all) || super(symbol, include_all)
27
+
28
+ end
29
+
30
+ end # Mudfly
@@ -0,0 +1,76 @@
1
+ require 'json'
2
+
3
+ require 'mudfly/request'
4
+
5
+ module Mudfly
6
+
7
+ class Client
8
+
9
+ def self.analyze(url, strategy: :desktop)
10
+
11
+ unless [:desktop, :mobile].include?(strategy)
12
+
13
+ raise ArgumentError.new('Invalid strategy, only :desktop and :mobile are allowed.')
14
+
15
+ end
16
+
17
+ request_url = 'runPagespeed'
18
+
19
+ query_string = {
20
+
21
+ :url => url,
22
+ :strategy => strategy
23
+
24
+ }
25
+
26
+ response_body = JSON.parse( Request.get(request_url, query_string) )
27
+
28
+ report = OpenStruct.new
29
+
30
+ report.response_code = response_body['responseCode']
31
+ report.title = response_body['title']
32
+ report.score = response_body['score']
33
+ report.kind = response_body['kind']
34
+ report.id = response_body['id']
35
+
36
+ report.stats = OpenStruct.new({
37
+
38
+ :resources_number => response_body['pageStats']['numberResources'],
39
+ :hosts_number => response_body['pageStats']['numberHosts'],
40
+ :total_request_bytes => response_body['pageStats']['totalRequestBytes'],
41
+ :static_resources_number => response_body['pageStats']['numberStaticResources'],
42
+ :html_response_bytes => response_body['pageStats']['htmlResponseBytes'],
43
+ :css_response_bytes => response_body['pageStats']['cssResponseBytes'],
44
+ :image_response_bytes => response_body['pageStats']['imageResponseBytes'],
45
+ :javascript_response_bytes => response_body['pageStats']['javascriptResponseBytes'],
46
+ :javascript_resources_number => response_body['pageStats']['numberJsResources'],
47
+ :css_resources_number => response_body['pageStats']['numberCssResources']
48
+
49
+ })
50
+
51
+ report.rules = response_body['formattedResults']['ruleResults'].values.each.inject([]) do |rules, rule_temp|
52
+
53
+ rules << OpenStruct.new({
54
+
55
+ :name => rule_temp['localizedRuleName'],
56
+ :score => rule_temp['ruleScore'],
57
+ :impact => rule_temp['ruleImpact']
58
+
59
+ })
60
+
61
+ end
62
+
63
+ report.version = OpenStruct.new({
64
+
65
+ :major => response_body['version']['major'],
66
+ :minor => response_body['version']['minor']
67
+
68
+ })
69
+
70
+ return report
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,36 @@
1
+ require "mudfly/version"
2
+
3
+ module Mudfly
4
+
5
+ module Configuration
6
+
7
+ VALID_CONFIGURATION_KEYS = [:api_key, :user_agent, :endpoint, :locale]
8
+
9
+ DEFAULT_API_KEY = nil
10
+ DEFAULT_USER_AGENT = "Mudfly Ruby Gem #{Mudfly::VERSION}"
11
+ DEFAULT_ENDPOINT = 'https://www.googleapis.com/pagespeedonline/v1/'
12
+ DEFAULT_LOCALE = 'en_US'
13
+
14
+
15
+ attr_accessor *VALID_CONFIGURATION_KEYS
16
+
17
+ def self.extended(base)
18
+
19
+ base.api_key = DEFAULT_API_KEY
20
+ base.user_agent = DEFAULT_USER_AGENT
21
+ base.endpoint = DEFAULT_ENDPOINT
22
+ base.locale = DEFAULT_LOCALE
23
+
24
+ end
25
+
26
+ # Allow block configuration
27
+
28
+ def configure
29
+
30
+ yield self
31
+
32
+ end
33
+
34
+ end # Configuration
35
+
36
+ end # Mudfly
@@ -0,0 +1,9 @@
1
+ module Mudfly
2
+
3
+ module Error
4
+
5
+ class BadRequest < StandardError; end
6
+
7
+ end # Error
8
+
9
+ end # Mudfly
@@ -0,0 +1,9 @@
1
+ module Mudfly
2
+
3
+ module Error
4
+
5
+ class Forbidden < StandardError; end
6
+
7
+ end # Error
8
+
9
+ end # Mudfly
@@ -0,0 +1,9 @@
1
+ module Mudfly
2
+
3
+ module Error
4
+
5
+ class InternalServerError < StandardError; end
6
+
7
+ end # Error
8
+
9
+ end # Mudfly
@@ -0,0 +1,9 @@
1
+ module Mudfly
2
+
3
+ module Error
4
+
5
+ class ServiceUnavailable < StandardError; end
6
+
7
+ end # Error
8
+
9
+ end # Mudfly
@@ -0,0 +1,64 @@
1
+ require 'faraday'
2
+
3
+ require 'mudfly/response/http_exception'
4
+
5
+ module Mudfly
6
+
7
+ class Request
8
+
9
+ def self.get(url, query_string = {})
10
+
11
+ return perform(:get, url, query_string)
12
+
13
+ end
14
+
15
+ def self.post(url, query_string = {})
16
+
17
+ return perform(:post, url, query_string)
18
+
19
+ end
20
+
21
+
22
+ private
23
+
24
+ def self.connection
25
+
26
+ connection = Faraday::Connection.new({
27
+
28
+ :url => Mudfly.endpoint,
29
+ :headers => { 'User-Agent' => Mudfly.user_agent }
30
+
31
+ })
32
+
33
+ connection.use Mudfly::Response::HttpException
34
+
35
+ return connection
36
+
37
+ end
38
+
39
+ def self.perform(method, url, query_string = {})
40
+
41
+ query_string[:prettyprint] = false
42
+ query_string[:locale] = Mudfly.locale
43
+ query_string[:key] = Mudfly.api_key
44
+
45
+
46
+ response = connection.send(method) do |request|
47
+
48
+ case method
49
+ when :get
50
+ request.url(url, query_string)
51
+ when :post
52
+ request.path = url
53
+ request.body = query_string
54
+ end
55
+
56
+ end
57
+
58
+ return response.body
59
+
60
+ end
61
+
62
+ end # Request
63
+
64
+ end # Mudfly
@@ -0,0 +1,34 @@
1
+ require 'faraday'
2
+
3
+ require 'mudfly/error/bad_request'
4
+ require 'mudfly/error/forbidden'
5
+ require 'mudfly/error/service_unavailable'
6
+ require 'mudfly/error/internal_server_error'
7
+
8
+ module Mudfly
9
+
10
+ module Response
11
+
12
+ class HttpException < Faraday::Response::Middleware
13
+
14
+ def on_complete(response)
15
+
16
+ case response[:status].to_i
17
+
18
+ when 400; raise Mudfly::Error::BadRequest.new(response[:body])
19
+
20
+ when 403; raise Mudfly::Error::Forbidden.new(response[:body])
21
+
22
+ when 500; raise Mudfly::Error::InternalServerError.new('Something gone wrong')
23
+
24
+ when 503; raise Mudfly::Error::ServiceUnavailable.new('Impossible to handle the request')
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end # HttpException
31
+
32
+ end # Response
33
+
34
+ end # Mudfly
@@ -0,0 +1,5 @@
1
+ module Mudfly
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ end # Mudfly
data/mudfly.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'mudfly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = 'mudfly'
9
+ spec.version = Mudfly::VERSION
10
+ spec.author = 'Pablo Elices'
11
+ spec.email = 'contact@pabloelic.es'
12
+ spec.description = 'Mudfly is a Ruby wrapper for the PageSpeed Insights API.'
13
+ spec.summary = 'A Ruby wrapper for the PageSpeed Insights API.'
14
+ spec.homepage = 'https://github.com/pabloelices/mudfly'
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.required_ruby_version = '>= 2.0.0'
23
+
24
+ spec.add_runtime_dependency 'faraday'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.3'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mudfly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Elices
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Mudfly is a Ruby wrapper for the PageSpeed Insights API.
70
+ email: contact@pabloelic.es
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/mudfly.rb
82
+ - lib/mudfly/client.rb
83
+ - lib/mudfly/configuration.rb
84
+ - lib/mudfly/error/bad_request.rb
85
+ - lib/mudfly/error/forbidden.rb
86
+ - lib/mudfly/error/internal_server_error.rb
87
+ - lib/mudfly/error/service_unavailable.rb
88
+ - lib/mudfly/request.rb
89
+ - lib/mudfly/response/http_exception.rb
90
+ - lib/mudfly/version.rb
91
+ - mudfly.gemspec
92
+ homepage: https://github.com/pabloelices/mudfly
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: 2.0.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.0.rc.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A Ruby wrapper for the PageSpeed Insights API.
116
+ test_files: []