Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Fastly::Dictionary#item #89

Merged
merged 4 commits into from Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/fastly/client.rb
Expand Up @@ -6,6 +6,9 @@
class Fastly
# The UserAgent to communicate with the API
class Client #:nodoc: all

DEFAULT_URL = 'https://api.fastly.com'.freeze

attr_accessor :http, :api_key, :user, :password, :cookie, :customer

def initialize(opts)
Expand All @@ -15,7 +18,7 @@ def initialize(opts)
@customer = opts.fetch(:customer, nil)
@oldpurge = opts.fetch(:use_old_purge_method, false)

base = opts.fetch(:base_url, 'https://api.fastly.com')
base = opts.fetch(:base_url, DEFAULT_URL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

uri = URI.parse(base)
options = if uri.is_a? URI::HTTPS
{
Expand Down
9 changes: 9 additions & 0 deletions lib/fastly/dictionary.rb
Expand Up @@ -6,6 +6,15 @@ def items
fetcher.list_dictionary_items(:service_id => service_id, :dictionary_id => id)
end

# Returns a Fastly::DictionaryItem corresponding to this dictionary and the +key+
#
# * +key+ - Key of the dictionary item
def item(key)
fetcher.get_dictionary_item(service_id, id, key)
rescue Fastly::Error => e
raise unless e.message =~ /Record not found/
end

def add_item(key, value)
fetcher.create_dictionary_item(service_id: service_id, dictionary_id: id, item_key: key, item_value: value)
end
Expand Down
53 changes: 53 additions & 0 deletions test/fastly/dictionary_test.rb
@@ -0,0 +1,53 @@
require_relative '../test_helper'

describe Fastly::Dictionary do

let(:client) { Fastly.new(user: 'test@example.com', password: 'password') }
let(:service_id) { SecureRandom.hex(6) }
let(:version) { 1 }
let(:dictionary) { Fastly::Dictionary.new({id: SecureRandom.hex(6), service_id: service_id, version: 1}, client) }

before {
stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200, headers: { 'Set-Cookie' => 'tasty!' })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eyeroll.

}

describe '#item' do
it 'returns the nil when item is not present' do
item_key = 'key'
get_item_url = "#{Fastly::Client::DEFAULT_URL}/service/#{service_id}/dictionary/#{dictionary.id}/item/#{item_key}"

response_body = JSON.dump(
"msg" => "Record not found",
"detail" => "Couldn't find dictionary item '{ service => #{service_id}, dictionary_id => #{dictionary.id}, item_key => #{item_key}, deleted => 0000-00-00 00:00:00'",
)

stub_request(:get, get_item_url).to_return(body: response_body, status: 404)

assert_nil dictionary.item('key')
end

it 'returns the corresponding dictionary item when present' do
item_key = 'key'
item_value = 'value'

response_body = JSON.dump(
"dictionary_id" => dictionary.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you're using double quotes here and single quotes elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy pasta from the api docs 🍝

"service_id" => service_id,
"item_key" => item_key,
"item_value" => item_value,
"created_at" => "2016-04-21T18:14:32+00:00",
"deleted_at" => nil,
"updated_at" => "2016-04-21T18:14:32+00:00",
)

get_item_url = "#{Fastly::Client::DEFAULT_URL}/service/#{service_id}/dictionary/#{dictionary.id}/item/#{item_key}"

stub_request(:get, get_item_url).to_return(body: response_body, status: 200)

item = dictionary.item('key')

assert_equal item_key, item.key
assert_equal item_value, item.value
end
end
end