hotchoc 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hotchoc/api.rb CHANGED
@@ -1,44 +1,20 @@
1
1
  module Hotchoc
2
2
  module API
3
3
 
4
- def get_album(id)
5
- raise ArgumentError, "Album ID is required" unless id
6
-
7
- get("albums/#{id}")['album']
8
- end
9
-
10
4
  def get_albums(opts = {})
11
- get("albums", opts)['albums']
12
- end
13
-
14
- def get_page(id)
15
- raise ArgumentError, "Page ID is required" unless id
16
-
17
- get("pages/#{id}")['page']
5
+ get('albums', opts)['albums'].map { |album| Hotchoc::Presenters::Album.new(album) }
18
6
  end
19
7
 
20
8
  def get_pages(opts = {})
21
- get("pages", opts)['pages']
22
- end
23
-
24
- def get_post(id)
25
- raise ArgumentError, "Post ID is required" unless id
26
-
27
- get("posts/#{id}")['post']
9
+ get('pages', opts)['pages'].map { |page| Hotchoc::Presenters::Page.new(page) }
28
10
  end
29
11
 
30
12
  def get_posts(opts = {})
31
- get("posts", opts)['posts']
32
- end
33
-
34
- def get_topic(id)
35
- raise ArgumentError, "Topic ID is required" unless id
36
-
37
- get("topics/#{id}")['topic']
13
+ get('posts', opts)['posts'].map { |post| Hotchoc::Presenters::Post.new(post) }
38
14
  end
39
15
 
40
16
  def get_topics(opts = {})
41
- get("topics", opts)['topics']
17
+ get('topics', opts)['topics'].map { |topic| Hotchoc::Presenters::Topic.new(topic) }
42
18
  end
43
19
 
44
20
  end
@@ -9,4 +9,4 @@ module Hotchoc
9
9
  end
10
10
 
11
11
  end
12
- end
12
+ end
@@ -24,7 +24,7 @@ module Hotchoc
24
24
  if response.timed_out?
25
25
  raise Hotchoc::Errors::RequestError, "Request timed out"
26
26
  elsif !response.success?
27
- raise Hotchoc::Errors::RequestError, "Request failed, code #{response.code}, message: #{response.return_message}"
27
+ raise Hotchoc::Errors::RequestError, "Request failed with code #{response.code}. #{response.return_message == 'No error' ? '' : response.return_message}"
28
28
  end
29
29
  end
30
30
 
