coderbits 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4e0049c3c53f1a9a7d60e7641d687457f8b4e91
4
- data.tar.gz: 8efd2cdbc2ceeb5407d24479d33159c69d0eae54
3
+ metadata.gz: 0f3f89f4d80c71f4f30ccec6aeb3e3e4537d99b7
4
+ data.tar.gz: 4f4ec14200e32a65f7a2006c49771fa0a9df8183
5
5
  SHA512:
6
- metadata.gz: c84856626af07e1f24de4e5d6ab3cd8c987aa8fb584703b769362719d070b870634ced894f6dc45de946d1eb05fbc3f7fcca5477730798f0533358040b9d4681
7
- data.tar.gz: ceb6e5e36152a0a0361c9dc2ccdd49ce0f725e654511b80adb18e74ba829a132e9421b1f40824f742b8889a67ce08ba56a13cf990e3c28ab7948483f54c3558c
6
+ metadata.gz: d14bfbc1f15991c35d29f491e3ca034c491d001f0aab185a4734c5173869b5b3428d32a199364612236b112bc397cfd695719c0b69f64767988a2c1f485b1ca7
7
+ data.tar.gz: 494a6e187229d9de9f030c540a4b4157abf6a495b1a00e39550553711c115a31fc5b7bd36ab875e3cf729f6a422013e4bf613a061b73475153df0debb2c8a96f
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # coderbits
2
2
 
