quintype-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cc3aac817c35e6ac9cb03b46bdecf715baee374
4
+ data.tar.gz: 953311b536e2425d47b3078f403e9ad87f6ad263
5
+ SHA512:
6
+ metadata.gz: e92ee459a05ae48f44d89a74ce77cb20a85cbf11981dd541a00a1f4bb3faab92f47d268729ea86bd9e7b51b0539af315c2486c67608ab07f0d7f544352c4b49c
7
+ data.tar.gz: 563a669888533bbcf0e25e90930717e302b7883fa2e561515ed789e026d3259b3d525af2ec27557bfdebc040a2851226f828d658b9b0027929ab31d5012d0fc1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in quintype-api.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Quintype::Api
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/quintype/api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'quintype-api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install quintype-api
22
+
23
+ ## Usage
24
+
25
+ ### Establishing Connection
26
+
27
+ First, you must establish a connection, as follows
28
+
29
+ ```ruby
30
+ Quintype::API::Client.establish_connection("http://sketches.quintype.com", Faraday.default_adapter)
31
+ ```
32
+
33
+ ### Subclassing API classes
34
+
35
+ It is highly recommended that you subclass API sections. This is mostly honored during requests
36
+
37
+ ```ruby
38
+ class QtConfig < Quintype::API::Config
39
+ end
40
+
41
+ class Story < Quintype::API::Story
42
+ end
43
+ ```
44
+
45
+ ### Fetching the Config
46
+
47
+ ```ruby
48
+ QtConfig.get.sections
49
+ ```
50
+
51
+ ### Fetching a story by slug
52
+
53
+ ```ruby
54
+ Story.find_by_slug("5-timeless-truths-from-the-serenity-prayer-that-offer-wisdom")
55
+ ```
56
+
57
+ ### Bulk Fetching stories
58
+
59
+ ```ruby
60
+ request = Quintype::API::Bulk.new
61
+ request
62
+ .add_request("entertainment", Story.bulk_stories_request("top").add_params(section: "Entertainment"))
63
+ .add_request("sports", Story.bulk_stories_request("top").add_params(section: "Sports"))
64
+ .execute!
65
+ entertainment_stories = request.get_response("entertainment")
66
+ sports_stories = request.get_response("sports")
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/quintype/quintype-api-ruby.
78
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "quintype/api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ module Quintype::API
2
+ module BaseFunctions
3
+ module ClassFunctions
4
+ def members_as_string
5
+ @members_as_string ||= members.map {|i| i.to_s.gsub(/_/, "-")}
6
+ end
7
+
8
+ def from_hash(hash, *args)
9
+ new *(args + hash.values_at(*members_as_string))
10
+ end
11
+
12
+ def coerce_array(key, clazz)
13
+ class_eval <<-EOF
14
+ def #{key.to_s}
15
+ super.map { |i| #{clazz.name}.from_hash(i)}
16
+ end
17
+ EOF
18
+ end
19
+
20
+ def coerce_key(key, clazz)
21
+ class_eval <<-EOF
22
+ def #{key.to_s}
23
+ #{clazz.name}.from_hash(i)
24
+ end
25
+ EOF
26
+ end
27
+ end
28
+
29
+ def self.included(clazz)
30
+ clazz.extend(ClassFunctions)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ module Quintype::API
2
+ class Bulk
3
+ def initialize
4
+ @requests = {}
5
+ end
6
+
7
+ def add_request(name, request)
8
+ @requests[name] = request
9
+ self
10
+ end
11
+
12
+ def execute!
13
+ requests = @requests.inject({}) do |acc, pair|
14
+ acc[pair[0]] = pair[1].to_bulk_request
15
+ acc
16
+ end
17
+ response = Client.instance.post_bulk(requests: requests).body
18
+ @responses = response["results"].inject({}) do |acc, pair|
19
+ acc[pair[0]] = @requests[pair[0]].from_bulk_response(pair[1])
20
+ acc
21
+ end
22
+ end
23
+
24
+ def get_response(name)
25
+ @responses[name]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ module Quintype::API
2
+ class ClientException < Exception
3
+ attr_reader :response
4
+
5
+ def initialize(message, response)
6
+ super(message)
7
+ @response = response
8
+ end
9
+ end
10
+
11
+ class Client
12
+ class << self
13
+ def establish_connection(host, adapter = nil)
14
+ connection = Faraday.new(host) do |conn|
15
+ conn.request :json
16
+ conn.response :json, :content_type => /\bjson$/
17
+ conn.adapter(adapter || Faraday.default_adapter)
18
+ end
19
+ @client = new(connection)
20
+ end
21
+
22
+ def instance
23
+ @client
24
+ end
25
+ end
26
+
27
+ def initialize(conn)
28
+ @conn = conn
29
+ end
30
+
31
+ def get_config
32
+ get("/api/v1/config")
33
+ end
34
+
35
+ def get_story_by_slug(slug)
36
+ get("/api/v1/stories-by-slug", slug: slug)
37
+ end
38
+
39
+ def post_bulk(requests)
40
+ post("/api/v1/bulk", requests)
41
+ end
42
+
43
+ private
44
+ def get(url, params = {})
45
+ parse_response @conn.get(url, params)
46
+ end
47
+
48
+ def post(url, body, params = {})
49
+ parse_response @conn.post(url, body, params)
50
+ end
51
+
52
+ def parse_response(response)
53
+ if(response.status < 400)
54
+ response
55
+ else
56
+ raise ClientException.new("API returned a non successful response", response)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ module Quintype::API
2
+ class Config < Base(:sketches_host, :sections, :layout, :cdn_name, :publisher_id, :story_slug_format, :cdn_image)
3
+ class << self
4
+ def get
5
+ from_hash(Client.instance.get_config.body)
6
+ end
7
+ end
8
+
9
+ coerce_array :sections, Section
10
+
11
+ def menu_items
12
+ layout["menu"].map { |i| MenuItem.from_hash(i, self) }
13
+ end
14
+
15
+ def stacks
16
+ StackCollection.new layout["stacks"].map { |i| Stack.from_hash(i) }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Quintype::API
2
+ class MenuItem < Base(:id, :item_id, :rank, :title, :item_type, :tag_name, :section_slug, :parent_id, :data)
3
+
4
+ def initialize(config, *args)
5
+ @config = config
6
+ super(*args)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Quintype::API
2
+ class Section < Base(:id, :name, :display_name, :slug, :parent_id)
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Quintype::API
2
+ class Stack < Base(:show_on_locations, :background_color, :rank, :story_group, :max_stories, :id, :show_on_all_sections?, :heading)
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ module Quintype::API
2
+ class StackCollection
3
+ include Enumerable
4
+
5
+ def initialize(stacks)
6
+ @stacks = stacks
7
+ end
8
+
9
+ def each
10
+ @stacks.each { |i| yield i }
11
+ end
12
+
13
+ def for_location(location)
14
+ select { |stack| stack.show_on_all_sections? || stack.show_on_locations.include?(location)}
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ module Quintype::API
2
+ class StoriesRequest
3
+ def initialize(klazz, story_group)
4
+ @klazz = klazz
5
+ @params = {"story-group" => story_group}
6
+ end
7
+
8
+ def add_params(params)
9
+ @params.merge!(params)
10
+ self
11
+ end
12
+
13
+ def to_bulk_request
14
+ @params.merge(_type: "stories")
15
+ end
16
+
17
+ def from_bulk_response(response)
18
+ response["stories"].map {|i| @klazz.from_hash(i) }
19
+ end
20
+ end
21
+
22
+ class Story < Base(:updated_at, :assignee_id, :author_name, :tags, :headline, :storyline_id, :votes, :story_content_id, :slug, :last_published_at, :sections, :content_created_at, :owner_name, :custom_slug, :push_notification, :publisher_id, :hero_image_metadata, :comments, :published_at, :storyline_title, :summary, :autotags, :status, :bullet_type, :id, :hero_image_s3_key, :cards, :story_version_id, :content_updated_at, :author_id, :owner_id, :first_published_at, :hero_image_caption, :version, :story_template, :created_at, :authors, :metadata, :publish_at, :assignee_name)
23
+
24
+ class << self
25
+ def find_by_slug(slug)
26
+ from_hash(Client.instance.get_story_by_slug(slug).body["story"])
27
+ end
28
+
29
+ def bulk_stories_request(story_group)
30
+ StoriesRequest.new(self, story_group)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ module Quintype
2
+ module Api
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require "quintype/api/version"
2
+
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+
6
+ module Quintype
7
+ module API
8
+ autoload :Client, "quintype/api/client"
9
+ autoload :BaseFunctions, "quintype/api/base_functions"
10
+
11
+ def self.Base(*args)
12
+ clazz = Struct.new(*args)
13
+ clazz.send(:include, BaseFunctions)
14
+ clazz
15
+ end
16
+
17
+ autoload :Config, "quintype/api/config"
18
+ autoload :Section, "quintype/api/section"
19
+ autoload :MenuItem, "quintype/api/menu_item"
20
+ autoload :Stack, "quintype/api/stack"
21
+ autoload :StackCollection, "quintype/api/stack_collection"
22
+ autoload :Story, "quintype/api/story"
23
+ autoload :Bulk, "quintype/api/bulk"
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quintype/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quintype-api"
8
+ spec.version = Quintype::Api::VERSION
9
+ spec.authors = ["Tejas Dinkar"]
10
+ spec.email = ["tejas@gja.in"]
11
+
12
+ spec.summary = %q{Ruby Gem to Access the Quintype API}
13
+ spec.homepage = "https://github.com/quintype/quintype-api-ruby"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "faraday", "~> 0.9.2"
21
+ spec.add_dependency "faraday_middleware", "~> 0.10.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quintype-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tejas Dinkar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - tejas@gja.in
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - README.md
79
+ - Rakefile
80
+ - bin/console
81
+ - bin/setup
82
+ - lib/quintype/api.rb
83
+ - lib/quintype/api/base_functions.rb
84
+ - lib/quintype/api/bulk.rb
85
+ - lib/quintype/api/client.rb
86
+ - lib/quintype/api/config.rb
87
+ - lib/quintype/api/menu_item.rb
88
+ - lib/quintype/api/section.rb
89
+ - lib/quintype/api/stack.rb
90
+ - lib/quintype/api/stack_collection.rb
91
+ - lib/quintype/api/story.rb
92
+ - lib/quintype/api/version.rb
93
+ - quintype-api.gemspec
94
+ homepage: https://github.com/quintype/quintype-api-ruby
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Ruby Gem to Access the Quintype API
117
+ test_files: []