hotchoc 0.0.1 → 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: 712dd4cc4edbc921ba19da7b36c609597ba489f5
4
- data.tar.gz: f5d5f5eefea75d10a73a2f7f95038039d96056a5
3
+ metadata.gz: 772df776e809aa41302d368813a4378aed3f4cc0
4
+ data.tar.gz: 060a43cfd5352ab0ac2ee6aa7e637efc28c5314f
5
5
  SHA512:
6
- metadata.gz: 0bc4281b3606d88dc0c5a7ddd47c7d4a3a60de153938d321a65a57ea5bf277dbbcff93023e6aa24f7e73b01462bdc5b3958b035ceeceb97b552029321e572f8c
7
- data.tar.gz: 5d864289c2202cb28ae3cf878e111cfd313ad3c93e569b580956faea2d91ddf4e55310c713f867dce344164f64df9589ea27750a437a83689602877f65038b79
6
+ metadata.gz: 0485bb80b24c5520f951394ecb880214f89ab800a27d2d2d01bbf5350c9a3bb172622fa5a12f3f51ea7f119a144b12a68d6c545f07e8fdc154aae4529f9644dd
7
+ data.tar.gz: 8ef2d166f82a28e3f67ade1a05f2d68cc93acacce629a00b2ba0ec713a34ad19ddd9d68007ae13ff05fb98f238e8f3ab2246b6d9861a76d9c72293e2bba15486
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ [Full changelog](https://github.com/choc/hotchoc-ruby/compare/v0.0.1...v0.1.0)
6
+
7
+ Major changes:
8
+
9
+ * Require authenticated requests
10
+ * Removed format option
11
+
12
+ Enhancements:
13
+
14
+ * Basic GET support for albums, pages, posts and topics API
15
+
16
+ ## 0.0.1
17
+
18
+ Initial release.
data/README.md CHANGED
@@ -12,21 +12,46 @@ And then execute:
12
12
 
13
13
  $ bundle
14
14
 
15
- Or install it yourself as:
15
+ Or install it manually as:
16
16
 
17
17
  $ gem install hotchoc
18
18
 
19
19
  ## Usage
20
20
 
21
- ### Rails
21
+ client = Hotchoc::Client.new(
22
+ api_key: 'abcde', # Your API key
23
+ site: 'mysite' # Short name of your site
24
+ )
22
25
 
23
- Add an initializer ```config/initializers/hotchoc.rb``` with your details:
26
+ ## Examples
24
27
 
25
- Hotchoc.configure do |config|
26
- config.api_key = 'abcde' # Your API key
27
- config.site = 'mysite' # Short name of your site
28
- config.format = :json # Can be :json (default) or :xml
29
- end
28
+ # Get single album by ID
29
+ client.get_album('53d0dcdc6c6f725407000000')
30
+ => {"id"=>"53d0dcdc6c6f725407000000", "title"=>"New album", "subtitle"=>"A subtitle", ...
31
+
32
+ # Get all albums
33
+ client.get_albums
34
+ => [{"id"=>"53d0dcdc6c6f725407000000", "title"=>"New album", "subtitle"=>"A subtitle", ...
35
+
36
+ # Get only first 10 albums
37
+ client.get_albums(limit: 10)
38
+ => [{"id"=>"53d0dcdc6c6f725407000000", "title"=>"New album", "subtitle"=>"A subtitle", ...
39
+
40
+ ## API
41
+
42
+ ### Get single resource
43
+
44
+ client.get_album(id)
45
+ client.get_page(id)
46
+ client.get_post(id)
47
+ client.get_topic(id)
48
+
49
+ ### Get collection
50
+
51
+ client.get_albums
52
+ client.get_pages
53
+ client.get_posts
54
+ client.get_topics
30
55
 
31
56
  ## Contributing
32
57
 
@@ -40,4 +65,9 @@ Add an initializer ```config/initializers/hotchoc.rb``` with your details:
40
65
 
41
66
  Copyright (c) 2014 Matthias Siegel. See [LICENSE][] for details.
42
67
 
68
+ ## Changelog
69
+
70
+ See [CHANGELOG][] for details.
71
+
43
72
  [license]: LICENSE.md
73
+ [changelog]: CHANGELOG.md
data/hotchoc.gemspec CHANGED
@@ -16,7 +16,10 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(spec)/})
17
17
  spec.require_paths = ["lib"]
18
18
 
19
+ spec.add_dependency "typhoeus", "~> 0.6.9"
20
+
19
21
  spec.add_development_dependency "bundler", "~> 1.5"
20
- spec.add_development_dependency "rake"
21
- spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "webmock", "~> 1.18"
22
25
  end
@@ -0,0 +1,45 @@
1
+ module Hotchoc
2
+ module API
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
+ 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']
18
+ end
19
+
20
+ 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']
28
+ end
29
+
30
+ 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']
38
+ end
39
+
40
+ def get_topics(opts = {})
41
+ get("topics", opts)['topics']
42
+ end
43
+
44
+ end
45
+ end
@@ -1,26 +1,37 @@
1
1
  module Hotchoc
2
2
  class Client
3
+ include Hotchoc::API
3
4
 
4
5
  #
5
6
  # Define the same set of accessors as in the Hotchoc module
6
7
  #
7
8
  attr_accessor *Configuration::VALID_CONFIG_KEYS
8
9
 
9
- def initialize(options = {})
10
- merged_options = Hotchoc.options.merge(options)
10
+ def initialize(opts = {})
11
+ options = Hotchoc.options.merge(opts)
12
+
13
+ raise ArgumentError, "API key is required" unless options[:api_key]
14
+ raise ArgumentError, "Site name is required" unless options[:site]
11
15
 
12
16
  #
13
17
  # Copy the merged values to this client and ignore those
14
18
  # not part of the configuration.
15
19
  #
16
20
  Configuration::VALID_CONFIG_KEYS.each do |key|
17
- send("#{key}=", merged_options[key])
21
+ send("#{key}=", options[key])
18
22
  end
19
23
  end
20
24
 
21
- def user_agent
22
- @user_agent ||= "Hotchoc gem #{Hotchoc::VERSION}"
25
+ private
26
+
27
+ def get(path, opts = {})
28
+ url = "https://#{site}.#{hostname}/api/#{path}"
29
+
30
+ Fetcher.get(url, merged_options(opts), verify)
23
31
  end
24
32
 
33
+ def merged_options(opts)
34
+ { api_key: api_key }.merge(opts)
35
+ end
25
36
  end
26
37
  end
@@ -1,17 +1,17 @@
1
1
  module Hotchoc
2
2
  module Configuration
3
3
 
4
- VALID_CONFIG_KEYS = [:api_key, :site, :hotchoc_hostname, :format].freeze
4
+ VALID_CONFIG_KEYS = [:api_key, :site, :hostname, :verify].freeze
5
5
 
6
6
  DEFAULT_API_KEY = nil
7
7
  DEFAULT_SITE = nil
8
- DEFAULT_HOTCHOC_HOSTNAME = 'hotchoc.dev'
9
- DEFAULT_FORMAT = :json
8
+ DEFAULT_HOSTNAME = 'hotchoc.io'
9
+ DEFAULT_VERIFY = true
10
10
 
11
11
  #
12
12
  # Build accessor methods for every config options so we can do this, for example:
13
13
  #
14
- # Hotchoc.format = :xml
14
+ # Hotchoc.site = 'mattdownunder'
15
15
  #
16
16
  attr_accessor *VALID_CONFIG_KEYS
17
17
 
@@ -25,17 +25,12 @@ module Hotchoc
25
25
  def reset
26
26
  self.api_key = DEFAULT_API_KEY
27
27
  self.site = DEFAULT_SITE
28
- self.hotchoc_hostname = DEFAULT_HOTCHOC_HOSTNAME
29
- self.format = DEFAULT_FORMAT
30
- end
31
-
32
- def configure
33
- yield self
28
+ self.hostname = DEFAULT_HOSTNAME
29
+ self.verify = DEFAULT_VERIFY
34
30
  end
35
31
 
36
32
  def options
37
33
  Hash[*VALID_CONFIG_KEYS.map { |key| [key, send(key)] }.flatten]
38
34
  end
39
-
40
35
  end
41
36
  end
@@ -0,0 +1,12 @@
1
+ module Hotchoc
2
+ module Errors
3
+
4
+ # Base error class for all custom errors
5
+ class HotchocError < StandardError
6
+ end
7
+
8
+ class RequestError < HotchocError
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ require 'typhoeus'
2
+ require 'json'
3
+
4
+ module Hotchoc
5
+ class Fetcher
6
+
7
+ DEFAULT_OPTIONS = {
8
+ headers: {
9
+ Accept: "application/json",
10
+ "User-Agent" => "Hotchoc Ruby #{Hotchoc::VERSION}"
11
+ },
12
+ followlocation: true,
13
+ accept_encoding: 'gzip'
14
+ }
15
+
16
+ def self.get(url, opts, verify)
17
+ request = Typhoeus::Request.new(url, {
18
+ method: :get,
19
+ params: opts,
20
+ ssl_verifypeer: verify
21
+ }.merge(DEFAULT_OPTIONS))
22
+
23
+ request.on_complete do |response|
24
+ if response.timed_out?
25
+ raise Hotchoc::Errors::RequestError, "Request timed out"
26
+ elsif !response.success?
27
+ raise Hotchoc::Errors::RequestError, "Request failed, code #{response.code}, message: #{response.return_message}"
28
+ end
29
+ end
30
+
31
+ JSON.parse(request.run.body)
32
+ end
33
+
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Hotchoc
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/hotchoc.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'hotchoc/errors'
2
+ require 'hotchoc/api'
3
+ require 'hotchoc/fetcher'
1
4
  require 'hotchoc/configuration'
2
5
  require 'hotchoc/client'
3
6
  require 'hotchoc/version'
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Hotchoc::API' do
4
+
5
+ let(:client) do
6
+ Hotchoc::Client.new({
7
+ api_key: 'test',
8
+ site: 'test'
9
+ })
10
+ end
11
+
12
+ describe 'Albums' do
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
+ context 'GET collection' do
25
+ let(:albums) do
26
+ JSON.parse(File.read(response_stub('albums')))['albums']
27
+ end
28
+
29
+ it "returns an array with the albums" do
30
+ expect(client.get_albums).to eq albums
31
+ end
32
+ end
33
+ end
34
+
35
+ describe 'Pages' do
36
+
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
+ context 'GET collection' do
48
+ let(:pages) do
49
+ JSON.parse(File.read(response_stub('pages')))['pages']
50
+ end
51
+
52
+ it "returns an array with the pages" do
53
+ expect(client.get_pages).to eq pages
54
+ end
55
+ end
56
+ end
57
+
58
+ describe 'Posts' do
59
+
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
+ context 'GET collection' do
71
+ let(:posts) do
72
+ JSON.parse(File.read(response_stub('posts')))['posts']
73
+ end
74
+
75
+ it "returns an array with the posts" do
76
+ expect(client.get_posts).to eq posts
77
+ end
78
+ end
79
+ end
80
+
81
+ describe 'Topics' do
82
+
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
+ context 'GET collection' do
94
+ let(:topics) do
95
+ JSON.parse(File.read(response_stub('topics')))['topics']
96
+ end
97
+
98
+ it "returns an array with the topics" do
99
+ expect(client.get_topics).to eq topics
100
+ end
101
+ end
102
+ end
103
+ end
@@ -6,66 +6,34 @@ describe 'Hotchoc::Client' do
6
6
  @keys = Hotchoc::Configuration::VALID_CONFIG_KEYS
7
7
  end
8
8
 
9
- describe 'with module configuration' do
10
-
9
+ describe 'with class configuration' do
11
10
  before do
12
- Hotchoc.configure do |config|
13
- @keys.each do |key|
14
- config.send("#{key}=", key)
15
- end
16
- end
17
- end
18
-
19
- after do
20
- Hotchoc.reset
11
+ @config = {
12
+ api_key: 'abc',
13
+ site: 'def',
14
+ hostname: 'ghi',
15
+ verify: false
16
+ }
21
17
  end
22
18
 
23
- it "should inherit module configuration" do
24
- api = Hotchoc::Client.new
19
+ it 'should override module configuration' do
20
+ client = Hotchoc::Client.new(@config)
25
21
 
26
22
  @keys.each do |key|
27
- api.send(key).should == key
23
+ expect(client.send(key)).to eq @config[key]
28
24
  end
29
25
  end
30
26
 
31
- describe 'with class configuration' do
27
+ it 'should override module configuration after' do
28
+ client = Hotchoc::Client.new(api_key: 'rst', site: 'uvw', hostname: 'xyz', verify: true)
32
29
 
33
- before do
34
- @config = {
35
- api_key: 'abc',
36
- site: 'def',
37
- hotchoc_hostname: 'ghi',
38
- format: 'jkl'
39
- }
30
+ @config.each do |key, value|
31
+ client.send("#{key}=", value)
40
32
  end
41
33
 
42
- it 'should override module configuration' do
43
- api = Hotchoc::Client.new(@config)
44
-
45
- @keys.each do |key|
46
- api.send(key).should == @config[key]
47
- end
48
- end
49
-
50
- it 'should override module configuration after' do
51
- api = Hotchoc::Client.new
52
-
53
- @config.each do |key, value|
54
- api.send("#{key}=", value)
55
- end
56
-
57
- @keys.each do |key|
58
- api.send("#{key}").should == @config[key]
59
- end
34
+ @keys.each do |key|
35
+ expect(client.send("#{key}")).to eq @config[key]
60
36
  end
61
37
  end
62
38
  end
63
-
64
- describe ".user_agent" do
65
-
66
- it "should return the user agent with the current version" do
67
- Hotchoc::Client.new.user_agent.should == "Hotchoc gem #{Hotchoc::VERSION}"
68
- end
69
- end
70
-
71
39
  end
@@ -9,19 +9,7 @@ describe 'Hotchoc::Configuration' do
9
9
  Hotchoc::Configuration::VALID_CONFIG_KEYS.each do |key|
10
10
  describe ".#{key}" do
11
11
  it "should return the default #{key}" do
12
- Hotchoc.send(key).should == Hotchoc::Configuration.const_get("DEFAULT_#{key.upcase}")
13
- end
14
- end
15
- end
16
-
17
- describe '.configure' do
18
-
19
- Hotchoc::Configuration::VALID_CONFIG_KEYS.each do |key|
20
- it "should set the #{key}" do
21
- Hotchoc.configure do |config|
22
- config.send("#{key}=", key)
23
- Hotchoc.send(key).should == key
24
- end
12
+ expect(Hotchoc.send(key)).to eq Hotchoc::Configuration.const_get("DEFAULT_#{key.upcase}")
25
13
  end
26
14
  end
27
15
  end
@@ -3,6 +3,6 @@ require 'spec_helper'
3
3
  describe 'Hotchoc' do
4
4
 
5
5
  it 'should have a version' do
6
- Hotchoc::VERSION.should_not be_nil
6
+ expect(Hotchoc::VERSION).to_not be_nil
7
7
  end
8
8
  end
data/spec/spec_helper.rb CHANGED
@@ -2,9 +2,13 @@ require 'bundler/setup'
2
2
  Bundler.setup
3
3
 
4
4
  require 'hotchoc'
5
+ require 'webmock/rspec'
6
+
7
+ WebMock.disable_net_connect!
8
+
9
+ Dir.glob("#{File.dirname(__FILE__)}/support/**/*.rb").each { |f| require f }
5
10
 
6
11
  RSpec.configure do |config|
7
- config.treat_symbols_as_metadata_keys_with_true_values = true
8
12
  config.run_all_when_everything_filtered = true
9
13
  config.filter_run :focus
10
14
 
@@ -0,0 +1,3 @@
1
+ def response_stub(name)
2
+ "#{File.dirname(__FILE__)}/responses/#{name}.json"
3
+ end