twenty20 0.0.3 → 0.0.4

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: f4a9f6b147774926e206dc91a7710b4c0a200627
4
- data.tar.gz: 1501033229edd7d185bf0ced06a5883ef2684a79
3
+ metadata.gz: 7fb6d625e1fb0923b08f12c8913130fd12f7adbe
4
+ data.tar.gz: 08d133954558cdd806bfd1fed577a2d3ee7884dc
5
5
  SHA512:
6
- metadata.gz: c1af16a8ece35a9ddb302776a2c938833d1e072180b06b11ea05b9b50816726b4611b0a4b1935c69254e46b7fa0ace6dd050a580b8df53e770dace0a5878549a
7
- data.tar.gz: b14966f2f3d0a055601c4391d4f536cabaf5d00687dcee8ff960d51b1b1efcfb63ea147eff3787a66984b2ce50e1bc0c69e95549f7b9949924e08784414ac362
6
+ metadata.gz: 86618150aabf23d6ab3e0fc7549538a7c77bb0cf7b00de58f3192e956f4191dcd5b3befb48c6fc3af2666cbaf98a1219089b63f3a561c41c383f4bf8bdee9c12
7
+ data.tar.gz: 4613f7c1e8a0f54da3e28be38bc81fc50bd28c62c0480204c790393253e6b3a98c19f36aea81e0dd4bf55417aecd4532f7843e6894f0def59db38e0a8aa2a3b5
@@ -1,62 +1,10 @@
1
- require "twenty20/version"
2
- require "twenty20/item"
3
- require "twenty20/challenge"
4
- require "httparty"
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/hash/conversions'
3
+ require 'json'
5
4
 
6
- module Twenty20
7
- class Client
8
- include HTTParty
9
- BASE_URI = "https://api-v2.twenty20.com/"
10
-
11
- def get_featured_items(&block)
12
- featured_route = "items/featured"
13
- featured_params = "?featured=true"
14
- resource = build_route(featured_route, featured_params)
15
- response = self.class.get(resource)
16
- if(response.code == 200)
17
- item_collection = to_collection(response, "item")
18
- if(block_given?)
19
- item_collection.each do |item|
20
- block.call(item)
21
- end
22
- else
23
- item_collection
24
- end
25
- else
26
- "Error"
27
- end
28
- end
29
-
30
- def get_challenges(&block)
31
- challenges_route = "/challenges/open-for-submissions"
32
- resource = build_route(challenges_route)
33
- response = self.class.get(resource)
34
- if(response.code == 200)
35
- challenge_collection = to_collection(response, "challenge")
36
- if block_given?
37
- challenge_collection.each do |challenge|
38
- block.call(challenge)
39
- end
40
- else
41
- challenge_collection
42
- end
43
- else
44
- "Error"
45
- end
46
- end
47
-
48
- private
49
-
50
- def to_collection(response, collection_class)
51
- response.parsed_response[collection_class + "s"].collect do |e|
52
- o = Object.const_get("Twenty20::" + collection_class.capitalize) #creates new array full of collection_class object (i.e. Items, Challenges)
53
- o.new(e)
54
- end
55
- end
5
+ %w(version client api_object item challenge).each do |file|
6
+ require "twenty20/#{file}"
7
+ end
56
8
 
57
- def build_route(uri, params = "")
58
- route = BASE_URI + uri + params
59
- route
60
- end
61
- end
9
+ module Twenty20
62
10
  end
@@ -0,0 +1,14 @@
1
+ module Twenty20
2
+ class ApiObject
3
+ def self.attributes
4
+ []
5
+ end
6
+
7
+ def initialize(attributes = {})
8
+ attributes.each do |key, value|
9
+ self.class.class_eval{attr_reader key.to_sym}
10
+ instance_variable_set("@#{key}", value)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,14 +1,4 @@
1
1
  module Twenty20
2
- class Challenge
3
- attr_reader :slug, :name, :description, :starts_at, :ends_at, :submission_count
4
-
5
- def initialize(attributes = {})
6
- @slug = attributes["slug"]
7
- @name = attributes["name"]
8
- @description = attributes["description"]
9
- @starts_at = attributes["starts_at"]
10
- @ends_at = attributes["ends_at"]
11
- @submission_count = attributes["submission_count"]
12
- end
2
+ class Challenge < ApiObject
13
3
  end
