myfonts 0.0.4

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.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pochta.gemspec
4
+ gemspec
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ MyFonts (0.0.2)
5
+ httparty
6
+ nokogiri
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.3.2)
12
+ crack (0.3.1)
13
+ diff-lcs (1.1.3)
14
+ httparty (0.9.0)
15
+ multi_json (~> 1.0)
16
+ multi_xml
17
+ multi_json (1.4.0)
18
+ multi_xml (0.5.1)
19
+ nokogiri (1.5.5)
20
+ rspec (2.11.0)
21
+ rspec-core (~> 2.11.0)
22
+ rspec-expectations (~> 2.11.0)
23
+ rspec-mocks (~> 2.11.0)
24
+ rspec-core (2.11.1)
25
+ rspec-expectations (2.11.3)
26
+ diff-lcs (~> 1.1.3)
27
+ rspec-mocks (2.11.3)
28
+ vcr (2.3.0)
29
+ webmock (1.9.0)
30
+ addressable (>= 2.2.7)
31
+ crack (>= 0.1.7)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ MyFonts!
38
+ rspec
39
+ vcr
40
+ webmock
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Artem Shitov
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.
@@ -0,0 +1,2 @@
1
+ myfonts
2
+ =======
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__)) unless $LOAD_PATH.include?(File.dirname(__FILE__))
2
+
3
+ require "myfonts/designer"
4
+ require "myfonts/face"
5
+ require "myfonts/family"
6
+ require "myfonts/model"
7
+ require "myfonts/previewable"
8
+ require "myfonts/foundry"
9
+ require "myfonts/version"
@@ -0,0 +1,33 @@
1
+ require "myfonts/model"
2
+
3
+ module MyFonts
4
+ class Designer < Model
5
+ def initialize(url, name=nil)
6
+ @name = name
7
+ super(url)
8
+ end
9
+
10
+ def name
11
+ @name ||= get_name
12
+ end
13
+
14
+ def families
15
+ @families ||= get_families
16
+ end
17
+
18
+ private
19
+
20
+ def get_name
21
+ dom.css("title").text.gsub(/ . MyFonts/, "")
22
+ end
23
+
24
+ def get_families
25
+ result = dom.css("h4 a").select do |a|
26
+ a.get_attribute("href") =~ /^\/fonts/
27
+ end
28
+ result.map do |a|
29
+ Family.new("http://www.myfonts.com" + a.get_attribute("href"), a.text)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ require "myfonts/model"
2
+ require "myfonts/previewable"
3
+
4
+ module MyFonts
5
+ class Face < Model
6
+ include Previewable
7
+
8
+ def initialize(url, name=nil)
9
+ @name = name
10
+ super(url)
11
+ end
12
+
13
+ def name
14
+ @name ||= get_name
15
+ end
16
+
17
+ def family
18
+ @family ||= get_family
19
+ end
20
+
21
+ private
22
+
23
+ def get_name
24
+ dom.css("h1 img")[0].get_attribute("title")
25
+ end
26
+
27
+ def get_family
28
+ a = dom.css("#headcrumbs li")[-2].css("a")[0]
29
+ Family.new("http://www.myfonts.com" + a.get_attribute("href"), a.text)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,82 @@
1
+ require "myfonts/model"
2
+ require "myfonts/previewable"
3
+
4
+ module MyFonts
5
+ class Family < Model
6
+ include Previewable
7
+
8
+ def initialize(url, name=nil)
9
+ @name = name
10
+ super(url)
11
+ end
12
+
13
+ def name
14
+ @name ||= get_name
15
+ end
16
+
17
+ def foundry
18
+ @foundry ||= get_foundry
19
+ end
20
+
21
+ def faces
22
+ @faces ||= get_faces
23
+ end
24
+
25
+ def designers
26
+ @designers ||= get_designers
27
+ end
28
+
29
+ def design_dates
30
+ @design_dates ||= get_design_dates
31
+ end
32
+
33
+ def description
34
+ @description ||= get_description
35
+ end
36
+
37
+ def images
38
+ @images ||= get_images
39
+ end
40
+
41
+ private
42
+
43
+ def get_name
44
+ dom.css("title").text.gsub(/ - (Desktop|Webfont).+/, "")
45
+ end
46
+
47
+ def get_foundry
48
+ foundry_link = dom.css("ul.family_metadata li a").select{ |l| l.get_attribute("href") =~ /foundry/ }[0]
49
+ Foundry.new(foundry_link.get_attribute("href"), foundry_link.text)
50
+ end
51
+
52
+ def get_faces
53
+ links = dom.css("h4 a").select{ |l| !l.text.empty? }
54
+ links.map do |l|
55
+ Face.new("http://www.myfonts.com" + l.get_attribute("href"), l.text)
56
+ end
57
+ end
58
+
59
+ def get_designers
60
+ links = dom.css("ul.family_metadata li a")
61
+ designers_links = links.select{ |l| l.get_attribute("href") =~ /person/ }
62
+ designers_links.map do |l|
63
+ Designer.new("http://www.myfonts.com" + l.get_attribute("href"), l.text)
64
+ end
65
+ end
66
+
67
+ def get_design_dates
68
+ li = dom.css("ul.family_metadata li")
69
+ li = li.select { |l| l.text =~ /Design date/}
70
+ dates = li[0].text[/(\d+(, )*)+/]
71
+ dates.split(", ").map { |e| e.to_i }
72
+ end
73
+
74
+ def get_description
75
+ dom.css(".article_tease_container").text.gsub(/(^\s+)|(\s+$)|(\t+)/, "")
76
+ end
77
+
78
+ def get_images
79
+ dom.text.scan(/[\d\/]+\.png/).map { |e| "http://cdn.myfonts.net/s/aw/720x360/" + e }
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,33 @@
1
+ require "myfonts/model"
2
+
3
+ module MyFonts
4
+ class Foundry < Model
5
+ def initialize(url, name=nil)
6
+ @name = name
7
+ super(url)
8
+ end
9
+
10
+ def name
11
+ @name ||= get_name
12
+ end
13
+
14
+ def families
15
+ @families ||= get_families
16
+ end
17
+
18
+ private
19
+
20
+ def get_name
21
+ dom.css("title").text.gsub(/ . MyFonts/, "")
22
+ end
23
+
24
+ def get_families
25
+ result = dom.css("h4 a").select do |a|
26
+ a.get_attribute("href") =~ /^\/fonts/
27
+ end
28
+ result.map do |a|
29
+ Family.new("http://www.myfonts.com" + a.get_attribute("href"), a.text)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ require "httparty"
2
+ require "nokogiri"
3
+
4
+ module MyFonts
5
+ class Model
6
+ attr_reader :url
7
+
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def dom
13
+ @dom ||= get_dom(@url)
14
+ end
15
+
16
+ private
17
+
18
+ def get_dom(url)
19
+ url.sub!(/\/*$/, "/")
20
+ myfonts_page = HTTParty.get(url)
21
+ Nokogiri::HTML(myfonts_page.body)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module MyFonts
2
+ module Previewable
3
+ def preview(text="The little brown fox jumps over the lazy dog")
4
+ response = HTTParty.post("http://www.myfonts.com/widgets/testdrive/testdrive-ajax.php",
5
+ query: {
6
+ "seed" => 0,
7
+ "size" => 72,
8
+ "text" => text,
9
+ "fg" => "000000",
10
+ "bg" => "ffffff",
11
+ "src" => "custom",
12
+ "tab" => "desktop",
13
+ "goodies" => "ot.liga",
14
+ "browser[]" => "mac_106_firefox_3_6",
15
+ "w" => "720",
16
+ "i[0]" => url.gsub(/((http:\/\/)?(www\.)?myfonts\.com\/fonts\/)/, "").gsub(/\/$/, "") + ",,720"
17
+ }
18
+ )
19
+ response.body.gsub(/\\/, "")[/src="(.+?)"/, 1]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module MyFonts
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/myfonts/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Artem Shitov"]
6
+ gem.email = ["inbox@artemshitov.ru"]
7
+ gem.summary = "Ruby interface to MyFonts"
8
+ gem.homepage = "https://github.com/artemshitov/myfonts"
9
+ gem.description = "Gem allows to fetch info about fonts hosted on MyFonts"
10
+
11
+ gem.add_development_dependency "rspec"
12
+ gem.add_development_dependency "webmock"
13
+ gem.add_development_dependency "vcr"
14
+
15
+ gem.add_dependency "httparty"
16
+ gem.add_dependency "nokogiri"
17
+
18
+ gem.files = `git ls-files`.split($\)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.name = "myfonts"
22
+ gem.require_paths = ["lib"]
23
+ gem.version = MyFonts::VERSION
24
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'myfonts/designer'
5
+
6
+ describe MyFonts::Designer do
7
+ before do
8
+ @designer = MyFonts::Designer.new("http://www.myfonts.com/person/yuri-gordon/")
9
+ end
10
+
11
+ around do |test|
12
+ VCR.use_cassette('YuriGordon') do
13
+ test.run
14
+ end
15
+ end
16
+
17
+ it "returns correct designer name" do
18
+ @designer.name.should == "Yuri Gordon"
19
+ end
20
+
21
+ it "returns designer's type families" do
22
+ @designer.families.size.should == 14
23
+ @designer.families[0].name.should == "Conqueror Sans™"
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'myfonts/face'
5
+
6
+ describe MyFonts::Face do
7
+ before :all do
8
+ @face = MyFonts::Face.new("http://www.myfonts.com/fonts/letterheadrussia/21-cent/cond-ultra-light-ital/")
9
+ end
10
+
11
+ around do |test|
12
+ VCR.use_cassette "21Cent-Cond-Ultra-Light-Ital" do
13
+ test.run
14
+ end
15
+ end
16
+
17
+ it "shows correct face name" do
18
+ @face.name.should == "21 Cent Condensed Ultra Light Italic"
19
+ end
20
+
21
+ it "references correct family" do
22
+ @face.family.name.should == "21 Cent"
23
+ end
24
+
25
+ it "returns correct preview image url" do
26
+ VCR.use_cassette "TestDrive-Face" do
27
+ @face.preview.should =~ /http:\/\/origin\.myfonts\.net/
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'myfonts/family'
5
+
6
+ describe MyFonts::Family do
7
+ before :all do
8
+ @family = MyFonts::Family.new("http://www.myfonts.com/fonts/letterheadrussia/21-cent/")
9
+ end
10
+
11
+ around :each do |test|
12
+ VCR.use_cassette('21Cent') do
13
+ test.run
14
+ end
15
+ end
16
+
17
+ it "returns correct foundry" do
18
+ @family.foundry.name.should == "Letterhead Studio-YG"
19
+ end
20
+
21
+ it "returns correct designers list" do
22
+ @family.designers.size.should == 1
23
+ @family.designers[0].name.should == "Yuri Gordon"
24
+ end
25
+
26
+ it "returns correct list of faces" do
27
+ @family.faces.size.should == 10
28
+ @family.faces[0].name.should == "21 Cent Thin"
29
+ end
30
+
31
+ it "returns correct design dates" do
32
+ @family.design_dates.should == [2008, 2010]
33
+ end
34
+
35
+ it "returns correct description" do
36
+ @family.description.should include "21 Cent - not Century or Clarendon."
37
+ end
38
+
39
+ it "returns correct list of images" do
40
+ i = @family.images
41
+ i.should include "http://cdn.myfonts.net/s/aw/720x360/99/0/50813.png"
42
+ i.size.should == 8
43
+ end
44
+
45
+ it "returns correct preview image url" do
46
+ VCR.use_cassette "TestDrive-Family" do
47
+ @family.preview.should =~ /http:\/\/origin\.myfonts\.net/
48
+ end
49
+ end
50
+ end