@@ -0,0 +1,65 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class Album < Hotchoc::Presenters::Base
5
+
6
+ def id
7
+ @item['id']
8
+ end
9
+
10
+ def title
11
+ @item['title']
12
+ end
13
+
14
+ def subtitle
15
+ @item['subtitle']
16
+ end
17
+
18
+ def description
19
+ @item['description']
20
+ end
21
+
22
+ def identifier
23
+ @item['identifier']
24
+ end
25
+
26
+ def status
27
+ @item['status']
28
+ end
29
+
30
+ def publish_date
31
+ Time.parse(@item['publish_date'])
32
+ end
33
+
34
+ def image_count
35
+ @item['image_count']
36
+ end
37
+
38
+ def cover
39
+ @item['cover'] ? block_for(@item['cover']) : nil
40
+ end
41
+
42
+ def blocks
43
+ @item['blocks'].to_a.map { |block| block_for(block) }
44
+ end
45
+
46
+ def topics
47
+ @item['topics'].to_a.map { |topic| Hotchoc::Presenters::Topic.new(topic) }
48
+ end
49
+
50
+ def created_at
51
+ Time.parse(@item['created_at'])
52
+ end
53
+
54
+ def updated_at
55
+ Time.parse(@item['updated_at'])
56
+ end
57
+
58
+ def inspect
59
+ %(#<Hotchoc::Presenters::Album: title="#{title}">)
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class Base
5
+
6
+ attr_reader :item
7
+
8
+ def initialize(item = {})
9
+ @item = item
10
+ end
11
+
12
+ private
13
+
14
+ def block_for(block)
15
+ case block['type']
16
+ when 'text'
17
+ Hotchoc::Presenters::Blocks::Text.new(block)
18
+ when 'image'
19
+ Hotchoc::Presenters::Blocks::Image.new(block)
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ module Hotchoc
2
+ module Presenters
3
+ module Blocks
4
+
5
+ class Base < Hotchoc::Presenters::Base
6
+
7
+ def id
8
+ @item['id']
9
+ end
10
+
11
+ def type
12
+ @item['type']
13
+ end
14
+
15
+ def position
16
+ @item['position']
17
+ end
18
+
19
+ def created_at
20
+ Time.parse(@item['created_at'])
21
+ end
22
+
23
+ def updated_at
24
+ Time.parse(@item['updated_at'])
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module Hotchoc
2
+ module Presenters
3
+ module Blocks
4
+
5
+ class Image < Hotchoc::Presenters::Blocks::Base
6
+
7
+ def title
8
+ @item['content']['title']
9
+ end
10
+
11
+ def image
12
+ @item['content']['image'] ? Hotchoc::Presenters::File.new(@item['content']['image']) : nil
13
+ end
14
+
15
+ def inspect
16
+ %(#<Hotchoc::Presenters::Blocks::Image: title="#{title}" position="#{position}">)
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Hotchoc
2
+ module Presenters
3
+ module Blocks
4
+
5
+ class Text < Hotchoc::Presenters::Blocks::Base
6
+
7
+ def markdown
8
+ @item['content']['markdown']
9
+ end
10
+
11
+ def html
12
+ @item['content']['html']
13
+ end
14
+
15
+ def inspect
16
+ %(#<Hotchoc::Presenters::Blocks::Text: position="#{position}">)
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class File < Hotchoc::Presenters::Base
5
+
6
+ def id
7
+ @item['id']
8
+ end
9
+
10
+ def name
11
+ @item['name']
12
+ end
13
+
14
+ def url
15
+ @item['url']
16
+ end
17
+
18
+ def width
19
+ @item['width']
20
+ end
21
+
22
+ def height
23
+ @item['height']
24
+ end
25
+
26
+ def size
27
+ @item['size']
28
+ end
29
+
30
+ def content_type
31
+ @item['content_type']
32
+ end
33
+
34
+ def thumbnails
35
+ if @item['thumbnails']
36
+ @item['thumbnails']['original'].map { |thumbnail| Hotchoc::Presenters::Thumbnail.new(thumbnail) } +
37
+ @item['thumbnails']['square'].map { |thumbnail| Hotchoc::Presenters::Thumbnail.new(thumbnail) }
38
+ else
39
+ []
40
+ end
41
+ end
42
+
43
+ def exif
44
+ @item['exif']
45
+ end
46
+
47
+ def created_at
48
+ Time.parse(@item['created_at'])
49
+ end
50
+
51
+ def updated_at
52
+ Time.parse(@item['updated_at'])
53
+ end
54
+
55
+ def inspect
56
+ %(#<Hotchoc::Presenters::File: name="#{name}">)
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,69 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class Page < Hotchoc::Presenters::Base
5
+
6
+ def id
7
+ @item['id']
8
+ end
9
+
10
+ def title
11
+ @item['title']
12
+ end
13
+
14
+ def subtitle
15
+ @item['subtitle']
16
+ end
17
+
18
+ def identifier
19
+ @item['identifier']
20
+ end
21
+
22
+ def status
23
+ @item['status']
24
+ end
25
+
26
+ def path
27
+ @item['path']
28
+ end
29
+
30
+ def parent_id
31
+ @item['parent_id']
32
+ end
33
+
34
+ def position
35
+ @item['position']
36
+ end
37
+
38
+ def level
39
+ @item['level']
40
+ end
41
+
42
+ def publish_date
43
+ Time.parse(@item['publish_date'])
44
+ end
45
+
46
+ def blocks
47
+ @item['blocks'].to_a.map { |block| block_for(block) }
48
+ end
49
+
50
+ def topics
51
+ @item['topics'].to_a.map { |topic| Hotchoc::Presenters::Topic.new(topic) }
52
+ end
53
+
54
+ def created_at
55
+ Time.parse(@item['created_at'])
56
+ end
57
+
58
+ def updated_at
59
+ Time.parse(@item['updated_at'])
60
+ end
61
+
62
+ def inspect
63
+ %(#<Hotchoc::Presenters::Page: title="#{title}">)
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,53 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class Post < Hotchoc::Presenters::Base
5
+
6
+ def id
7
+ @item['id']
8
+ end
9
+
10
+ def title
11
+ @item['title']
12
+ end
13
+
14
+ def subtitle
15
+ @item['subtitle']
16
+ end
17
+
18
+ def identifier
19
+ @item['identifier']
20
+ end
21
+
22
+ def status
23
+ @item['status']
24
+ end
25
+
26
+ def publish_date
27
+ Time.parse(@item['publish_date'])
28
+ end
29
+
30
+ def blocks
31
+ @item['blocks'].to_a.map { |block| block_for(block) }
32
+ end
33
+
34
+ def topics
35
+ @item['topics'].to_a.map { |topic| Hotchoc::Presenters::Topic.new(topic) }
36
+ end
37
+
38
+ def created_at
39
+ Time.parse(@item['created_at'])
40
+ end
41
+
42
+ def updated_at
43
+ Time.parse(@item['updated_at'])
44
+ end
45
+
46
+ def inspect
47
+ %(#<Hotchoc::Presenters::Post: title="#{title}">)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class Thumbnail < Hotchoc::Presenters::Base
5
+
6
+ def name
7
+ @item['name']
8
+ end
9
+
10
+ def width
11
+ @item['width']
12
+ end
13
+
14
+ def height
15
+ @item['height']
16
+ end
17
+
18
+ def square?
19
+ width == height
20
+ end
21
+
22
+ def size
23
+ @item['size']
24
+ end
25
+
26
+ def url
27
+ @item['url']
28
+ end
29
+
30
+ def inspect
31
+ %(#<Hotchoc::Presenters::Thumbnail:>)
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ module Hotchoc
2
+ module Presenters
3
+
4
+ class Topic < Hotchoc::Presenters::Base
5
+
6
+ def id
7
+ @item['id']
8
+ end
9
+
10
+ def name
11
+ @item['name']
12
+ end
13
+
14
+ def identifier
15
+ @item['identifier']
16
+ end
17
+
18
+ def created_at
19
+ Time.parse(@item['created_at'])
20
+ end
21
+
22
+ def updated_at
23
+ Time.parse(@item['updated_at'])
24
+ end
25
+
26
+ def inspect
27
+ %(#<Hotchoc::Presenters::Topic: name="#{name}">)
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Hotchoc
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/hotchoc.rb CHANGED
@@ -1,9 +1,20 @@
1
+ require 'hotchoc/presenters/base'
2
+ require 'hotchoc/presenters/album'
3
+ require 'hotchoc/presenters/blocks/base'
4
+ require 'hotchoc/presenters/blocks/image'
5
+ require 'hotchoc/presenters/blocks/text'
6
+ require 'hotchoc/presenters/file'
7
+ require 'hotchoc/presenters/page'
8
+ require 'hotchoc/presenters/post'
9
+ require 'hotchoc/presenters/thumbnail'
10
+ require 'hotchoc/presenters/topic'
11
+
12
+ require 'hotchoc/version'
1
13
  require 'hotchoc/errors'
2
14
  require 'hotchoc/api'
3
15
  require 'hotchoc/fetcher'
4
16
  require 'hotchoc/configuration'
5
17
  require 'hotchoc/client'
6
- require 'hotchoc/version'
7
18
 
8
19
  module Hotchoc
9
20
  extend Configuration
@@ -11,92 +11,52 @@ describe 'Hotchoc::API' do
11
11
 
12
12
  describe 'Albums' do
13
13
 
14
- context 'GET single item' do
15
- let(:album) do
16
- JSON.parse(File.read(response_stub('album')))['album']
17
- end
18
-
19
- it "returns a hash with the album" do
20
- expect(client.get_album('53d3a2266c6f72785d010000')).to eq album
21
- end
22
- end
23
-
24
14
  context 'GET collection' do
25
15
  let(:albums) do
26
- JSON.parse(File.read(response_stub('albums')))['albums']
16
+ JSON.parse(File.read(response_stub('albums')))['albums'].map { |album| Hotchoc::Presenters::Album.new(album) }
27
17
  end
28
18
 
29
19
  it "returns an array with the albums" do
30
- expect(client.get_albums).to eq albums
20
+ expect(client.get_albums).to have_the_same_items_as albums
31
21
  end
32
22
  end
33
23
  end
34
24
 
35
25
  describe 'Pages' do
36
26
 
37
- context 'GET single item' do
38
- let(:page) do
39
- JSON.parse(File.read(response_stub('page')))['page']
40
- end
41
-
42
- it "returns a hash with the page" do
43
- expect(client.get_page('53d3a6ee6c6f72785d260000')).to eq page
44
- end
45
- end
46
-
47
27
  context 'GET collection' do
48
28
  let(:pages) do
49
- JSON.parse(File.read(response_stub('pages')))['pages']
29
+ JSON.parse(File.read(response_stub('pages')))['pages'].map { |page| Hotchoc::Presenters::Page.new(page) }
50
30
  end
51
31
 
52
32
  it "returns an array with the pages" do
53
- expect(client.get_pages).to eq pages
33
+ expect(client.get_pages).to have_the_same_items_as pages
54
34
  end
55
35
  end
56
36
  end
57
37
 
58
38
  describe 'Posts' do
59
39
 
60
- context 'GET single item' do
61
- let(:post) do
62
- JSON.parse(File.read(response_stub('post')))['post']
63
- end
64
-
65
- it "returns a hash with the post" do
66
- expect(client.get_post('53d3a7596c6f72785d290000')).to eq post
67
- end
68
- end
69
-
70
40
  context 'GET collection' do
71
41
  let(:posts) do
72
- JSON.parse(File.read(response_stub('posts')))['posts']
42
+ JSON.parse(File.read(response_stub('posts')))['posts'].map { |post| Hotchoc::Presenters::Post.new(post) }
73
43
  end
74
44
 
75
45
  it "returns an array with the posts" do
76
- expect(client.get_posts).to eq posts
46
+ expect(client.get_posts).to have_the_same_items_as posts
77
47
  end
78
48
  end
79
49
  end
80
50
 
81
51
  describe 'Topics' do
82
52
 
83
- context 'GET single item' do
84
- let(:topic) do
85
- JSON.parse(File.read(response_stub('topic')))['topic']
86
- end
87
-
88
- it "returns a hash with the topic" do
89
- expect(client.get_topic('53d0dca56c6f7253e6060000')).to eq topic
90
- end
91
- end
92
-
93
53
  context 'GET collection' do
94
54
  let(:topics) do
95
- JSON.parse(File.read(response_stub('topics')))['topics']
55
+ JSON.parse(File.read(response_stub('topics')))['topics'].map { |topic| Hotchoc::Presenters::Topic.new(topic) }
96
56
  end
97
57
 
98
58
  it "returns an array with the topics" do
99
- expect(client.get_topics).to eq topics
59
+ expect(client.get_topics).to have_the_same_items_as topics
100
60
  end
101
61
  end
102
62
  end
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :have_the_same_items_as do |expected|
2
+ match do |actual|
3
+ actual.map(&:item) == expected.map(&:item)
4
+ end
5
+ end