14
4
  end
@@ -0,0 +1,47 @@
1
+ require 'net/http'
2
+
3
+ module Twenty20
4
+ class Client
5
+ BASE_URI = 'https://api-v2.twenty20.com/'
6
+
7
+ def get_featured_items(&block)
8
+ resource = build_route('items/featured', featured: true)
9
+ response_for(resource, :item, &block)
10
+ end
11
+
12
+ def get_challenges(&block)
13
+ resource = build_route('/challenges/open-for-submissions')
14
+ response_for(resource, :challenge, &block)
15
+ end
16
+
17
+ private
18
+
19
+ def response_for(resource, type, &block)
20
+ uri = URI(resource)
21
+ response = Net::HTTP.get_response(uri) #returns response as a string
22
+
23
+ return 'Error' unless response.code == "200"
24
+
25
+ collection = to_collection(response, type)
26
+
27
+ return collection unless block_given?
28
+
29
+ collection.each do |item|
30
+ block.call(item)
31
+ end
32
+ end
33
+
34
+ def to_collection(response, collection_class)
35
+ collection_class = collection_class.to_s
36
+ parsed_response = JSON.parse(response.body) #parses response as JSON - originally string
37
+
38
+ parsed_response[collection_class.pluralize].map do |e|
39
+ "Twenty20::#{collection_class.singularize.classify}".constantize.new(e)
40
+ end
41
+ end
42
+
43
+ def build_route(uri, params = {})
44
+ BASE_URI + uri + '?' + params.to_query
45
+ end
46
+ end
47
+ end
@@ -1,19 +1,4 @@
1
1
  module Twenty20
2
- class Item
3
- attr_reader :caption, :id, :source_name, :image_url, :featured_at, :user_first_name,
4
- :user_last_name, :user_avatar_url, :username, :display_name
5
- def initialize(attributes = {}) #set default so attributes will default to nil and not throw exception if not present
6
- @caption = attributes["caption"]
7
- @id = attributes["id"]
8
- @source_name = attributes["source_name"]
9
- @image_url = attributes["standard_url"]
10
- @featured_at = attributes["featured_at"]
11
- @user_first_name = attributes["first_name"]
12
- @user_last_name = attributes["last_name"]
13
- @user_avatar_url = attributes["avatar"]
14
- @username = attributes["username"]
15
- @display_name = attributes["display_name"]
16
- end
2
+ class Item < ApiObject
17
3
  end
18
4
  end
19
-
@@ -1,3 +1,3 @@
1
1
  module Twenty20
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1 +1,18 @@
1
1
  require 'twenty20'
2
+ require 'webmock/rspec'
3
+ require 'vcr'
4
+
5
+ Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ VCR.configure do |c|
10
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
11
+ c.hook_into :webmock # or :fakeweb
12
+ end
13
+
14
+
15
+ RSpec.configure do |config|
16
+ config.expose_current_running_example_as :example
17
+ config.raise_errors_for_deprecations!
18
+ end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twenty20
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Wheeler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-30 00:00:00.000000000 Z
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: httparty
14
+ name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -54,6 +54,48 @@ dependencies:
54
54
  version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
57
99
  requirement: !ruby/object:Gem::Requirement
58
100
  requirements:
59
101
  - - ">="
@@ -77,14 +119,13 @@ files:
77
119
  - README.md
78
120
  - Rakefile
79
121
  - lib/twenty20.rb
122
+ - lib/twenty20/api_object.rb
80
123
  - lib/twenty20/challenge.rb
124
+ - lib/twenty20/client.rb
81
125
  - lib/twenty20/item.rb
82
126
  - lib/twenty20/version.rb
83
- - spec/challenge_spec.rb
84
- - spec/client_spec.rb
85
- - spec/item_spec.rb
86
127
  - spec/spec_helper.rb
87
- homepage: ''
128
+ homepage: https://github.com/alexwheeler/twenty20_API
88
129
  licenses:
89
130
  - MIT
90
131
  metadata: {}
@@ -109,7 +150,4 @@ signing_key:
109
150
  specification_version: 4
110
151
  summary: explore twenty20
111
152
  test_files:
112
- - spec/challenge_spec.rb
113
- - spec/client_spec.rb
114
- - spec/item_spec.rb
115
153
  - spec/spec_helper.rb
