Skip to content

Commit

Permalink
Improve API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Sanchez committed Jun 21, 2016
1 parent 0bcafa8 commit c53b6ac
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
2 changes: 2 additions & 0 deletions lib/refinery/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "refinery/api/pages"
require "refinery/api/resources"
require "refinery/api/images"
require "refinery/api/blog/posts"
require "refinery/api/inquiries/inquiries"

require "faraday"

Expand Down
42 changes: 42 additions & 0 deletions lib/refinery/api/blog/posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "refinery/api/helpers/client_helper"

module Refinery
module API
module Blog
class Posts
include Refinery::API::Helpers::ClientHelper

def index
config.connection.get(posts_path)
end

def show(id:)
config.connection.get(posts_path(id))
end

def create(post:)
config.connection.post(posts_path) do |req|
req.body = post.to_json
end
end

def update(id:, post:)
config.connection.put(posts_path(id)) do |req|
req.body = post.to_json
end
end

def destroy(id:)
config.connection.delete(posts_path(id))
end

private
def posts_path(id = nil)
path = api_path + "/posts"
path << "/#{id}" if id
path
end
end
end
end
end
4 changes: 0 additions & 4 deletions lib/refinery/api/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def index
config.connection.get(images_path)
end

# def product_attributes
# @_attributes ||= config.connection.get(products_path + "/new")
# end

def create(image:)
config.connection.post(images_path) do |req|
req.body = image.to_json
Expand Down
36 changes: 36 additions & 0 deletions lib/refinery/api/inquiries/inquiries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "refinery/api/helpers/client_helper"

module Refinery
module API
module Inquiries
class Inquiries
include Refinery::API::Helpers::ClientHelper

def index
config.connection.get(inquiries_path)
end

def show(id:)
config.connection.get(inquiries_path(id))
end

def create(post:)
config.connection.post(inquiries_path) do |req|
req.body = post.to_json
end
end

def destroy(id:)
config.connection.delete(inquiries_path(id))
end

private
def inquiries_path(id = nil)
path = api_path + "/inquiries"
path << "/#{id}" if id
path
end
end
end
end
end
16 changes: 6 additions & 10 deletions lib/refinery/api/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ def index
config.connection.get(resources_path)
end

# def product_attributes
# @_attributes ||= config.connection.get(products_path + "/new")
# end

# def create(resource:)
# config.connection.post(resources_path) do |req|
# req.body = resource.to_json
# end
# end
def create(resource:)
config.connection.post(resources_path) do |req|
req.body = resource.to_json
end
end

def show(id:)
config.connection.get(resources_path(id))
Expand All @@ -29,7 +25,7 @@ def update(id:, resource:)
end
end

def delete(id:)
def destroy(id:)
config.connection.delete(resources_path(id))
end

Expand Down

0 comments on commit c53b6ac

Please sign in to comment.