3
3
  [![Gem version](https://badge.fury.io/rb/coderbits.png)](https://rubygems.org/gems/coderbits)
4
- [![Build Status](https://secure.travis-ci.org/artemeff/coderbits.png)][travis]
4
+ [![Build Status](https://secure.travis-ci.org/artemeff/coderbits.png)](https://travis-ci.org/artemeff/coderbits)
5
5
  [![Code Climate](https://codeclimate.com/github/artemeff/coderbits.png)](https://codeclimate.com/github/artemeff/coderbits)
6
6
  [![Dependency Status](https://gemnasium.com/artemeff/coderbits.png)](https://gemnasium.com/artemeff/coderbits)
7
7
 
@@ -19,7 +19,9 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.add_development_dependency 'bundler', '~> 1.0'
21
21
  s.add_dependency 'faraday', '~> 0.8'
22
- s.add_dependency 'multi_json', '~> 1.3'
22
+ s.add_dependency 'faraday_middleware', '~> 0.8'
23
+ s.add_dependency 'multi_json', '~> 1.7'
23
24
  s.add_dependency 'sprite-factory', '~> 1.5'
24
25
  s.add_dependency 'chunky_png', '~> 1.2'
26
+ s.add_dependency 'hashie', '~> 2.0'
25
27
  end
@@ -1,27 +1,26 @@
1
+ require 'sprite_factory'
2
+ require 'multi_json'
3
+ require 'hashie'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+
7
+ require 'coderbits/defaults'
1
8
  require 'coderbits/client'
9
+ require 'coderbits/object'
2
10
  require 'coderbits/glue'
3
11
 
4
12
  module Coderbits
5
13
  class << self
6
- # Alias for Coderbits::Client.new
7
- #
8
- # @return [Coderbits::Client]
14
+ # Alias for Coderbits::Client.new.get
9
15
  def new username, options = {}
10
- Coderbits::Client.new# username, options
11
- end
12
-
13
- # Alias for Coderbits::Client.new(...).get
14
- #
15
- # @return [Hash]
16
- def get username
17
- Coderbits::Client.new.get username
16
+ Coderbits::Client.new.get username, options
18
17
  end
18
+
19
+ alias_method :get, :new
19
20
 
20
21
  # Alias for Coderbits::Glue.new(...)
21
- #
22
- # @return
23
22
  def glue username, options = {}
24
23
  Coderbits::Glue.new username, options
25
24
  end
26
25
  end
27
- end
26
+ end
@@ -1,6 +1,3 @@
1
- require 'faraday'
2
- require 'multi_json'
3
-
4
1
  module Coderbits
5
2
  class Client
6
3
  # valid options
@@ -10,25 +7,48 @@ module Coderbits
10
7
  attr_reader :connection
11
8
 
12
9
  # Coderbits::Client.new
13
- #
14
- # @return @connection
15
- def initialize
16
- @connection = Faraday.new url: "https://coderbits.com"
10
+ # establish faraday connection
11
+ def initialize options = {}
12
+ options[:url] ||= Coderbits::Defaults::BASE_URL
13
+ options[:headers] ||= Coderbits::Defaults::HEADERS
14
+ options[:ssl] ||= Coderbits::Defaults::SSL
15
+
16
+ @connection = Faraday.new(options)
17
17
  end
18
18
 
19
19
  # Coderbits::Client.new.get username: 'artemeff'
20
+ # gets user data
20
21
  def get username, options = {}
21
- response = @connection.get "/#{username}.json", safe(options)
22
-
23
- if options[:callback]
24
- response.body
25
- else
26
- MultiJson.load response.body, symbolize_keys: true
22
+ begin
23
+ response = @connection.get "/#{username}.json", safe(options)
24
+
25
+ status = response.status
26
+ error = false
27
+ rescue Exception => e
28
+ status = -1
29
+ error = "Connection error: #{e}"
27
30
  end
31
+
32
+ # return string if options has callback
33
+ return response.body if options[:callback]
34
+
35
+ # parse json
36
+ user_data = MultiJson.load response.body, symbolize_keys: true
37
+
38
+ # set error if loaded json is empty
39
+ error = "User not found" if user_data.empty?
40
+
41
+ # set status and error message to returned data
42
+ user_data[:status] = status
43
+ user_data[:error] = error
44
+
45
+ # hashie
46
+ Coderbits::Object.new user_data
28
47
  end
29
48
 
30
49
  private
31
50
 
51
+ # safety option
32
52
  def safe options
33
53
  options.select { |k, v| OPTIONS.include? k }
34
54
  end
@@ -0,0 +1,7 @@
1
+ module Coderbits
2
+ module Defaults
3
+ BASE_URL = 'https://coderbits.com' unless defined? Coderbits::Defaults::BASE_URL
4
+ HEADERS = { accept: 'application/json' } unless defined? Coderbits::Defaults::HEADERS
5
+ SSL = { verify: false } unless defined? Coderbits::Defaults::SSL
6
+ end
7
+ end
@@ -1,6 +1,3 @@
1
- require 'sprite_factory'
2
- require 'faraday'
3
-
4
1
  module Coderbits
5
2
  class Glue
6
3
  # valid options
@@ -15,10 +12,11 @@ module Coderbits
15
12
  @username = username
16
13
  end
17
14
 
15
+ # launching all rockets
18
16
  def run!
19
17
  imgs = download_images
20
18
  css = merge_images
21
- html = generate_html
19
+ html = generate_html imgs
22
20
 
23
21
  {
24
22
  images: imgs,
@@ -32,36 +30,47 @@ module Coderbits
32
30
  data = Coderbits::Client.new.get @username
33
31
 
34
32
  # collect images
35
- images = data[:badges].select{ |b| b[:earned] }.map { |b| b[:image_link].match(/(.*)\/(.*)/)[2] }.uniq
36
-
37
- # get connection
38
- connection = Coderbits::Client.new.connection
33
+ images = data.badges.select { |b| b.earned }.map { |b| b.image_link.match(/(.*)\/(.*)/)[2] }.uniq
39
34
 
40
- # create directory
35
+ # create directories
41
36
  Dir.mkdir @options[:save_to] unless Dir.exists? @options[:save_to]
42
37
  Dir.mkdir "#{@options[:save_to]}/badges" unless Dir.exists? "#{@options[:save_to]}/badges"
43
38
 
39
+ # connection for download images
40
+ connection = Coderbits::Client.new(headers: { accept: 'image/png' }).connection
41
+
44
42
  # download images
45
43
  images.each do |img|
46
44
  # specify badge size
47
45
  img = change_size img unless @options[:badge_size] == 64
48
46
 
49
47
  # download image
50
- response = connection.get "https://coderbits.com/images/badges/#{img}"
51
-
52
- # save to disk
53
- if response.headers["content-type"] == "image/png"
54
- File.open("#{@options[:save_to]}/badges/#{img.downcase}", 'wb') { |f| f.write response.body }
48
+ begin
49
+ response = connection.get "/images/badges/#{img}"
50
+
51
+ # save to disk
52
+ if response.headers["content-type"] == "image/png"
53
+ File.open("#{@options[:save_to]}/badges/#{img.downcase}", 'wb') { |f| f.write response.body }
54
+ end
55
+ rescue Exception => e
56
+ # TODO logging
55
57
  end
56
58
  end
57
59
  end
58
60
 
61
+ # merge and generate css by SpriteFactory
59
62
  def merge_images
60
63
  SpriteFactory.run! "#{@options[:save_to]}/badges", @options
61
64
  end
62
65
 
63
- def generate_html
64
- nil # coming soon
66
+ def generate_html images
67
+ html_tags = []
68
+
69
+ images.each do |name|
70
+ html_tags << "<img src='#{@options[:output_image]}' class='#{@options[:selector][1..-1]}#{name.downcase.gsub(/.png/,'')}'>"
71
+ end
72
+
73
+ html_tags.join "\n"
65
74
  end
66
75
 
67
76
  def safe options
@@ -70,7 +79,7 @@ module Coderbits
70
79
 
71
80
  dflt = {
72
81
  badge_size: 64, # badges size for download
73
- save_to: './', # path for files
82
+ save_to: '.', # path for files
74
83
 
75
84
  layout: 'horizontal', # horizontal, vertical or packed
76
85
  style: 'css', # css, scss or sass
@@ -0,0 +1,5 @@
1
+ module Coderbits
2
+ class Object < Hashie::Mash
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  class Colos
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -15,8 +15,16 @@ describe Coderbits::Client do
15
15
  @data = Coderbits::Client.new.get 'artemeff'
16
16
  end
17
17
 
18
- it "should have user data" do
19
- expect(@data).to be_a Hash
18
+ it "should be a Coderbits::Object instance" do
19
+ expect(@data).to be_a Coderbits::Object
20
+ end
21
+
22
+ it "should return status eq 200" do
23
+ @data.status.should be 200
24
+ end
25
+
26
+ it "shouldn't have error" do
27
+ @data.error.should be false
20
28
  end
21
29
 
22
30
  it "should have username" do
@@ -31,10 +39,24 @@ describe Coderbits::Client do
31
39
  data.should be_an_instance_of String
32
40
  end
33
41
 
34
- it "should be a Hash with account option" do
42
+ it "should be a Coderbits::Object with account option" do
35
43
  data = Coderbits::Client.new.get 'artemeff', account: 'github'
36
44
 
37
- data.should be_an_instance_of Hash
45
+ data.should be_an_instance_of Coderbits::Object
46
+ end
47
+ end
48
+
49
+ describe "with nonexistent user" do
50
+ before do
51
+ @data = Coderbits::Client.new.get "prettyuserthatdoesntexist#{rand(10)}"
52
+ end
53
+
54
+ it "should have 200 status" do
55
+ @data.status.should be 200
56
+ end
57
+
58
+ it "should have error" do
59
+ @data.error.should match "User not found"
38
60
  end
39
61
  end
40
62
  end
@@ -17,33 +17,15 @@ describe Coderbits::Glue do
17
17
  FileUtils.rm_rf './temp/'
18
18
  end
19
19
 
20
- it "should run without errors" do
20
+ it "should work" do
21
21
  Coderbits.glue('artemeff', save_to: './temp/').run!
22
22
  end
23
23
 
24
- it "should run without errors with custom badge size" do
24
+ it "should work with custom badge size" do
25
25
  Coderbits.glue('artemeff', badge_size: 32, save_to: './temp/').run!
26
26
  end
27
27
  end
28
28
 
29
- describe ".download_images" do
30
- it "..." do
31
- pending "Write tests"
32
- end
33
- end
34
-
35
- describe ".merge_images" do
36
- it "..." do
37
- pending "Write tests"
38
- end
39
- end
40
-
41
- describe ".generate_html" do
42
- it "..." do
43
- pending "Write tests"
44
- end
45
- end
46
-
47
29
  describe "#safe" do
48
30
  before do
49
31
  @glue = Coderbits::Glue.new 'artemeff'
@@ -2,15 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Coderbits do
4
4
 
5
- describe ".new" do
5
+ describe ".new and .get" do
6
6
  it "is a Coderbits::Client" do
7
- expect(Coderbits.new('artemeff')).to be_a Coderbits::Client
8
- end
9
- end
10
-
11
- describe ".get" do
12
- it "is a Coderbits::Client" do
13
- expect(Coderbits.get('artemeff')).to be_a Hash
7
+ expect(Coderbits.new('artemeff')).to be_a Hash
14
8
  end
15
9
  end
16
10
 
@@ -3,7 +3,4 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
4
  require 'rspec'
5
5
  require 'coderbits'
6
- require 'coveralls'
7
6
  require 'fileutils'
8
-
9
- Coveralls.wear!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderbits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Artemev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-21 00:00:00.000000000 Z
11
+ date: 2013-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,20 +38,34 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: multi_json
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ~>
46
60
  - !ruby/object:Gem::Version
47
- version: '1.3'
61
+ version: '1.7'
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - ~>
53
67
  - !ruby/object:Gem::Version
54
- version: '1.3'
68
+ version: '1.7'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: sprite-factory
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - ~>
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: hashie
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
83
111
  description: Simple wrapper for the Coderbits API
84
112
  email:
85
113
  - i@artemeff.com
@@ -93,7 +121,9 @@ files:
93
121
  - Rakefile
94
122
  - coderbits.gemspec
95
123
  - lib/coderbits/client.rb
124
+ - lib/coderbits/defaults.rb
96
125
  - lib/coderbits/glue.rb
126
+ - lib/coderbits/object.rb
97
127
  - lib/coderbits/version.rb
98
128
  - lib/coderbits.rb
99
129
  - spec/coderbits/client_spec.rb