@@ -1,35 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Twenty20::Challenge do
4
- describe "slug" do
5
-
6
- before(:all) do
7
- attributes = {"slug" => "technology-challenge"}
8
- @challenge = Twenty20::Challenge.new(attributes)
9
- end
10
-
11
- it "returns the slug for the challenge" do
12
- expect(@challenge.slug).to eq("technology-challenge")
13
- end
14
-
15
- it "should be immutable" do
16
- expect{@challenge.slug = "some new slug"}.to raise_error(NoMethodError)
17
- end
18
- end
19
-
20
- describe "name" do
21
- it "returns the name of the challenge" do
22
- attributes = {"name" => "technology"}
23
- challenge = Twenty20::Challenge.new(attributes)
24
- expect(challenge.name).to eq("technology")
25
- end
26
- end
27
-
28
- describe "description" do
29
- it "returns the description of the challenge" do
30
- attributes = {"description" => "really cool challenge"}
31
- challenge = Twenty20::Challenge.new(attributes)
32
- expect(challenge.description).to eq("really cool challenge")
33
- end
34
- end
35
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Twenty20::Client do
4
-
5
- describe "BASE_URI" do
6
- it "returns the base uri for the api" do
7
- expect(Twenty20::Client::BASE_URI).to eq("https://api-v2.twenty20.com/")
8
- end
9
- end
10
-
11
- describe "get_featured_items" do
12
-
13
- before(:all) do
14
- @client = Twenty20::Client.new
15
- end
16
-
17
- it "returns an Array" do
18
- expect(@client.get_featured_items.class).to eq(Array)
19
- end
20
-
21
- it "returns an Array of Item objects" do
22
- expect(@client.get_featured_items[0].class).to eq(Twenty20::Item)
23
- end
24
-
25
- it "returns error if status is not 200" do
26
- fake_response = double
27
- fake_response.stub(:code) { 400 }
28
- Twenty20::Client.stub(:get) { fake_response }
29
- response = @client.get_featured_items
30
- expect(response).to eq("Error")
31
- end
32
-
33
- it "can accept a block and do something cool like push challenges into a new array" do
34
- val = []
35
- @client.get_featured_items {|item| val.push(item)}
36
- expect(val.empty?).to_not eq(true)
37
-
38
- end
39
-
40
- end
41
-
42
- describe "get_challenges" do
43
- before(:all) do
44
- @client = Twenty20::Client.new
45
- end
46
-
47
- it "should return an array of challenge objects" do
48
- response = @client.get_challenges
49
- expect(response.class).to eq(Array);
50
- end
51
-
52
- it "can accept a block and do something cool like push challenges into a new array" do
53
- val = []
54
- @client.get_challenges {|challenge| val.push(challenge)}
55
- expect(val.empty?).to_not eq(true)
56
- end
57
- end
58
- end
@@ -1,47 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Twenty20::Item do
4
- describe "category" do
5
- it "returns the items category" do
6
- attributes = {"caption" => "mobile photography ftw"}
7
- item = Twenty20::Item.new(attributes)
8
- expect(item.caption).to eq("mobile photography ftw")
9
- end
10
-
11
- it "returns nil when not present" do
12
- item = Twenty20::Item.new
13
- expect(item.caption).to eq(nil)
14
- end
15
-
16
- it "should be immutable" do
17
- item = Twenty20::Item.new
18
- expect{item.category = "not allowed"}.to raise_error(NoMethodError)
19
- end
20
- end
21
-
22
- describe "id" do
23
- it "returns the items id " do
24
- attributes = {"id" => 1}
25
- item = Twenty20::Item.new(attributes)
26
- expect(item.id).to eq(1)
27
- end
28
-
29
- it "returns nil when not present" do
30
- item = Twenty20::Item.new
31
- expect(item.id).to eq(nil)
32
- end
33
- end
34
-
35
- describe "source_name" do
36
- it "returns the items id " do
37
- attributes = {"source_name" => "Instagram"}
38
- item = Twenty20::Item.new(attributes)
39
- expect(item.source_name).to eq("Instagram")
40
- end
41
-
42
- it "returns nil when not present" do
43
- item = Twenty20::Item.new
44
- expect(item.source_name).to eq(nil)
45
- end
46
- end
47
- end