-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Error handling + API calls now made through Jammed::API.request
- Loading branch information
1 parent
2fa900e
commit 4cb7a37
Showing
15 changed files
with
140 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'httparty' | ||
require 'json' | ||
Dir[File.dirname(__FILE__) + '/jammed/*.rb'].each do |file| | ||
require file | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,43 @@ | ||
module Jammed | ||
class API | ||
|
||
HTTParty.base_uri 'http://api.thisismyjam.com/1' | ||
def self.request(method, path, opts={}) | ||
base_uri = 'http://api.thisismyjam.com/1' | ||
|
||
def request(method, api_key, path, opts={}) | ||
params = {} | ||
params[:path] = path | ||
params[:options] = opts | ||
params[:method] = method | ||
|
||
response = HTTParty.send(method, "#{base_uri}#{path}", | ||
:query => opts[:query], | ||
:body => opts[:body], | ||
:format => :plain, | ||
:headers => { | ||
"Accept" => "application/json", | ||
"Content-Type" => "application/json; charset=utf-8", | ||
"User-Agent" => "Jammed/#{Jammed::VERSION}" | ||
} | ||
) | ||
|
||
params[:response] = response.inspect.to_s | ||
|
||
case response.code | ||
when 200..201 | ||
response | ||
when 400 | ||
raise Jammed::BadRequest.new(response, params) | ||
when 404 | ||
raise Jammed::NotFound.new(response, params) | ||
when 500 | ||
raise Jammed::ServerError.new(response, params) | ||
when 502 | ||
raise Jammed::Unavailable.new(response, params) | ||
when 503 | ||
raise Jammed::RateLimited.new(response, params) | ||
else | ||
raise Jammed::InformJammed.new(response, params) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Jammed | ||
class HTTPError < StandardError | ||
attr_reader :response | ||
attr_reader :params | ||
|
||
def initialize(response, params = {}) | ||
@response = response | ||
@params = params | ||
super(response) | ||
end | ||
|
||
def to_s | ||
"#{self.class.to_s} : #{response.code} #{response.body}" | ||
end | ||
end | ||
|
||
class RateLimited < HTTPError; end | ||
class NotFound < HTTPError; end | ||
class Unavailable < HTTPError; end | ||
class InformJammed < HTTPError; end | ||
class BadRequest < HTTPError; end | ||
class ServerError < HTTPError; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
module Jammed #:nodoc: | ||
# Provides method for calling API endpoint /popular.json? | ||
class PopularJams | ||
class PopularJams < API | ||
# Calls API for popular jams | ||
# | ||
# ==== Examples | ||
# | ||
# Jammed::PopularJams.popular_jams('08972935872035') #returns a sample of popular jams | ||
def self.popular_jams(api_key) | ||
search = Search.get "/popular.json?key=#{api_key}" | ||
search['jams'] ? search['jams'] : "404 Not Found" | ||
response = request(:get, "/popular.json", :query => {:key => api_key}) | ||
JSON.parse(response.body)['jams'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
module Jammed #:nodoc: | ||
# Provides method for calling API endpoint /suggestedPeople.json? | ||
class SuggestedPeople | ||
class SuggestedPeople < API | ||
# Calls API for suggested people | ||
# | ||
# ==== Examples | ||
# | ||
# Jammed::SuggestedPeople.people('08972935872035') #returns a list of users with many followers/likes | ||
def self.people(api_key) | ||
response = Search.get "/suggestedPeople.json?key=#{api_key}" | ||
response["people"][0] ? response["people"] : "No people found" | ||
response = request(:get, "/suggestedPeople.json", :query => {:key => api_key}) | ||
JSON.parse(response.body)['people'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
module Jammed | ||
describe API do | ||
describe "#request" do | ||
it "sends a request to HTTParty" do | ||
pending | ||
end | ||
it "returns a BadRequest error when called with a bad API key" do | ||
pending | ||
end | ||
it "returns a NotFound error when called with an unknown user" do | ||
pending | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters